?
Solved

MSSQL PIVOT TABLE

Posted on 2014-02-04
4
Medium Priority
?
507 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 40

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 2000 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 41

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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

777 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