Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Dynamic Grouping


Dynamic Grouping

Author
Message
danielfountain
danielfountain
Right there with Babe
Right there with Babe (789 reputation)Right there with Babe (789 reputation)Right there with Babe (789 reputation)Right there with Babe (789 reputation)Right there with Babe (789 reputation)Right there with Babe (789 reputation)Right there with Babe (789 reputation)Right there with Babe (789 reputation)

Group: General Forum Members
Points: 789 Visits: 888
Hey all,

I had a query that i didnt like - as it was a pain to maintain. Basically there are 6 levels of grouping that can be requested by a stored proc. So i had

IF @level =1
begin
select ....
end
ELSE IF @level=2
begin
select .....
end

I am sure you get the idea. Well i wondered if could do something like this



SELECT metricind
,LegalEntity
,SLKBU
,SLKDepartment
,SLKTeam
,CASE WHEN @Level = 6 THEN AssignedHandler ELSE '' END AS AssignedHandler
,Banding
,BandingOrder
,SUM(LedgerAmountGBP) AS LedgerAmountGBP
,SUM(Records) AS Records
,SLKTeam AS DashboardText
FROM DATATable
GROUP BY LegalEntity
,SLKBU
,SLKDepartment
, SLKTeam
,CASE WHEN @Level < 6 THEN AssignedHandler ELSE '' END
,metricind
,Banding
,BandingOrder




Obviously i would then move onto other columns for 5-1.... but this was a test.

However i am getting back

Column 'DATATable.AssignedHandler' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

Now i read a couple of forum posts which seem to suggest you can do what i have asked.

So my question - can you do this (was the forum poster incorrect) and if so what am i doing wrong.

Else the only other way i can think of is building the SQL query. Dont fancy that either TBH

Any help greatfully received.

Dan
Gazareth
Gazareth
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2859 Visits: 5320
Your CASE statements need to match in the select & group by clauses - you have @Level = 6 in the select, and @Level < 6 in the group by.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search