Skip to content

Tag: sql-server

Sort column values in a particular way

I have 2 tables: I want to get average of points for each student and each course and sort the result by Student ID and Course in order (Mathematics,Physics and Chemistry). The output should be the following: For each Student the course order should be (Math,Phys, Chem) and also it should be ordered by Studen…

SQL – get rid of the nested aggregate select

There is a table Payment, which for example tracks the amount of money user puts into account, simplified as What is need is to receive a table with balance before payment moment, eg: Currently the select statement looks something like How can I rewrite this select to get rid of the nested aggregate selection…

SQL ROLLUP with COALESCE

The data: The query: The output I get is like this: It gives null every individual brand item not grouped, and I want the last null to be ‘Grand Total’ not ‘Monthly Total’. I tried putting PurchaseDate or Brand column in and out the ROLLUP but it doesn’t seem to work for the Bran…

How to use Left Join in subquery SQL Server?

I am trying to use left join in subquery in SQL Server. My query looks fine to me but it gives syntax error. This is my query: This is the error I’m getting: This is my table structure: Answer As @Chris mentioned, the query is bit incomplete. I guess you are trying to do something like this: This query …

LEFT Join on a Subquery with specific criteria

I have two tables that I am trying to JOIN I am attempting to Select from table1 a and LEFT JOIN table2 b on a.id = b.id – however, I want to only select on the id in table2 where MAX(EndDt) Is something like that doable? Answer There are a few ways you can do this. I make some assumptions