Skip to content
Advertisement

Tag: tsql

Join two columns as a date in sql

I am currently working with a report through Microsoft Query and I ran into this problem where I need to calculate the total amount of money for the past year. The table looks like this: How would you calculate the total amount from 02-2019 to 02-2020 for the item number 12345? Answer Assuming that you are running SQL Server, you

SQL CASE statement needs to handle Text

Apologies if this has been asked before – I’ve spent a couple of hours searching but not found anything that’s helped. It’s quite simple really – I’ve been asked to create a query which includes a field that when it was set up (not by me) was created as a VARCHAR instead of an INT. I need to do some

How can I run this SELECT query?

I’m working on a Messaging feature for an application, and I have a the following tables in MSSQL, along with a User table (not listed): I want to query the ThreadParticipant table for whether or not a thread exists containing only a given set of user ids, let’s say users 1,3,5,7. Each Thread contains a unique set of user ids.

SQL Server substring throws error “multiple results”

I use Microsoft SQL Server 2016. I have a column that is called Failover and looks like his: I want that number so I use : It works fine, but if I want a second column called Account, it crashed with multiple results… How to fix this ? Is there a simple way to take the second number and third?

Check for condition in GROUP BY?

Take this example data: I need an SQL statement that will group all the data but bring back the current status. So for ID 1 the group by needs a condition that only returns the Completed row and also returned the pending rows for ID 2 and 3. I am not 100% how to write in the condition for this.

How to get weekending having all approved records?

I have a table like below now want to get weekending ‘3/1/2020’ since it have all 1’s in isApproved like this I want to get list of weekending having all 1’s in isApproved Answer You can use aggregation and filter with a having clause: This works because isapproved only has 0/1 values. So condition min(isapproved) = 1 actually ensures that

How can I get values of the Rating column together with their definition for every specific month by using Pivot with Month in MSSQL

Example : Data: Query: I get this error: Incorrect syntax near ‘,’. In line with ” Definition, Rating ” Result should look like this: Is this possible? Any help, please! Thank you! *The code above was based on PIVOT with MONTH(). Answer You could just use conditional aggregation: This syntax is somehow more flexible than the specific PIVOT operator (and

Advertisement