I have a query that gives me this result:
|SystemName |SystemDescription |CertificateType |CertName |CertDate | ---------------------------------------------------------------------------- | ABC |Blah-blah ABC | X | X-865567 | 1 Jan | | ABC |Blah-blah ABC | Y | Y-8a5567 | 2 Jan | | ABC |Blah-blah ABC | Z | Z-86af67 | 3 Jan | | EFG |Blah-blah EFG | X | X-8kuhkj | 4 Jan | | EFG |Blah-blah EFG | Y | Y-uiiou | 5 Jan | | EFG |Blah-blah EFG | Z | Z-8lkjhu | 6 Jan | ----------------------------------------------------------------------------
I need to produce this result:
|SystemName |SystemDescription | X-Name | X-Date | Y-Name | Y-Date | Z-Name | Z-Date| --------------------------------------------------------------------------------------- |ABC | Blah-blah ABC |X-865567 | 1 Jan |Y-8a5567| 2 Jan |Z-86af67| 3 Jan | |EFG | Blah-blah EFG |X-8kuhkj | 4 Jan |Y-uiiou | 5 Jan |Z-8lkjhu| 6 Jan | ----------------------------------------------------------------------------------------
In other words I need to pivot without aggregation and with at least 2 columns not pivoted.
Is it possible to produce this without functions, stored procedures and temp tables?? I know how to easily implement this with a few funcitons, I can come up with solution with temp table.
I wonder if it is possible to get a neat solution using PIVOT??
p.s. this is not a homework, just a brain-teaser -)
It is not necessary to use
PIVOTto do this, sometimes it’s more of a mess to use it than it’s worth. As far as not using aggregate functions – think about it, you want to group your records in terms ofSystemNameandSystemDescription, you don’t have a choice but to use aggregates. That’s ok though, just useMAXas I have done, you’re going to get the results you want. The only thing you would need to worry about is in the scenario where there are two records with sameSystemName,SystemDescriptionandCertificationTypethat have different values forCertNameorCertDate– in that case you need some way to choose which attributes are selected for the group.Here is a non-Pivot example: