Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
SQL server 7
Database 6
Tips 5
Tricks 5
#SQLServer 3
Backup 2
MSSQL 1
'E0' 1
isnumeric 1
Issues 1

Archive · View All
December 2011 4
March 2012 2
January 2012 1

Raghunath Bhandari's Blog

Dynamic PIVOT Query For Cross Tab Result

Jan 5 2012 11:46AM by Raghunath Bhandari   

Cross tab result is always required and it is bit complex in case of dynamic column. If column name has to be dynamic then this is the good idea to have the PIVOT Query,
for example I have 3 Tables and data like this:

First Table

-----------------------------------------------------------
IF OBJECT_ID('tempdb..#customer') IS NOT NULL
DROP TABLE #customer

CREATE TABLE #customer (
CustomerId INT,
CustomerName VARCHAR(50)
)

INSERT #customer
SELECT 1, 'Ram'
INSERT #customer
SELECT 2, 'Hari'
INSERT #customer
SELECT 3, 'Depesh'
INSERT #customer
SELECT 4, 'Sailesh'

--Select * from #customer

Second Table

-----------------------------------------------------------
IF OBJECT_ID('tempdb..#LoanType') IS NOT NULL
DROP TABLE #LoanType

CREATE TABLE #LoanType (
LoanId INT,
LoanName VARCHAR(50)
)

INSERT #LoanType
SELECT 1, 'Loan-1'
INSERT #LoanType
SELECT 2, 'Loan-2'
INSERT #LoanType
SELECT 3, 'Loan-3'
INSERT #LoanType
SELECT 4, 'Loan-4'
INSERT #LoanType
SELECT 5, 'Loan-5'
INSERT #LoanType
SELECT 6, 'Loan-6'

--Select * from #LoanType


——————————————————————————

Third Table

-----------------------------------------------------------
IF OBJECT_ID('tempdb..#LoanCustomer') IS NOT NULL
DROP TABLE #LoanCustomer

CREATE TABLE #LoanCustomer (
CustomerId INT,
LoanId INT,
Amount MONEY
)

INSERT #LoanCustomer
SELECT 1, 1, 500
INSERT #LoanCustomer
SELECT 2, 1, 5300
INSERT #LoanCustomer
SELECT 3, 3, 1500
INSERT #LoanCustomer
SELECT 2, 1, 2500
INSERT #LoanCustomer
SELECT 1, 6, 5500
INSERT #LoanCustomer
SELECT 2, 4, 1100
INSERT #LoanCustomer
SELECT 2, 3, 3200
INSERT #LoanCustomer
SELECT 2, 3, 3200

--Select * from #LoanCustomer

Combine Result of three table from PIVOT Query:

DECLARE
@sql VARCHAR(MAX)
,@LoanType VARCHAR(MAX)

SELECT
@LoanType = ISNULL(@LoanType + ', ', '') + '[' + LoanName + ']'
FROM (
SELECT
DISTINCT
l.LoanName
FROM #LoanCustomer lc
INNER JOIN #LoanType l ON lc.LoanId = l.LoanId
) x

SET @sql = '
SELECT
CustomerName
,' + @LoanType +
Sample for Dynamic PIVOT

FROM (
SELECT
c.CustomerName,
l.LoanName,
lc.Amount
FROM #LoanCustomer lc
INNER JOIN #customer c ON lc.CustomerId = c.CustomerId
INNER JOIN #LoanType l ON l.LoanId = lc.LoanId
) dataList
PIVOT
(
SUM(Amount)
FOR LoanName IN(' + @LoanType+ ')
) X '

EXEC (@sql)

Tags: Database, SQL server, Tricks, Tips, #SQLServer, TSQL, PIVOT, Cross tab result, SQLServer,


Raghunath Bhandari
183 · 1% · 253
2
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

1  Comments  

  • The following is fully dynamic and very flexible to use http://beyondrelational.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspx

    commented on Jan 6 2012 2:39AM
    Madhivanan
    3 · 39% · 12419

Your Comment


Sign Up or Login to post a comment.

"Dynamic PIVOT Query For Cross Tab Result" rated 5 out of 5 by 2 readers
Dynamic PIVOT Query For Cross Tab Result , 5.0 out of 5 based on 2 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]