Solved

Conditional Group by

Posted on 2010-09-19
6
795 Views
Last Modified: 2012-05-10
Hello
i have a a table mytable has a data as follows
---------------------------------------------------------------------------------------
ACC_no        | Tr_Date                    | Doc_No             | Doc_TY   line_No     | AMT
-----------------------------------------------------------------------------------------
12272              01-09-2010               12                           01         05              100
12272              05-09-2010               12                           01         06                20
12272              06-09-2010               12                           04         01              150
12272              07-09-2010               12                           02         01                  5
12272              07-09-2010               12                           04         01               110
12272              07-09-2010               12                           02         17                  90

What i need is to group by with a condition "When Doc_Ty =01 >>>> then group Doc_TY, When Doc_ty='04' >>>> then group by TR_DT , otherwise DONT GROUP)
0
Comment
Question by:ali_alannah
6 Comments
 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 33711991
hi..

apart from grouping column, other columns are going 2 aggregate function?

else all columns will going to come in group by.

you can achieve it by

select columnlist
from table
group by case Doc_Ty when '01' then Doc_TY else when '04' then TR_DT end
0
 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 33712004

sory sory.....

my query is wrong.

how can u do conditional grouping when same result set having both values
0
 
LVL 3

Expert Comment

by:daddotnet
ID: 33712076
You can create a table to receive the result sets of 3 different queries, those being filtered using a WHERE clause for your 3 grouping types, something like:

select
ACC_no,
Tr_Date,
Doc_No,
Doc_TY,
min(line_No),
sum(AMT)
into #t_mytable
from mytable
where Doc_Ty='01'
group by
ACC_no,
Tr_Date,
Doc_No,
Doc_TY

insert into #t_mytable
select
ACC_no,
Tr_Date,
min(Doc_No),
min(Doc_TY),
min(line_No),
sum(AMT)
from mytable
where Doc_ty='04'
group by
ACC_no,
Tr_Date

insert into #t_mytable
select
ACC_no,
Tr_Date,
Doc_No,
Doc_TY,
line_No,
AMT
from mytable
where Doc_ty not in ('01','04')

select * from #t_mytable

drop table #t_mytable

Good luck,
DdN
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Expert Comment

by:fahadalihashmi
ID: 33712144
i think this would help this is a solution of a problem if bc_smtrtype is K1 AND A ACCNO NUMBER HAS OCCURED 1 THEN ONLY IT SHOULD BE COUNTED.

 Select *

  From (Select Bc_Sconn_Id,

               Bc_Saccno,

               Count(*) All_Mtr,

               Count(Case

                       When bc_smtrtype = 'K1' Then

                        1

                     End) K1_Mtr

          From Ind.Bs_Bill_Calreading C

         Where Bc_Sconn_Id In (Select Bc_Sconn_Id

                                 From Ind.Bs_Bill_Calreading

                                Where bc_smtrtype = 'K1')

           And bc_izone In (43, 44)

           And bc_strf in

               ('T39', 'T40', 'T41', 'T42', 'T43', 'T44', 'T66', 'T67')

         Group By Bc_Sconn_Id, Bc_Saccno)

Where All_Mtr = 1 And K1_Mtr = 1
0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 500 total points
ID: 33712560
you basically write the 3 separate queries for each situation and use UNION (ALL) to join them into one result set.select ACC_no ,Tr_Date ,Doc_No , Doc_TY ,  line_No , AMT from mytable  where Doc_ty not in (''01' .'04')Union  select ACC_no ,Max(Tr_Date) ,max(Doc_No) , '01' , max( line_No) ,sum( AMT) from mytable  where Doc_ty = ''01'  group by Acc_noUnion  select ACC_no ,Tr_Date ,max(Doc_No) , '04' , max( line_No) ,sum( AMT) from mytable  where Doc_ty = ''04'  group by Acc_no,tr_dateOrder by Acc_no,tr_date
0
 

Author Closing Comment

by:ali_alannah
ID: 33714401
Perfect
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now