2017-04-04 51 views
2

我有一個包含客戶ID,位置ID及其訂單值的表格。我需要選擇每一位客戶提供最大的地方ID花

Customer | Location | Order $ 
1   | 1A   | 100 
1   | 1A   | 20 
1   | 1B   | 100 
2   | 2A   | 50 
2   | 2B   | 20 
2   | 2B   | 50 

所以我會得到

Customer | Location | Order $ 
1   | 1A   | 120 
2   | 2B   | 70 

我想是這樣的:

SELECT 
    a.CUST 
    ,a.LOC 
    ,c.BOOKINGS 
FROM (SELECT DISTINCT TOP 1 b.CUST, b.LOC, sum(b.ORDER_VAL) as BOOKINGS 
    FROM ORDER_TABLE b 
    GROUP BY b.CUST, b.LOC 
    ORDER BY BOOKINGS DESC) as c 
INNER JOIN ORDER_TABLE a 
    ON a.CUST = c.CUST 

但是,這只是返回頂級訂單。

+0

您正在使用哪個數據庫管理系統與Q_2相匹配? –

+0

爲什麼你刪除'MySQL'標籤?你想如何處理來自同一客戶的位置之間的關係,這是一個問題嗎? –

回答

0

只需使用變量來模擬ROW_NUM()

DEMO

SELECT * 
FROM (SELECT `Customer`, `Location`, SUM(`Order`) as `Order`, 
       @rn := IF(@customer = `Customer`, 
         @rn + 1, 
         IF(@customer := `Customer`, 1, 1) 
         ) as rn 
     FROM Table1 
     CROSS JOIN (SELECT @rn := 0, @customer := '') as par 
     GROUP BY `Customer`, `Location` 
     ORDER BY `Customer`, SUM(`Order`) DESC 
    ) t 
WHERE t.rn = 1 
+0

這樣做。由Customer對行進行了編號,並使用了ROW = 1的所有行。謝謝! – noxdogcloud

0

杉杉你要總結的價值觀爲每個位置:

select Customer, Location, Sum(Order) as tot_order 
from order_table 
group by Customer, Location 

那麼你可以用MAX最大的訂單,以及頂部的位置,GROUP_CONCAT的組合,將返回所有地點,由總排序desc,並且SUBSTRING_INDEX爲了只讓高層之一:

select 
    Customer, 
    substring_index(
    group_concat(Location order by tot_order desc), 
    ',', 1 
) as location, 
    Max(tot_order) as max_order 
from (
    select Customer, Location, Sum(Order) as tot_order 
    from order_table 
    group by Customer, Location 
) s 
group by Customer 

(如果有一條領帶,以相同的順序頂部的兩個位置,這個查詢將返回只有一個)

0

如何:

select a.* 
from (
    select customer, location, SUM(val) as s 
    from orders 
    group by customer, location 
    ) as a 
left join 
    (
    select customer, MAX(b.tot) as t 
    from ( 
     select customer, location, SUM(val) as tot 
     from orders 
     group by customer, location 
     ) as b 
     group by customer 
    ) as c 
on a.customer = c.customer where a.s = c.t; 
+0

這隻會返回每個位置的訂單值,但不會給我最有價值的位置。 – noxdogcloud

+0

看編輯,沒有完全掌握你的問題 – rlbaker

0

這是我怎麼會處理它(也許不是最好的方法是什麼?) - 我寫的它首先使用CTE,只是看到MySQL不支持CTE,然後切換到兩次寫同一子查詢:

SELECT B.Customer, C.Location, B.MaxOrderTotal 
FROM 
(
    SELECT A.Customer, MAX(A.OrderTotal) AS MaxOrderTotal 
    FROM 
    (
     SELECT Customer, Location, SUM(`Order`) AS OrderTotal 
     FROM Table1 
     GROUP BY Customer, Location 
    ) AS A 
    GROUP BY A.Customer 
) AS B INNER JOIN 
(
    SELECT Customer, Location, SUM(`Order`) AS OrderTotal 
    FROM Table1 
    GROUP BY Customer, Location 
) AS C ON B.Customer = C.Customer AND B.MaxOrderTotal = C.OrderTotal; 

編輯:用於提供

該解決方案將在領帶的情況下提供多個行的表結構。 SQL fiddle for this solution

0

這看起來像一個通過使用聚合函數問題的順序。這是我的刺傷;

SELECT 

    c.customer, 
    c.location, 

    SUM(`order`) as `order_total`, 
    (
    SELECT 

    SUM(`order`) as `order_total` 

    FROM customer cm 


    WHERE cm.customer = c.customer 
    GROUP BY location 
    ORDER BY `order_total` DESC LIMIT 1 

) as max_order_amount 


FROM customer c 
GROUP BY location 
HAVING max_order_amount = order_total 

這裏是SQL小提琴。 http://sqlfiddle.com/#!9/2ac0d1/1

0
with 
Q_1 as 
(
select customer,location, sum(order_$) as order_sum 
from cust_order 
group by customer,location 
order by customer, order_sum desc 
), 

Q_2 as 
(
    select customer,max(order_sum) as order_max 
    from Q_1 
    group by customer 
), 

Q_3 as 
(
    select Q_1.customer,Q_1.location,Q_1.order_sum 
    from Q_1 inner join Q_2 on Q_1.customer = Q_2.customer and Q_1.order_sum = Q_2.order_max 
) 

select * from Q_3 

Q_1 - 選擇普通集料,Q_2 - 選擇最大(總)出Q_1和Q_3的選擇從Q_1客戶,地點和(訂單)

+0

有關此代碼的一些解釋只回答將會很有用。 – Pyves