Skip to content

Tag: sql-server

Rotate Database Table

Imagine I have a database table that has the following format: ╔══════════════════════╗ ║ Name Quarter Sales ║ ╠══════════════════════╣ ║ Joe Q1 700 ║ ║ Joe Q2 650 ║ ║ Joe Q3 …

LAG function with two different partitions

I have a table with multiple calendars inside it and I need to get the previous business day for each row in the table. I’ve been trying to use the LAG function to achieve this but I’m unable to get the correct return for every item. I’ve ended up splitting it into two queries. When the day …

Display date values as column

Below is my code to display date values as column name. But it won’t get the new data of the next month unless encoded in Pivot values. How should I do it dynamically? Below is the sample output. Answer You could use “real” dynamic SQL: First get your list of months and store it into a nvarc…

Bring a row into a column

I have the following table as my input: Date | Agent Name | Agent Department | Call ID | Transfer? | TransferToAgent_Dept ————————————————————————…