Skip to content

Add a variable in syntax

In this example which runs here live I would like to add the user id in the retrieved data I use this ;with postvotes as ( select v.creationdate, v.userid , p.id postid , case v….

How to compare column combinations from two tables?

I have two tables from different years, 2015 and 2016. Both tables have three columns as exampled below in table_2015: I want to see how many people had the same plan type between the years and how many people switched plan types. How do I go about doing this? Answer If customers are in each table only once a…

Getting wrong sum when using group by with inner join

With reference to this question (How to get the sum in a joined table when using group by – getting wrong results) I have two tables orders and order_items. I need to group the results by days. But I also need to get the sum of energy_used for each day from another table. When I try that using a join,

Update Rows Where the following conditions are true

I am attempting to overwrite a field value with the value from another table where a certain condition is true. I have mocked up my code below Pseudocode: My Code: But this is returning the error: I’m not sure where i am going wrong exactly Answer If you are looking for the team “ops”, then …

How can I generate JSON results in older SQL Server versions?

I have a table which contains data similar to this. I want to get ItemNames in JSON format as I have mentioned in the expected output below. Expected output: Note: I know in latest SQL Server, there are functions FOR JSON to convert to JSON format but it won’t work in older SQL versions. Therefore, I am…