继续我的问题。
我想在where子句上显示agregate函数。但是发生了错误。
我的桌子:
我试过这样的方法(哪种方法是在where子句后显示sales_id的总购买量。
select * from customer where customer_id = ANY (
select customer_id from sales where sales_id = ANY (
select sales_id from sales_detail where sum(total) < (
select sum(total) from sales_detail where sales_id = (
select sales_id from sales where customer_id = (
select customer_id from customer where name = 'steven'
)
)
) group by sales_id
)
)
我试过这样的(购买次数在子查询中)
select * from customer where customer_id = ANY (
select customer_id from sales where sales_id = ANY (
select sales_id from sales_detail where (
select sum(total) from sales_detail where sales_id = (
select sales_id from sales where customer_id = (
select customer_id from customer where name = 'steven'
)
)
)
< ANY (
select sum(total) as number_of_purchase, sales_id /*, sales_id*/ from sales_detail group by sales_id
) group by sales_id
)
)
我已经显示了这5个销售ID的购买数量
select sum(total) as number_of_purchase, sales_id /*, sales_id*/
from sales_detail
group by sales_id
,结果就像这样
我想要的是
我想要显示&#39; costumer name&#39;和&#39; costumer id&#39;其中购买次数为&#39;超过购买数量&#39; &#39;史蒂文&#39;
史蒂文的购买数量为3
所以结果应该是这样的
| name | customer_id|
| Clark | c03 |
| Josh | c05 |
因为所有购买总数超过3 您可以从上面的结果中查看
THX。
答案 0 :(得分:2)
select customer_id , name ,sum(total)
from customer ,sales_detail d, Sales s
where d.sales_id = s.sales_id
and customer_id = s.customer_id
having sum(total) > ( select sum(total)
from customer , sales s, sales_detail d
where customer_id = s.customer_id
and d.sales_id = s.sales_id
and name = 'Steven')
答案 1 :(得分:1)
大家好。根据你的建议,我明白了。
我只是重新创建查询并变成这样。 :d
select * from customer where customer_id = ANY (
select customer_id from sales where sales_id = ANY (
select sales_id from sales_detail
group by sales_id
having sum(sales_detail.total) > (
select sum(total) from sales_detail where sales_id = (
select sales_id from sales where customer_id = (
select customer_id from customer where name = 'steven'
)
)
)
)
)