Skip to content

Tag: sql-server

Trigger to update a table whenever there is an insert

I am trying to use a trigger (in SQL Server) to update a table whenever there is an insert on the table but I get an error: Conversion failed when converting date and/or time from character string. Trigger used: The table table_scan is to be updated when there is NULL in start_date after an insert happens. An…

Returning multiple aggregated columns from Subquery

I am trying to extend an existing query by aggregating some rows from another table. It works when I only return one column like this: Then I tried to add another column for the aggregated volume: However, this returns the following error: Only one expression can be specified in the select list when the subqu…

finding missing dates in a time interval (SQL)

I am a bit of a noob with SQL, so I was searching for some bit of code that might help me find missing date values for a time interval when I stumbled upon this code. Link to the code It works really well for my problem but I am not able to understand how it increments the date. I

Get difference from top ranked item

I’m using the following query to rank items, but I need it to also show the difference between the item and the top ranked item (The top ranked item will show the difference between it and the 2nd ranked item.), like so: How do I create the DiffTop column? Answer You can use window functions for this as…

Order By in Union Nested selects

I have two really long selects that are both nested How can I order these (both queries combined into one table results) by NewestMessage which is a type of datetime? Answer I would slightly alter your query to: I am basically replacing your union of tuples to a subquery on a union query, using an ORDER BY wi…