| Author |
Topic |
|
nic
Posting Yak Master
209 Posts |
Posted - 2007-06-15 : 15:09:45
|
Hi,I have a query request but I'm not sure it is possible via a sql script. Essentially a report that has multiple group breakdowns.Essentially after each unique agency, I need to display a summary for the total premium. If you run the below script you'll see it returns a summary for each user per agency. I then need to have a summary line for each agency (see the "all" lines):AgencyCode UserID QuoteType TotalPremium -------------------- ----------- -------------------- ------------------------ agency1 1 auto 1500agency1 1 home 900agency1 2 auto 1200agency1 all all 3600agency2 3 auto 2500agency2 3 homw 1000agency2 4 home 400agency2 all all 3900 Can this be done via just sql? create table test( userID int ,AgencyCode varchar(20) ,QuoteType varchar(20) ,TotalPremium float)insert into test select 1,'agency1','auto',1000insert into test select 1,'agency1','auto',500insert into test select 1,'agency1','home',700insert into test select 1,'agency1','home',200insert into test select 2,'agency1','auto',1000insert into test select 2,'agency1','auto',200insert into test select 3,'agency2','auto',1000insert into test select 3,'agency2','auto',1500insert into test select 3,'agency2','homw',1000insert into test select 4,'agency2','home',400select AgencyCode ,UserID ,QuoteType ,SUM(TotalPremium) AS TotalPremiumfrom testGroup BY AgencyCode ,UserID ,QuoteTypedrop table test Nic |
|
|
DustinMichaels
Constraint Violating Yak Guru
464 Posts |
Posted - 2007-06-15 : 15:14:56
|
| You might be able to do this with the ROLLUP statement in the group by clause. Look it up in Books on Line. |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-06-15 : 15:48:04
|
will this do?CREATE TABLE test( userID INT ,AgencyCode VARCHAR(20) ,QuoteType VARCHAR(20) ,TotalPremium FLOAT)INSERT INTO test SELECT 1,'agency1','auto',1000INSERT INTO test SELECT 1,'agency1','auto',500INSERT INTO test SELECT 1,'agency1','home',700INSERT INTO test SELECT 1,'agency1','home',200INSERT INTO test SELECT 2,'agency1','auto',1000INSERT INTO test SELECT 2,'agency1','auto',200INSERT INTO test SELECT 3,'agency2','auto',1000INSERT INTO test SELECT 3,'agency2','auto',1500INSERT INTO test SELECT 3,'agency2','homw',1000INSERT INTO test SELECT 4,'agency2','home',400SELECT AgencyCode ,UserID ,QuoteType ,TotalPremiumFROM( SELECT 1 AS ordinal ,AgencyCode ,UserID ,QuoteType ,SUM(TotalPremium) AS TotalPremium FROM test GROUP BY AgencyCode, UserID, QuoteType UNION ALL SELECT 2 AS ordinal , AgencyCode , -1 -- convert this to ALL in your front end , 'all' , SUM(TotalPremium) AS TotalPremium FROM test GROUP BY AgencyCode) t1 ORDER BY AgencyCode, ordinalDROP TABLE test _______________________________________________Causing trouble since 1980blog: http://weblogs.sqlteam.com/mladenp |
 |
|
|
nic
Posting Yak Master
209 Posts |
Posted - 2007-06-15 : 16:14:50
|
| Wow, ROLLUP Rocks! Never knew about that one.Nic |
 |
|
|
|
|
|