Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use option with dynamicSQL, <a href="http://msdn.microsoft.com/en-us/library/ms175156%28v=sql.105%29.aspx" rel="nofollow">APPLY()</a> and <a href="http://msdn.microsoft.com/en-us/library/ms177410%28v=sql.105%29.aspx" rel="nofollow">PIVOT</a> operators. In this scenario columns order in results, sorted by a column Col4.</p> <pre><code>DECLARE @cols nvarchar(max), @query nvarchar(max) SELECT @cols = STUFF((SELECT x.ColName FROM ( SELECT ',' + QUOTENAME('Col' + CAST(3 + ROW_NUMBER() OVER(PARTITION BY Col1, Col2, Col3 ORDER BY Col4) AS nvarchar(10))) AS ColName, ROW_NUMBER() OVER(PARTITION BY Col1, Col2, Col3 ORDER BY Col4) AS rn FROM dbo.test31 CROSS APPLY (VALUES('Col'), ('Col'), ('Col')) o(Col)) x GROUP BY x.ColName, x.rn ORDER BY x.rn FOR XML PATH(''), TYPE).value('.', 'nvarchar(max)'), 1, 1, '') SET @query = 'SELECT * FROM ( SELECT t.Col1, t.Col2, t.Col3, ''Col'' + CAST(3 + ROW_NUMBER() OVER(PARTITION BY t.Col1, t.Col2, t.Col3 ORDER BY t.Col4) AS nvarchar(10)) AS ColName, COALESCE(CAST(o.oCol4 AS nvarchar(10)), o.oCol5, o.oCol6) AS ListValues FROM dbo.test31 t CROSS APPLY ( SELECT oCol4, oCol5, oCol6 FROM (VALUES (t.Col4, NULL, NULL), (NULL, t.Col5, NULL), (NULL, NULL, t.Col6)) x(oCol4, oCol5, oCol6) ) o ) x PIVOT ( MAX(ListValues) FOR ColName IN(' + @cols + ') ) p' EXEC (@query) </code></pre> <p>Demo on <a href="http://sqlfiddle.com/#!3/e5c2b/1" rel="nofollow"><strong>SQLFiddle</strong></a></p> <p>Option with columns name like this " Plant, Brand, Area1,DownTime1,Reasons1,Area2,DownTime2,Reasons2 and so on."</p> <pre><code>DECLARE @cols nvarchar(max), @query nvarchar(max) SELECT @cols = STUFF((SELECT ',' + QUOTENAME(o.Col) AS ColName FROM ( SELECT *, CAST(ROW_NUMBER() OVER(PARTITION BY Plant, Brand ORDER BY Area) AS nvarchar(10)) AS rn FROM dbo.test35) t CROSS APPLY (VALUES('Area' + t.rn), ('DownTime' + t.rn), ('Reasons' + t.rn) ) o(Col) GROUP BY o.Col, t.rn ORDER BY t.rn FOR XML PATH(''), TYPE).value('.', 'nvarchar(max)'), 1, 1, '') SET @query = 'SELECT * FROM ( SELECT t.Plant, t.Brand, o.ColName, COALESCE(CAST(o.Area AS nvarchar(10)), o.DownTime, o.Reasons) AS ListValues FROM ( SELECT Plant, Brand, Area, DownTime, Reasons, CAST(ROW_NUMBER() OVER(PARTITION BY Plant, Brand ORDER BY Area) AS nvarchar(10)) AS rn FROM dbo.test35) t CROSS APPLY ( SELECT Area, DownTime, Reasons, ColName FROM (VALUES (t.Area, NULL, NULL, ''Area'' + t.rn), (NULL, t.DownTime, NULL, ''DownTime'' + t.rn), (NULL, NULL, t.Reasons, ''Reasons'' + t.rn)) x(Area, DownTime, Reasons, ColName) ) o ) x PIVOT ( MAX(ListValues) FOR ColName IN(' + @cols + ') ) p' EXEC (@query) </code></pre> <p>Demo on <a href="http://sqlfiddle.com/#!3/43a3c/1" rel="nofollow"><strong>SQLFiddle</strong></a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload