Solved

MSSQL PIVOT TABLE

Posted on 2014-02-04
4
492 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

816 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

10 Experts available now in Live!

Get 1:1 Help Now