Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
risho
Starting Member
12 Posts |
Posted - 2013-04-17 : 13:12:54
|
I've got a sproc with a pivot where my columns are out of order and was wondering if someone can help me figure out how to correct this.The stored procedure looks like this: DECLARE @cols NVARCHAR(MAX)DECLARE @query NVARCHAR(MAX) SET NOCOUNT ON;SET TRANSACTION ISOLATION LEVEL READ COMMITTEDSELECT @cols = STUFF(( SELECT DISTINCT TOP 100 PERCENT '],[' + [Description] FROM dbo.vQualScoringGrade ORDER BY '],[' + [Description] FOR XML PATH('')), 1,2,'') + ']' SET @query = N'SELECT [Fee Period], ' + @cols + ' FROM (SELECT [Award Fee Period],GradeCount, Description, Value, OrderBy FROM dbo.vQualScoringGrade) pPIVOT (SUM([Value]) for [Description] IN ( ' + @cols + ' )) AS pvt ORDER BY [Fee Period]' execute(@query) The view [vQualScoringGrade] that feeds the sproc looks like this:SELECT TOP (100) PERCENT [Fee Period], GradeCount, Description, GradeCount AS Value, OrderByFROM (SELECT [Fee Period], 'No of Interim' AS Description, COUNT(VerNumber) AS GradeCount, 1 AS OrderByFROM dbo.vQualScoringExcellent AS vQualScoringExcellentGROUP BY GIIANumber, DescriptionUNIONSELECT [Fee Period], 'Excellent' AS Description, COUNT(Description) AS GradeCount, 2 AS OrderByFROM dbo.vQualScoringExcellent AS vQualScoringExcellent_1GROUP BY VerNumber, DescriptionUNIONSELECT [Fee Period], 'Very Good' AS Description, COUNT(Description) AS GradeCount, 3 AS OrderByFROM dbo.vQualScoringVeryGood AS vQualScoringVeryGoodGROUP BY VerNumber, DescriptionUNIONSELECT [Fee Period], 'Good' AS Description, COUNT(Description) AS GradeCount, 4 AS OrderByFROM dbo.vQualScoringGood AS vQualScoringGoodGROUP BY VerNumber, DescriptionUNIONSELECT [Fee Period], 'Satisfactory' AS Description, COUNT(Description) AS GradeCount, 5 AS OrderByFROM dbo.vQualScoringSatisfactory AS vQualScoringSatisfactoryGROUP BY VerNumber, DescriptionUNIONSELECT [Fee Period], 'Unsatisfactory' AS Description, COUNT(Description) AS GradeCount, 6 AS OrderByFROM dbo.vQualScoringUnsatisfactory AS vQualScoringUnsatisfactoryGROUP BY VerNumber, Description) AS QualScoringGradeORDER BY OrderBy So presently the columns appear from left to right in the following order:[Fee Period], [Excellent], [Good], [No of Interim], [Satisfactory], [Unsatisfactory], [Very Good].Instead I need the following order:[Fee Period], [No of Interim], [Excellent], [Very Good],[Good],[Satisfactory], [Unsatisfactory]. Is there a way to make this happen? Thanks.EJM |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-04-17 : 13:34:15
|
Order the query where you are constructing the comma-separated list. You can do what I am showing below, but I guess that would defeat the purpose of having a dynamic query. Is there another column that you can use for ordering? If there is use that column.SELECT @cols = STUFF(( SELECT DISTINCT TOP 100 PERCENT '],[' + [Description] FROM dbo.vQualScoringGrade ORDER BY '],[' + CASE WHEN [Description]= 'Fee Period' THEN 0 WHEN [Description]= 'No Interim' THEN 1 WHEN [Description]= 'Excellent' THEN 2 -- etc END FOR XML PATH('')), 1,2,'') + ']' |
|
|
risho
Starting Member
12 Posts |
Posted - 2013-04-17 : 14:23:54
|
quote: Originally posted by James K Is there another column that you can use for ordering?
There is, I call it "OrderBy" and with each query I assing a numerical value between 1 and 6. I've tried to change my query in the sproc but I was under the impression that entire line "ORDER BY '],[' + [Description] FOR XML PATH('')), 1,2,'') + ']'" is needed to build the colums. When I change that by adding a my sort column, it breaks.EJM |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-04-17 : 15:05:20
|
Is it complaining about syntax error? Can you try this?SELECT @cols = STUFF(( SELECT DISTINCT TOP 100 PERCENT '],[' + [Description] FROM dbo.vQualScoringGrade ORDER BY -- '],[' + CASE WHEN [Description]= 'Fee Period' THEN 0 WHEN [Description]= 'No Interim' THEN 1 WHEN [Description]= 'Excellent' THEN 2 -- etc END FOR XML PATH('')), 1,2,'') + ']' |
|
|
risho
Starting Member
12 Posts |
Posted - 2013-04-17 : 16:09:21
|
This is the solution:SELECT @cols = STUFF(( SELECT '],[' + [Description] FROM dbo.vQualScoringGrade GROUP BY [Description] ORDER BY MAX([orderby]), [Description] FOR XML PATH('')), 1,2,'') + ']'Thanks for your help!EJM |
|
|
|
|
|
|
|