甲骨文显示购买汽车最多的客户
Oracle display customer who purchased most cars
我目前正在尝试回答以下问题:
显示从 Archie's Luxury Motors 购买汽车最多的客户的姓名。
我正在使用的表格:
客户
(custID, name, DOB, streetAddress, suburb, postcode,
gender, phoneNo, email, type)
销售交易
(VIN, custID, agentID, dateOfSale, agreedPrice)
我试过以下查询:
select customer.name
from customer, salestransaction
where customer.custid = salestransaction.custid
group by (salestransaction.custid)
having count(salestransaction.custid) = max(salestransaction.custid);
我收到以下错误:
ORA-00979: not a GROUP BY expression
请告诉我哪里做错了。
使用联接而不是在 where 子句中加入它们,同时将
SELECT c.name, MAX(s.custid) FROM (
SELECT c.name, Count(s.custid)
FROM customer c
INNER JOIN salestransaction s ON c.custid = s.custid
GROUP BY c.name);
select * from (
select customer.name, count(*)
from customer, salestransaction
where customer.custid = salestransaction.custid
group by (salestransaction.custid)
order by count(*) desc
) where rownum=1
可能这应该有效:
select * from (
select customer.name
from customer, salestransaction
where customer.custID = salestransaction.custID
group by (salestransaction.custID), customer.name
order by count(*) desc
) where rownum=1
使用排名的最简单方法:
select customer.name, st.cnt
from customer
join
(
select
custid,
count(*) as cnt,
rank() over (order by count(*) desc) as rnk
from salestransaction
group by custid
) st
on customer.custid = st.custid
where st.rnk = 1;
我目前正在尝试回答以下问题:
显示从 Archie's Luxury Motors 购买汽车最多的客户的姓名。
我正在使用的表格:
客户
(custID, name, DOB, streetAddress, suburb, postcode,
gender, phoneNo, email, type)
销售交易
(VIN, custID, agentID, dateOfSale, agreedPrice)
我试过以下查询:
select customer.name
from customer, salestransaction
where customer.custid = salestransaction.custid
group by (salestransaction.custid)
having count(salestransaction.custid) = max(salestransaction.custid);
我收到以下错误:
ORA-00979: not a GROUP BY expression
请告诉我哪里做错了。
使用联接而不是在 where 子句中加入它们,同时将
SELECT c.name, MAX(s.custid) FROM (
SELECT c.name, Count(s.custid)
FROM customer c
INNER JOIN salestransaction s ON c.custid = s.custid
GROUP BY c.name);
select * from (
select customer.name, count(*)
from customer, salestransaction
where customer.custid = salestransaction.custid
group by (salestransaction.custid)
order by count(*) desc
) where rownum=1
可能这应该有效:
select * from (
select customer.name
from customer, salestransaction
where customer.custID = salestransaction.custID
group by (salestransaction.custID), customer.name
order by count(*) desc
) where rownum=1
使用排名的最简单方法:
select customer.name, st.cnt
from customer
join
(
select
custid,
count(*) as cnt,
rank() over (order by count(*) desc) as rnk
from salestransaction
group by custid
) st
on customer.custid = st.custid
where st.rnk = 1;