Author |
Topic |
Champinco
Yak Posting Veteran
54 Posts |
Posted - 2006-11-14 : 19:27:22
|
Could i please get help with the following group by/aggregate:LOAD_DATE SOURCE_SYSTEM Records Records_processed08-Nov-06 System A 7076 700009-Nov-06 System A 8937 890030-Oct-06 System B 4054 400030-Oct-06 System B 3 130-Nov-05 System A 2 130-Dec-05 System C 2 1What I would like is an aggregate/group by of first the cource_system, and sum of records, and records_processed,for each month (group by month). I am not sure how to do this as the code that i have tried only aggregates it either by month or source system by not both. The result i wish is something like this (that is an aggregate of system by each month, summing the records, and records processed):LOAD_DATE SOURCE_SYSTEM Records Records_processedNov-06 System A 16103 15900Nov-05 System A 2 1Oct-06 System B 4057 4001Dec-06 System C 2 1Cheers hope that makes sense. |
|
Champinco
Yak Posting Veteran
54 Posts |
Posted - 2006-11-14 : 20:05:12
|
Dont worry guys...i figured it out solo. CheersGK |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-11-14 : 21:11:03
|
Care to post your solution ? KH |
|
|
Champinco
Yak Posting Veteran
54 Posts |
Posted - 2006-11-15 : 00:38:41
|
Sure...here is the original code.it wasnt the query itself but the formatting of the original date format. the group by wasnt grouping it by properly:select right(convert(varchar,load_date,103),7), source_system, sum(total_number_of_records_in_file) as Records_File, sum(total_number_of_records_processed) as Records_Processed, sum(total_number_of_records_loaded) as Records_Loaded, sum(total_number_of_records_not_loaded) as Records_Not_Loadedfrom load_reportgroup by right(convert(varchar,load_date,103),7), source_systemextract of end result: Sorry about the formattingRecord_Date*source_system*Records_File*Records_Processed* Records_Loaded*Records_Not_Loaded10/2006 A 26042 26042 3559 2248311/2006 A 58286 58286 6222 5206510/2006 B 6765 6765 6765 011/2006 B 18976 18976 18976 011/2006 C 52291 52291 51472 81910/2006 D 5019087 5019087 11952 5007135 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-15 : 00:42:32
|
must have been something like select datepart(year, load_date), datepart(month, load_date), source_system,sum(records), sum(records_processed)from sometablegroup by datepart(year, load_date), datepart(month, load_date), source_systemPeter LarssonHelsingborg, Sweden |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-11-15 : 00:52:39
|
Champinco,you should use Peter's method as it is more efficient. Furthermore, oonverting the date to string using style 103 will not allow you to sort it by year, month.you can also use dateadd(month, datediff(month, 0, load_date), 0) to "remove" the day select dateadd(month, datediff(month, 0, load_date), 0), source_system, sum(total_number_of_records_in_file) as Records_File, sum(total_number_of_records_processed) as Records_Processed, sum(total_number_of_records_loaded) as Records_Loaded, sum(total_number_of_records_not_loaded) as Records_Not_Loadedfrom load_reportgroup by dateadd(month, datediff(month, 0, load_date), 0), source_system KH |
|
|
Champinco
Yak Posting Veteran
54 Posts |
Posted - 2006-11-15 : 00:58:50
|
Thanks Peter, and KH,the last post worked perfectly, as did mine, except yes yours is more efficient.I will be posting up another one soon, that will be nice for you to think about. i cant figure it out in trans sql.CheersGK |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-15 : 01:11:26
|
Well, if you are worried about month name, try thisselect datepart(year, load_date), datename(month, load_date), source_system,sum(records), sum(records_processed)from sometablegroup by datepart(year, load_date), datename(month, load_date), source_systemPeter LarssonHelsingborg, Sweden |
|
|
Champinco
Yak Posting Veteran
54 Posts |
Posted - 2006-11-15 : 01:25:21
|
Hey guys, maybe one of you can help me with this baby, which is linked to my previous posts on group by and aggreggates.This code:SELECT commission_summary.distributor as Distributor, convert(varchar,commission_summary.commission_month,103) as 'Commission Date', '$' + convert(varchar(50), sum(commission_summary.payment_this_period_carried_forward),1) as Totalfrom commission_summarygroup by commission_summary.commission_month, distributororder by commission_summary.commission_month desc, sum(commission_summary.payment_this_period_carried_forward) descreturns this:Dist Commission D TotalMA000 01/08/2006 $602,431.96LY000 01/08/2006 $227,826.73LX000 01/08/2006 $102,945.67LB000 01/08/2006 $101,997.54LW000 01/08/2006 $100,783.15......MA000 01/07/2006 $743,176.50LY000 01/07/2006 $224,787.86LW000 01/07/2006 $168,456.88LX000 01/07/2006 $96,536.36LB000 01/07/2006 $95,393.88...etc...What I require is the top 2 values (in $ amount) per month. That is return the top 2 distributors in terms of amount for each month. Note that i have fooled around with the original code in terms of formatting.Cheers guys, this one is hard for me. I know in oracle you probably could use a partition. I was maybe thinking of an index count (resetting at each month), and then having a Having count > 2.CheersGK |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-15 : 02:00:15
|
Are you using SQL 2000 or SQL 2005?Peter LarssonHelsingborg, Sweden |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-15 : 02:13:20
|
[code]-- prepare test dataset dateformat dmydeclare @test table (dist varchar(5), dt datetime, total money)insert @testselect 'MA000', '01/08/2006', $602431.96 union allselect 'LY000', '01/08/2006', $227826.73 union allselect 'LX000', '01/08/2006', $102945.67 union allselect 'LB000', '01/08/2006', $101997.54 union allselect 'LW000', '01/08/2006', $100783.15 union allselect 'MA000', '01/07/2006', $743176.50 union allselect 'LY000', '01/07/2006', $224787.86 union allselect 'LW000', '01/07/2006', $168456.88 union allselect 'LX000', '01/07/2006', $96536.36 union allselect 'LB000', '01/07/2006', $95393.88-- do the workselect datepart(year, t.dt) [year], datename(month, t.dt) monthname, t.dist, t.totalfrom @test twhere t.total in (select top 2 e.total from @test e where datepart(year, e.dt) = datepart(year, t.dt) and datepart(month, e.dt) = datepart(month, t.dt))order by datepart(year, t.dt), datepart(month, t.dt), t.total desc[/code]Peter LarssonHelsingborg, Sweden |
|
|
|
|
|