oracle - SQL: How to display a column not in group by expression? -
i have following code:
select o.ono customers c, odetails od, orders o, parts p p.pno=od.pno , od.ono=o.ono , o.cno=c.cno group o.ono order sum(od.qty*p.price)desc;
however, instead of selecting o.ono
column, need select c.cname
different table. how go doing that?
if want list of customer names ordered sum of orders change query from
select o.ono ... group o.ono
to
select c.cname ... group c.cname
this display customer name , order number. note: repeat customer name many times there orders customer
also, preferred style of sql queries
select c.cname customers c inner join orders o on c.cno = o.cno inner join odetails od on o.ono = od.ono inner join parts p on oid.pno = p.pno p.pno=od.pno , od.ono=o.ono , o.cno=c.cno group c.cname order sum(od.qty*p.price)desc;
an additional suggestion clarify code. (please note: i'm not oracle person), in company (sqlserver) have table named after entity, primary key being id
, , property named without prefix. instance oid
refer orderid
or officeid
?
eg:
customer:
- id
- name
order:
- id
- customerid
then queries more readable:
select name customer inner join order on customer.id = order.customerid ... order customer.name
Comments
Post a Comment