January 28, 2015 at 12:33 pm
Hello,
I have a table that needs to be transposed and I got a solution from stackoverflow:
The original table contains the following columns:
[DETECT_DATE_TS],
[POLICY_NAME],
High
,MedLow
,EndPoint
,Reviewed
,Unreviewed
,Escalated
The table contains 6 different [POLICY_NAME], the transposed result should look like:
Severity Policy1 Policy2 Policy3 Policy4 Policy5 Policy6
High
MedLow
EndPoint
Reviewed
Unreviewed
Escalated
For each Policy field it shows different number (integer)
By following the stackoverflow solution produces wrong number, here is the original solution:
select *
from
(
select day, col, value
from yourtable
unpivot
(
value
for col in (A, B)
) unpiv
) src
pivot
(
max(value)
for day in (Mon, Tue, Wed, Thu, Fri)
) piv
Here is my adapted script:
select * from
(select POLICY_NAME, Severity, Value From
(
MyTable
) src
unpivot
(
Value for
Severity in (High
,MedLow
,EndPoint
,Reviewed
,Unreviewed
,Escalated)
) unpiv
)Src
Pivot
(
max(Value)
for POLICY_NAME in (PII, Confidential, PCIDSS, Inappropriate, Uncategorized, Other)
) piv
Thank you very much if you can point out why the adapted script returns wrong result.
January 28, 2015 at 5:12 pm
What happens when you have more than 6 policies in any of the categories?
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
January 29, 2015 at 7:56 am
dwain.c (1/28/2015)
What happens when you have more than 6 policies in any of the categories?
Thanks.
The number of output is fixed and won't change in the future.
January 29, 2015 at 4:51 pm
sonchoix (1/29/2015)
dwain.c (1/28/2015)
What happens when you have more than 6 policies in any of the categories?Thanks.
The number of output is fixed and won't change in the future.
Then you should probably read this article:
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns [/url] by Jeff Moden
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply