sql - Is it possible to group twice without subqueries? -


i answered 1 of questions on following query:

select s.address shop s  join visit v on s.shopid = v.shopid  join customer c on v.customerid = c.customerid  c.cname = 'john'  group  s.address , c.customerid  having count(*) > 1 

this works fine, return duplicate addresses if there 2 or more john's given shop meet query's criteria, modified answer:

select distinct a.address   ( select s.address shop s  join visit v on s.shopid = v.shopid  join customer c on v.customerid = c.customerid  c.cname = 'john'  group  s.address , c.customerid  having count(*) > 1 ) 

now question is: possible achieve grouping first when evaluating aggregate expression , before selecting in single query?

you can directly

select distinct s.address shop s  join visit v on s.shopid = v.shopid  join customer c on v.customerid = c.customerid  c.cname = 'john'  group  s.address , c.customerid  having count(*) > 1 

i think want.

the select last rbdms (well, really, order is). first filtering , grouping, select , ordering.

so use group by, , distinct.

no need show fields you're grouping. can't show fields not grouped, or used in aggregation functions.


Comments