Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MSSQL PIVOT TABLE

Posted on 2014-02-04
4
Medium Priority
?
516 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 Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
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
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

610 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