Hi
I have data like below. I dont want to use PIVOT. Departments are Dynamic. There can be more Departments.
| Posting Date | Location | Amount | Deptt |
| 11/8/2024 | 1 | 149,435.00 | Accounts |
| 24-08-2024 | 1 | 62,891.00 | Accounts |
| 25-09-2024 | 1 | 133,938.00 | Accounts |
| 26-09-2024 | 1 | 260,056.00 | Accounts |
| 7/4/2024 | 1 | 1,416.00 | Sale |
| 30-04-2024 | 1 | 15,115.80 | Sale |
| 12/4/2024 | 2 | 9,546.20 | Sale |
| 28-04-2024 | 2 | 9,148.00 | Sale |
I want Data to be displayed like below without using Pivot
| Apr-24 | Apr-24 | Aug-24 | Aug-24 | Sep-25 | Sep-24 | |
| Sale | Accounts | Sale | Accounts | Sale | Accounts | |
| 1 | 16531 | 0 | 0 | 212326 | 0 | 393994 |
| 2 | 18694 | 0 |
Thanks
Sarthak VarshneyPosted Mar 10, 2025, 4:09 AM
The
PIVOTclause in SQL is used to rotate row-based data into columnar format, making it easier to read and analyze. Let's break down the specific PIVOT statement you mentioned:This PIVOT operation transforms row values into columns dynamically based on the
Period_Depttcolumn.Breaking It Down
Aggregation Function (
SUM(Amount))SUM(Amount), which ensures that all amounts corresponding to a given Location, Month-Year, and Department are summed.FOR Clause (
FOR Period_Deptt IN (...))Period_Depttis the column containing Month-Year + Department (e.g.,"Apr-24_Sale","Aug-24_Accounts").IN (...)list specifies which values should become new column headers.@colsvariable holds this dynamic list of unique Month-Year and Department values.Alias (
AS PivotTable)FROMclause.Ramco RamcoPosted Mar 10, 2025, 3:29 AM
Hi Sarthak
Below u have used PIVOT
Thanks
Sarthak VarshneyPosted Mar 9, 2025, 5:41 PM
Hii Ramco Ramco
SQL Query (Without PIVOT)
Explanation
SUM(CASE WHEN...)CASEstatement ensures that only values for the matching condition are summed.FORMAT([Posting Date], 'MMM-yy')GROUP BY LocationLocation, summing up amounts for each condition.Ramco RamcoPosted Mar 9, 2025, 6:05 AM
Hi Sarthak
I dont want to use PIVOT. I want without PIVOT.
Thanks
Sarthak VarshneyPosted Mar 8, 2025, 5:12 PM
You can achieve this using conditional aggregation with
CASEstatements instead ofPIVOT. Below is a dynamic SQL approach to generate the required format without explicitly usingPIVOT:Explanation
Generate Column Names Dynamically:
FORMAT([Posting Date], 'MMM-yy')(Month-Year) andDeptt.STRING_AGG()to concatenate them dynamically.Construct Dynamic Query:
CASEinsideSUM()to conditionally sum amounts for each department and time period.Execute Query:
sp_executesql.Ramco RamcoPosted Mar 8, 2025, 7:59 AM
Hi
I need Sql query
Thanks.
Eliana BlakePosted Mar 8, 2025, 7:04 AM
Certainly! It seems like you have a table that contains data related to Posting Date, Location, Amount, and Department. You're looking to transform this data into a structured format without using PIVOT. Based on the provided data, the output table organizes the information by Department and Posting Date.
For example, in the transformed table:
- The columns represent different months and years.
- The rows show the total Amount for each Department within the respective month and year.
To achieve this transformation without using PIVOT, you can utilize SQL queries with conditional aggregation. By grouping the data based on Department and month-year combinations, you can calculate the total Amount for each category dynamically.
If you need a specific SQL query or further explanation on implementing this transformation, feel free to let me know!