TSQL – Using Pivot Table

A Pivot table in TSQL is useful to spread row data as columns. Row values can be defined into multiple columns.

A row in SQL Server represents an entity and a column as an attribute of the entity.

To build a pivot table, you will need to define three columns.

  • Column 1 – Row grouping
  • Column 2 – Create new columns based on the values
  • Column 3 – Aggregated values for the row and columns

Steps to build a pivot table:

  1. Select based dataset
  2. Create temporary result set using Common Expression Table (CTE)
  3. Apply PIVOT operator

Example

This is the original table

Rate_IdDescR01R02R03R04
1Plan14.1025.2028.5040.50
2Plan21.5039.5041.8060.90

This will be the final result

PlanSilverGold
Plan-114.1021.50
Plan-225.2039.50
Plan-328.5041.80
Plan-440.5060.90

Step 1: Create the table

CREATE TABLE #Rate (
Rate_Id int,
Rate_Desc varchar(100),
R01 decimal(17,2),
R02 decimal(17,2),
R03 decimal(17,2),
R04 decimal(17,2)
);

Step 2: Insert records

INSERT INTO #Rate VALUES (1,'Plan',14.10,25.20,28.50,40.50);
INSERT INTO #Rate VALUES (2,'Plan',21.50,39.50,41.80,60.90);

Step 3: Use cross apply to create additional rows

SELECT * FROM #Rate
cross apply
(
   select 1,R01 union
   select 2,R02 union
   select 3,R03 union
   select 4,R04 
) r (RowNo, Rate)

Output

Rate_IdRate_DescR01R02R03R04RowNoRate
1Plan14.1025.2028.5040.50114.10
1Plan14.1025.2028.5040.50225.20
1Plan14.1025.2028.5040.50328.50
1Plan14.1025.2028.5040.50440.50
2Plan21.5039.5041.8060.90121.50
2Plan21.5039.5041.8060.90239.50
2Plan21.5039.5041.8060.90341.80
2Plan21.5039.5041.8060.90460.90

Step 4: Use PIVOT to change rows to columns

WITH CTE_Rates As (
SELECT * FROM #Rate
cross apply
(
   select 1,R01 union
   select 2,R02 union
   select 3,R03 union
   select 4,R04 
) r (RowNo, Rate))
SELECT [Plan], [1] as [Silver], [2] as [Gold]
FROM (
SELECT Rate_Id,CONCAT(Rate_Desc,'-',RowNo) as [Plan], Rate_Desc, RowNo,Rate
FROM CTE_Rates) r
PIVOT(
 Max(Rate)
  for Rate_Id IN ([1],[2])
) p
;

Final table result from Step 4

PlanSilverGold
Plan-114.1021.50
Plan-225.2039.50
Plan-328.5041.80
Plan-440.5060.90

In this example I have converted Rate_Id 1 and 2 as Silver and Gold. Also R01, R02, R03, R04 as Plan-1, Plan-2, Plan-3, Plan-4.

Code #1: Reverse An Array

How to reverse an array (in C#) without using Array.Reverse() method?

int[] arr = {1,3,4,9,8};
for (int i = 0; i < arr.Length / 2; i++)
{
int tmp = arr[i];
arr[i] = arr[arr.Length - i - 1];
arr[arr.Length - i - 1] = tmp;
}
Console.WriteLine(string.Join(",", arr));

Market Movers – July 2020

July 6

  • ISM Non-Manufacturing Index for June increased to 57.1 (Note: above 50 is expansion)

July 2

  • June non-farm payroll jobs increased by 4.8 million.
  • A decline in the unemployment rate to 11.1%.
  • Sector performance: Energy (2.95%), Financials (2.17%), Industrials (1.98%), Materials (1.97%), Consumer Discretionary (1.33%), Information Technology (1.31%), Consumer Staples (1.20%), Real Estate (1.04%), Health Care (0.85%), Comminucation Services (0.72%), Utilities (0.59%)

July 1

  • Promising signs of COVID vaccine by Pfizer Link
  • ISM Manufacturing Index 52.6 (above 50 is Expansion) Link
  • Private sector Payroll Gain (2.37 million jobs) Link
  • FOMC’s June meeting Link

Credit Cards: