【问题标题】:Inception situation with oracle (sub query stuffs)oracle 的初始情况(子查询的东西)
【发布时间】:2013-05-19 15:50:31
【问题描述】:

对于甲骨文, 从这些表中,

表 1 CUSTOMER : has cust_fname, cust_lname, cust_id

表 2 SALESORDER : has so_number, so_custid

表 3 ITEM : has item_qty, item_sonum

while CUST_ID = SO_CUSTID and SO_NUMBER = ITEM_SONUM (FK relationship)

我想显示带有全名的客户名称(表示 cust_fname+cust_lname),而此客户(只有一个)是订购数量最多的客户(表示必须与 item_qty 做某事)。

如何为这个任务编写代码?

谢谢

【问题讨论】:

    标签: sql oracle sum subquery


    【解决方案1】:

    试试

    select cust_fname, cust_lname
    from
    (select c.cust_fname, c.cust_lname
    from customer c join salesorder so on so.so_custid = c.cust_id
    join ITEM i on i.item_sonum = so.so_number
    group by c.cust_fname, cust_lname
    order by sum(i.item_qty) desc)
    where rownum = 1
    

    Here is a sqlfiddle demo

    select cust_fname, cust_lname
    from
    (select c.cust_fname, c.cust_lname, rank() over (order by sum(i.item_qty) desc) rnk
    from customer c join salesorder so on so.so_custid = c.cust_id
    join ITEM i on i.item_sonum = so.so_number
    group by c.cust_fname, cust_lname
    )
    where rnk = 1;
    

    【讨论】:

    • 再次感谢 A.B.Cade。你的回答太天才了,即使你没有看到任何数据,你也喜欢坐在我旁边。我现在就把我的宗教身份改成你。谢谢
    • 对不起,A.B.Cade,我在哪里可以添加排名功能。它保持错误。我应该将 rank() 与 over() 函数一起使用吗?
    • @NiickMosqow,这很像你的其他问题......有一个很好的帖子here 我认为你需要阅读......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-20
    • 1970-01-01
    • 2019-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多