Solved

MSSQL PIVOT TABLE

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

911 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

20 Experts available now in Live!

Get 1:1 Help Now