Click here to Skip to main content
15,881,803 members
Articles / Database Development / SQL Server / SQL Server 2008
Tip/Trick

Grouping dynamically on different columns in single query

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
4 Jan 2013CPOL 11.2K   4   4
Grouping dynamically on different columns in single query

Introduction

Dynamic grouping is a big problem when you want to show grouping on different columns in the same query so either you need to write different queries or do some kind of dynamic query; instead of that, the solution I describe below will help to dynamically manipulate results.

Using the code

Grouping on different columns of a table incrementally by its hierarchy but conditionally, i.e.,

Grouping on organization hierarchy
Enterprise, Group, Company, RO, Branch

OR

Grouping on Employee hierarchy by Role
MD, CxO, GH, DH, Worker

OR

Grouping based on Time hierarchy
Hour, Minute, Sec, milisec

The SQL:

SQL
declare @Groupby int
set @Groupby = 2 

-- 1 shall do group by miliSec

-- 2 shall do group by Secs

-- 3 shall do group by Minute



Select 

case when @Groupby <= 4 then Hour else 'All' end as Hour,

case when @Groupby <= 3 then Minute else 'All' end as Minute,

case when @Groupby <= 2 then Secs else 'All' end as Secs,

case when @Groupby <= 1 then milisec else 'All' end as milisec

Sum(Cases) as NoOfCases

From TimeDim

GroupBy Hour, Minute, Secs, milisec 

Points of Interest

Use this in code at places where on user selection we wanted to show summary reports.

History 

First version released.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India
I work as CTO for Orwell IT Solutions Pvt Ltd.
I like to read about new technologies and experiment, I am always watchful and also mentor newbies joined in organization. I have blog on wordpress.

http://rahulajoshi.wordpress.com

Comments and Discussions

 
SuggestionIdea : use cube notion and the .QueryByCube function Pin
NLips18-Jan-14 6:48
NLips18-Jan-14 6:48 
GeneralRe: Idea : use cube notion and the .QueryByCube function Pin
RahulAJoshi27-Jan-14 17:31
RahulAJoshi27-Jan-14 17:31 
QuestionCan you provide any example? I understood NOTHING. Pin
Dmitry Barovik10-Jan-13 19:50
Dmitry Barovik10-Jan-13 19:50 
Step 1: Create Table TimeDimExample...

Step 2: Fill Table with some sample data...
INSERT INTO TimeDimExample ()
SELECT ...
UNION ALL
SELECT ...

Step 3: Provide example queries:
Query grouping by Year...
...
Query grouping by Month...
...
Query grouping by Day...
AnswerI guess I am pretty clear on what I am suggesting. Pin
RahulAJoshi11-Jan-13 3:02
RahulAJoshi11-Jan-13 3:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.