2016-09-09 58 views
2


我需要一些幫助,使SQL查詢。
我有表'stock_exchange',它記錄在四個不同的商店/網站之間互換的股票。
它記錄需要幫助創建mysql查詢多個選擇的具體條件

日期,ITEM_ID,數量,價格,TOTAL_PRICE,from_site,to_site

現在我需要創建一個查詢,爲商店/網站將返回總價的一個運行爲其他三家店之間發送和接收的股票。

例如:有商店1,商店,Store3,Store4
如果我需要檢查商店1,它應該返回:

商店Name____Total Sent____Total收到
商店2 _________ 90.90 _______ 50
Store3_________120_______100。 40
Store4_________400_______200

我嘗試過自己,但只能獲得發送或接收的股票的總和。

這裏是fiddle

編輯:
我改變了表中記錄存儲ID(1,2,3,4 ......),而不是現在的店別名(CRW,CHC,cal_rav ....),並且我接受的代碼(在答案中)已停止工作。我試圖在代碼這項修正案找到店鋪標識匹配的行,但簡化版,返回任何東西:

SELECT GREATEST(t2.name, t3.name) AS from_store, 
     LEAST(t2.name, t3.name) AS to_store, 
     SUM(CASE WHEN t2.name < t3.name THEN t1.total_price ELSE 0 END) AS total_sent, 
     SUM(CASE WHEN t2.name > t3.name THEN t1.total_price ELSE 0 END) AS total_received 
FROM stock_exchange t1 
INNER JOIN sites t2 
    ON t1.from_site = t2.id 
INNER JOIN sites t3 
    ON t1.to_site = t3.id 
GROUP BY GREATEST(t2.name, t3.name), 
     LEAST(t2.name, t3.name) 
HAVING MAX(GREATEST(t2.id, t3.id)) = '1' 

這裏是updated fiddle

+0

這背後的邏輯,在你的提琴療法是沒有價值Shop1,SHOP2,... – Jens

+0

沒有shop1,SHOP2 ......,我已經提到商店1,商店....到舉例來說,可以在小提琴中找到實際的商店名稱 – sohal07

+0

如果您在此處顯示的結果取決於小提琴中的小提琴 – Jens

回答

2

下面是一個查詢,顯示的發送和接收的數量從咖啡館紫檀所有其他商店:

SELECT GREATEST(t2.name, t3.name) AS from_store, 
     LEAST(t2.name, t3.name) AS to_store, 
     SUM(CASE WHEN t2.name < t3.name THEN t1.total_price ELSE 0 END) AS total_sent, 
     SUM(CASE WHEN t2.name > t3.name THEN t1.total_price ELSE 0 END) AS total_received 
FROM stock_exchange t1 
INNER JOIN sites t2 
    ON t1.from_site = t2.alias 
INNER JOIN sites t3 
    ON t1.to_site = t3.alias 
GROUP BY GREATEST(t2.name, t3.name), 
     LEAST(t2.name, t3.name) 
HAVING MAX(GREATEST(t2.name, t3.name)) = 'Cafe Rosewood' 

如果你想查看全部店鋪之間的交易,只需刪除HAVING條款。該查詢採用了將GREATEST(from_site, to_site)組合在一起的兩個商店的技巧。

​​

+0

謝謝蒂姆,你能不能請加入表'sites'to得到商店的實際名稱,而不是他們的別名 – sohal07

+0

@ sohal07完成...更新。 –

+0

嗨,蒂姆,ive只是更新從varchar存儲別名爲int,不能得到基於現在的網站id基於商店名稱之前的結果(Cafe Rosewood)..... http://sqlfiddle.com/#! 9/e85eb5/2 – sohal07

3

在我看來,雖然有可能有多個店鋪的更廣泛的組合,我更喜歡使用,而更多的查詢是更簡單的(而且通常更快執行):

SELECT name, SUM(total_price) as total_sent_to FROM stock_exchange se 
LEFT JOIN sites ON sites.alias = se.to_site 
WHERE from_site = 'crw' GROUP BY from_site, to_site; 
SELECT name, SUM(total_price) as total_received_from FROM stock_exchange se 
LEFT JOIN sites ON sites.alias = se.from_site 
WHERE to_site = 'crw' GROUP BY from_site, to_site; 

結果:

enter image description here

...所以我會測試它針對大數據量,並在生產中使用比較之前的解決方案。

計數總計:

SELECT *, (SELECT SUM(total_price) 
        FROM stock_exchange se 
        WHERE se.from_site = sites.alias 
        GROUP BY se.from_site) 
       as total_sent_sum, 
       (SELECT SUM(total_price) 
        FROM stock_exchange se 
        WHERE se.to_site = sites.alias 
        GROUP BY se.to_site) 
       as total_received_sum 
       FROM sites; 

enter image description here

+0

thankyou @pedrouan,你是一個明星 – sohal07

+0

歡迎:) – pedrouan

+0

由於這是返回總髮送和總收到每個商店,你可以請提供一個查詢來獲得:多少咖啡廳玫瑰木發送和收到每個其他商店(相對)??? – sohal07