Solved

MSSQL PIVOT TABLE

Posted on 2014-02-04
4
481 Views
Last Modified: 2014-02-04
Hello,
I have a query that give a result that I have to pivot.
I'm really new to pivot for mssql , so getting a little disappointed....
thanks for your help

here the query

SELECT ID_SERVICE,ID_DROIT,CODE_REVENDEUR from fn_function('8909' )
ORDER BY ORDRE_SERVICE,ORDRE_DROIT

the result, and the expected result is in the attached file.

thanks for any help .

regards.
PIVOT.xlsx
0
Comment
Question by:bruno_boccara
4 Comments
 
LVL 39

Expert Comment

by:lcohan
ID: 39833456
SELECT ID_SERVICE,
            ID_DROIT,
        [1] AS [1st],
        [2] AS [2nd],
        [3] AS [3rd],
        [4] AS [4th],
        [5] AS [5th],
        [6] AS [6th],
            [7] AS [7th]
   FROM (SELECT ID_SERVICE,ID_DROIT,CODE_REVENDEUR FROM fn_function('8909'))  AS src --(1)
  PIVOT (SUM(CODE_REVENDEUR) FOR ID_DROIT IN ([1],[2],[3],[4],[5],[6],[7])) AS pvt --(2)
  ORDER BY ORDRE_SERVICE,ORDRE_DROIT


--or like this if you want NULLs converted to 0

--===== Use a Pivot to do the same thing we did with the Cross Tab
 SELECT ID_SERVICE,
            ID_DROIT,
        COALESCE([1],0) AS [1st],
        COALESCE([2],0) AS [2nd],
        COALESCE([3],0) AS [3rd],
        COALESCE([4],0) AS [4th],
        COALESCE([5],0) AS [5th],
        COALESCE([6],0) AS [6th],
            COALESCE([7],0) AS [7th]
   FROM (SELECT ID_SERVICE,ID_DROIT,CODE_REVENDEUR FROM fn_function('8909'))  AS src --(1)
  PIVOT (SUM(CODE_REVENDEUR) FOR ID_DROIT IN ([1],[2],[3],[4],[5],[6],[7])) AS pvt --(2)
  ORDER BY ORDRE_SERVICE,ORDRE_DROIT
0
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 500 total points
ID: 39833494
you can try the below code, leave the insert statements, they are there to create your test data.

drop table #t

CREATE TABLE  #T
(
ID_SERVICE	INT, ID_DROIT	INT, CODE_REVENDEUR INT
)

INSERT INTO #T VALUES(5,	1,	NULL)
INSERT INTO #T VALUES(5,	2,	NULL)
INSERT INTO #T VALUES(5,	3,	8909)
INSERT INTO #T VALUES(5,	4,	8909)
INSERT INTO #T VALUES(1,	1,	NULL)
INSERT INTO #T VALUES(1,	2,	NULL)
INSERT INTO #T VALUES(1,	3,	NULL)
INSERT INTO #T VALUES(1,	4,	NULL)
INSERT INTO #T VALUES(3,	1,	NULL)
INSERT INTO #T VALUES(3,	2,	NULL)
INSERT INTO #T VALUES(3,	3,	NULL)
INSERT INTO #T VALUES(3,	4,	8909)
INSERT INTO #T VALUES(7,	1,	NULL)
INSERT INTO #T VALUES(7,	2,	NULL)
INSERT INTO #T VALUES(7,	3,	NULL)
INSERT INTO #T VALUES(7,	4,	NULL)
INSERT INTO #T VALUES(2,	1,	NULL)
INSERT INTO #T VALUES(2,	2,	NULL)
INSERT INTO #T VALUES(2,	3,	NULL)
INSERT INTO #T VALUES(2,	4,	NULL)
INSERT INTO #T VALUES(6,	1,	NULL)
INSERT INTO #T VALUES(6,	2,	8909)
INSERT INTO #T VALUES(6,	3,	NULL)
INSERT INTO #T VALUES(6,	4,	NULL)

SELECT  ID_SERVICE,[1],[2],[3],[4]
FROM
(select   ID_SERVICE,ID_DROIT,CODE_REVENDEUR FROM #T) P 
PIVOT
(
AVG(CODE_REVENDEUR) FOR ID_DROIT IN ([1],[2],[3],[4])
) as PVT

-- the above code works only when the ID_DROIT has the values of 1,2,3 and 4, if that is not the case and if they can have more values then you can use the dynamic pivot as given below 

DECLARE @SQL        NVARCHAR(MAX)
DECLARE @ColList    NVARCHAR(MAX)
SET @ColList = (SELECT STUFF((SELECT ',' + '[' + CAST(ID_DROIT AS VARCHAR) + ']' FROM  (SELECT DISTINCT ID_DROIT FROM #T ) A FOR XML PATH('')),1,1,'')) 

SET @SQL = 'SELECT  ID_SERVICE, ' + @ColList 
+ 'FROM
(select   ID_SERVICE,ID_DROIT,CODE_REVENDEUR FROM #T) P 
PIVOT
(
AVG(CODE_REVENDEUR) FOR ID_DROIT IN ('
+ @ColList 
+ ')
) as PVT'

EXEC (@SQL)

Open in new window

0
 
LVL 40

Expert Comment

by:Sharath
ID: 39833508
try this.
SELECT * 
  FROM Test
 PIVOT(MAX(CODE_REVENDEUR) FOR ID_DROIT IN ([1],[2],[3],[4]) ) AS P

Open in new window

http://sqlfiddle.com/#!3/61950/3
0
 

Author Closing Comment

by:bruno_boccara
ID: 39833999
MANY THANKS.

great solution.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now