Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to reduce the number of table access from multiple "select count(*) group by" with union all Expand / Collapse
Author
Message
Posted Friday, February 22, 2013 11:31 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, September 12, 2013 2:28 PM
Points: 12, Visits: 159
I have a select statement as below, which causes a table access for each union all..

My goal is to cut down the number of table access without using union all..

Select A, B, C, '100' as CntType, count(*) as Cnt from SameTable where A between 1 and 100 and B between 1 and 100 and C between 1 and 100 group by A,B,C
Union all
Select A, B, C, '200' as CntType, count(*) as Cnt from SameTable where A between 101 and 200 and B between 101 and 200 and C between 101 and 200 group by A,B,C
Union all
Select A, B, C, '300' as CntType, count(*) as Cnt from SameTable where A between 201 and 300 and B between 201 and 300 and C between 201 and 300 group by A,B,C
Union all
Select A, B, C, 'Over 300' as CntType, count(*) as Cnt from SameTable where A > 300 and B > 300 and C > 300 group by A,B,C

The above Union all will access the SameTable four times, I'd like to have SQL statements to access the SameTable once..

Please HELP!

Happy Friday...
Post #1423185
Posted Friday, February 22, 2013 1:27 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, July 12, 2013 7:21 AM
Points: 2, Visits: 102
This may be way off base but could you add a column to your query and use CASE to populate it based on your A, B & C values? Then you could include that in your GROUP BY.
Post #1423240
Posted Friday, February 22, 2013 1:27 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 5:27 PM
Points: 2,127, Visits: 3,216

Select 
A, B, C,
sum(case when A between 1 and 100 and B between 1 and 100 and C between 1 and 100 then 1 else 0 end) AS '100',
sum(case when A between 101 and 200 and B between 101 and 200 and C between 101 and 200 then 1 else 0 end) AS '200',
sum(case when A between 201 and 300 and B between 201 and 300 and C between 201 and 300 then 1 else 0 end) AS '300',
sum(case when A > 300 and B > 300 and C > 300 then 1 else 0 end) AS 'Over 300'
from SameTable
where
(A between 1 and 100 and B between 1 and 100 and C between 1 and 100) or
(A between 101 and 200 and B between 101 and 200 and C between 101 and 200) or
(A between 201 and 300 and B between 201 and 300 and C between 201 and 300) or
(A > 300 and B > 300 and C > 300)
group by A,B,C



SQL DBA,SQL Server MVP('07, '08, '09)

Carl Sagan said: "There is no such thing as a dumb question." Sagan obviously never watched a congressional hearing!
Post #1423241
Posted Friday, February 22, 2013 2:15 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, September 12, 2013 2:28 PM
Points: 12, Visits: 159
That's great...
Let me test it out...
Post #1423264
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse