Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL multiple group by 1

Status
Not open for further replies.

Willi

MIS
May 5, 2001
1
DE
how can a multiple group by query be efficiently arranged?

the query

select A1,Z,count(*) from D group by A1,Z union
select A2,Z,count(*) from D group by A2,Z union
...
select An,Z,count(*) from D group by An,Z;

at least in Oracle is not optimized. One pass over D should be sufficient to compute the counts.
 
The only reason for the group is the count. Can you get the count from a virtual table or subselect? (Your example is obviously simplified, so i have trouble imagining what you are trying to do in this fashion) Malcolm
wynden@telus.net
November is "be kind to dogs and programmers" month. Or is that "dogs or programmers"?
 
Try this:

SELECT A, Z, count(*)
FROM
(select A1 A,Z from D
union
select A2 A,Z from D
union
...
select An A,Z from D)
GROUP BY A,Z;

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top