0

I have query:

SELECT DISTINCT temp.ID,request.RequestTypeID
FROM @MyTempTable6 as temp
JOIN dbo.FingerMachineUsers as fingeruser
ON temp.UserNo = fingeruser.ID
JOIN dbo.AppUsers as appuser
ON appuser.Id = fingeruser.UserId
LEFT JOIN dbo.Requests as request
ON request.UserId = fingeruser.UserId 

And result of it:

enter image description here

How can I create table like this:

ID|RequestTypeID1|RequestTypeID2
1 |             4|             5
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Thien Vu
  • 61
  • 1
  • 8

4 Answers4

0

If you have only two values, then the simplest method is aggregation:

SELECT t.ID, MIN(r.RequestTypeID), MAX(r.RequestTypeID)
FROM @MyTempTable6 t JOIN
     dbo.FingerMachineUsers fu
     ON t.UserNo = fu.ID JOIN
     dbo.AppUsers au
     ON au.Id = fu.UserId LEFT JOIN
     dbo.Requests r
     ON r.UserId = fu.UserId
GROUP BY t.ID;

If you have have a variable number of values that you want to present, then the query is much more complicated.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

PIVOT operator could be your best friend, if there are always only 2 values each...

Antonio
  • 535
  • 10
  • 26
0

Try this query...

Disclaimer: This code is purely based on this answer. (https://stackoverflow.com/a/10404455/6327676)

DECLARE @cols  AS NVARCHAR(max), 
        @query AS NVARCHAR(max); 

SET @cols = Stuff((SELECT DISTINCT ',' + Quotename(stff.requesttypeid)
                   FROM   TableName stff
                   FOR xml path(''), type).value('.', 'NVARCHAR(MAX)'), 1, 1, '' 
            ) 
SET @query = 'SELECT ID, '+ @cols
             + ' FROM (select ID, RequestTypeId FROM TableName) x pivot (MAX(RequestTypeId) FOR RequestTypeId in (' 
             + @cols + ')) p' 

EXECUTE(@query) 
DxTx
  • 3,049
  • 3
  • 23
  • 34
0

Try this code it will work according to the result you want. Firstly you need to dump @MyTempTable6 table into #temptable then set RequestTypeID columns as comma seperated in @colums varible then set the columnname in @Requestcolumns variable,Then use pivot.

DECLARE @colums AS NVARCHAR(max)
DECLARE @Requestcolumns AS NVARCHAR(max)
DECLARE @query AS NVARCHAR(max);


SELECT DISTINCT temp.ID,request.RequestTypeID
into #temptable
FROM @MyTempTable6 as temp
JOIN dbo.FingerMachineUsers as fingeruser
ON temp.UserNo = fingeruser.ID
JOIN dbo.AppUsers as appuser
ON appuser.Id = fingeruser.UserId
LEFT JOIN dbo.Requests as request
ON request.UserId = fingeruser.UserId  

SET @colums = Stuff((SELECT DISTINCT ',' +Quotename(tab.RequestTypeID)
                   FROM   #temptable tab
                   FOR xml path(''), type).value('.', 'NVARCHAR(MAX)'), 1, 1, '' 
            )      

SET @Requestcolumns = Stuff((SELECT DISTINCT ',' +Quotename(tab.RequestTypeID) +' AS ',+Quotename('RequestTypeID'+CONVERT(varchar(100),tab.RequestTypeID))
                   FROM   #temptable tab
                   FOR xml path(''), type).value('.', 'NVARCHAR(MAX)'), 1, 1, '' 
            ) 

SET @query = 'SELECT ID, '+@Requestcolumns
             + ' FROM (select ID, RequestTypeID FROM #temptable) x pivot (MAX(RequestTypeID) FOR RequestTypeID in (' 
             + @colums + ')) p' 

EXECUTE(@query) 

DROP table #temptable
Sylvia
  • 80
  • 5