Skip to main content

SQL Server PIVOT

Dynamic Pivoting in SQL Server 

Introduction

PIVOT is the SQL Server keyword used to transpose the row data into column fashion. We can also say, pivot allows us to create column grouping.

Case Study

We have a table with Attribute (keeps city name) and value (contains sales Amt) as shown below.

We have to design a query which can work if any city city gets added.

Solution

Since we have to design the query such a way it can dynamically adjust the new city to new column, we need to use SQL dynamic query which can be stored in SQL variable and later executed through EXEC or SP_EXECUTESQL. We also have to keep in mind about the limitation of SQL the range of NVARCHAR(MAX) and columns.

-- Create table that accomodates Attributes and Values
CREATE TABLE tblAttVal
(
[Attribute] NVARCHAR(255),
[Value] NVARCHAR(255)
)
--Insert values to the table
INSERT INTO tblAttVal VALUES('Bangalore','4000')
INSERT INTO tblAttVal VALUES('Hyderabad','5000')
INSERT INTO tblAttVal VALUES('Bangalore','3000')
INSERT INTO tblAttVal VALUES('Hyderabad','2500')

--DECLARE SQL variables
DECLARE @ColList NVARCHAR(MAX) = '', @SQLQuery NVARCHAR(MAX) = ''

--Collect the list of colums for pivoting
SELECT @ColList = @ColList + '['+ [Attribute]+'],' FROM
(
SELECT DISTINCT [Attribute]
FROM tblAttVal) A

SET @ColList = LEFT(@ColList,LEN(@ColList) - 1)

SET @SQLQuery = 'SELECT * FROM 
(SELECT [Attribute],CAST([Value] AS INT) AS [Value] FROM tblAttVal ) P
PIVOT (SUM([Value]) FOR [Attribute] IN ('+@ColList+')) A'
EXEC(@SQLQuery)

Comments

Popular posts from this blog

DataZen Syllabus

INTRODUCTION TO DATAZEN PRODUCT ELEMENTS ARCHITECTURE DATAZEN ENTERPRISE SERVER INTRODUCTION SERVER ARCHITECTURE INSTALLATION SECURITY CONTROL PANEL WEB VIEWER SERVER ADMINISTRATION CREATING AND PUBLISHING DASHBOARDS CONNECTING TO DATASOURCES DESIGNER CONFIGURING NAVIGATOR CONFIGURING VISUALIZATION  PUBLISHING DASHBOARD WORKING WITH MAP  WORKING WITH DRILL THROUGH DASHBOARDS

PowerBI Interview Questions and Answers

Power BI Interview Questions – General Questions 1). What is self-service business intelligence? Ans: Self-Service Business Intelligence (SSBI) is an approach to data analytics that enables business users to filter, segment, and, analyse their data, without the in-depth technical knowledge in statistical analysis, business intelligence (BI). SSBI has made it easier for end users to access their data and create various visuals to get better business insights. Anybody who has basic understanding of the data can create reports to build intuitive and shareable dashboards. 2). What are the parts of Microsoft self-service business intelligence solution? Ans: Microsoft has two parts for Self-Service BI  Excel BI Toolkit – It allows users to create interactive report by importing data from different sources and model data according to report requirement.  Power BI – It is the online solution that enables you to share the interactive reports and queries that you have created using ...

MS BI Syllabus

Microsoft Business Intelligence Course Syllabus SSRS – SQL Server Reporting Services  Getting Started 1. Understanding Reporting (Authoring,Management,Delivery) 2. Installing Reporting (Native Mode, SharePoint Integration mode) 3. Building your first report  Authoring Reports 1. Developing Basic Reports (RDL,wizard,designer,datasource,dataset,formatting) 2. Working with expressions (expression to calculate value, Agg functions, exp for objects) 3. Organizing Data (Data Regions, Table, Matrix, Chart, List) 4. Advance Report (Parameter, drill down, drill through, links, 5. Report Model (Data Source, Data Source View, Model , Report Builder 3.0)  Managing Report ( Report Manager) 1. Managing Content (deploying report, folders, linked reports, datasources, value etc) 2. Managing Security (Item Level , Site navigation, localhost – sql) 3. Managing Server Config (Config Manager, Report Manager, Report Server DB)  Delivering Report 1. Accessing Report (Viewing...