Skip to content

Tag: tsql

Inserting rows based on conditions

Table Name: CaseHistory I’m currently working with a dataset that aims to offer insights on customer support cases on a weekly basis. In the example above, you can see the progression of case 123376 from Week 1 till Week 5, amongst other cases. What I would like to be able to do is insert a row for a ca…

Query for Exam Score calculation

Problem Statement: I have an EXAM table which has following fields and sample data I want to write a SQL query to output the following fields My SQL Query: Can I achieve my result by writing a better SQL query? Answer You could indeed join the table with several aggregate subqueries that compute the relevant …

How to select two values in one row?

Seems extremely basic, but I can’t find an elegant solution to it. I have two SQL tables, say one contains the users, and the other one, the cities. A third table contains the matches between the two …

Group by values that are each multiple of number

This is the table t. I want to group it every time the TotalQty >= 5n (let n = group). i.e. once the TotalQty >= 5n I want to sum together the qty from n-1 to n. With this query I can get pretty close but I doesn’t consider the the previous “valid” TotalQty + 5 — How do I