I have three tabels, each of them has a date column (the date column is an INT field and needs to stay that way). I need a UNION accross all three tables so that I get the list of unique dates in accending order like this: Then I need to add a column to the result of the query where
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 How would I show a rolling 3 month average for this query
Need some help with maths side of things with this bit of code. You may see I need help on even more! But any help you could give me would be great Basically, my aim is to bring back an; – AccountID,…
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.
How to rollback stored procedure that updates a table
Why doesn’t this ROLLBACK TRANSACTION statement work? BEGIN TRANSACTION; DECLARE @foo INT EXECUTE [database].[dbo].[get_counter] @CounterID=’inventory_records’, @nextValue=@foo OUTPUT; ROLLBACK …
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