2016-08-25 60 views
0

表A是訂單表,每個訂單有1行。表B是每個項目有1行的項目表。它們通過訂單號鏈接。當我在這個O/N上加入他們時,如果訂單上有多個項目,我會得到多行訂單。我按照不同表中的字段進行分組,因此我無法使用該方法。MYSQL按照「記錄」的不同行數加入相關表

加入它們的正確方法是什麼?1 Order行和3 Item行不會變成3 Order和3 Item?

對於基本問題的道歉,我的大腦今天正在崩潰。

我已簡化了我所要求的內容。

CREATE TABLE Orders 
    (`ORDURN` varchar(8), `ORDSOU` varchar(10), `ORDVAL` decimal(9,2)) 
; 

INSERT INTO Orders 
    (`ORDURN`, `ORDSOU`, `ORDVAL`) 
VALUES 
    ('12345112', 'WEB', '28.41'), 
    ('22544548', 'ADVERT', '11.58'), 
    ('44848643', 'TELEPHONE', '41.18') 
; 


CREATE TABLE Items 
    (`ITMSTK` varchar(10), `ITMQTY` varchar (3), `ITMCOG` int(9), `ITMURN` varchar(8), `ITMSOU` varchar(10)) 
; 

INSERT INTO Items 
    (`ITMSTK`, `ITMQTY`, `ITMCOG`, `ITMURN`, `ITMSOU`) 
VALUES 
    ('WN778', '1', '2.00', '12345112', 'WEB'), 
    ('WN776', '1', '1.45', '12345112', 'WEB'), 
    ('WN771', '1', '1.86', '12345112', 'WEB'), 
    ('WN845', '1', '1.45', '22544548', 'ADVERT'), 
    ('WN846', '1', '1.38', '22544548', 'ADVERT'), 
    ('WN845', '1', '20.16', '44848643', 'TELEPHONE') 
; 
CREATE TABLE Sources 
    (`SOUCOD` varchar(10), `SOUDESC` varchar(45)) 
; 
INSERT INTO Sources 
    (`SOUCOD`, `SOUDESC`) 
VALUES 
    ('WEB', 'Web Orders 2016'), 
    ('ADVERT', 'Advert Junes 2016'), 
    ('TELEPHONE', 'Telephone Orders 2016') 
; 

而且我然後運行下面

select 
S.soucod as Sources, 
s.soudesc as Description, 
sum(i.itmcog) as COG, 
count(DISTINCT o.ordurn) as Orders, 
sum(o.ordval) as OrderValue 
from sources s 
join orders o on o.ordsou = s.soucod 
join items i on i.itmsou = s.soucod 
group by s.soucod 

此查詢給我

Sources  Description   COG Orders OrderValue 
ADVERT  Advert Junes 2016  2  1  23.16 
TELEPHONE Telephone Orders 2016 20  1  41.18 
WEB   Web Orders 2016   5  1  85.23 

顯然,訂單價值已經被加入扭曲,因爲它已經算多行

這是所需的結果(商品成本是錯誤的,但我知道這是爲什麼,只是我的方式重複的信號數據,此列是不是在這裏除了展示,有必要在項目表帶上)任何目的:

Sources  Description   COG Orders OrderValue 
ADVERT  Advert Junes 2016  2  1  11.58 
TELEPHONE Telephone Orders 2016 20  1  41.18 
WEB   Web Orders 2016   5  1  28.41 

我希望這可以解釋它。

回答

1

需要按順序連接並計算中間結果。在連接的外層使用max()子查詢結果上的函數。

select Sources, Description, 
     sum(i.itmcog) as COG, 
     max(Orders) as Orders, max(OrderValue) as OrderValue 
    from (
     select s.soucod as Sources, s.soudesc as Description, 
       count(o.ordurn) as Orders, 
       sum(o.ordval) as OrderValue 
      from sources s 
      join orders o on o.ordsou = s.soucod 
     group by s.soucod 
     ) A 
    join items i on i.itmsou = A.Sources 
    group by A.Sources 

如果選擇從依賴表只有一個結果,你可以使用SELECT列表中的子查詢:

select s.SOUCOD as Sources, s.soudesc as Description, 
     (select sum(i.itmcog) 
      from items i 
     where i.itmsou=s.soucod 
     ) as COG, 
     count(o.ordurn) as Orders, 
     sum(o.ordval) as OrderValue 
    from sources s 
    join orders o on o.ordsou = s.soucod 
group by s.soucod 
0
SELECT s.soucod sources 
    , s.soudesc description 
    , SUM(i.itmcog) COG 
    , COUNT(DISTINCT o.ordurn) orders 
    , o.ordval ordervalue 
    FROM sources s 
    JOIN orders o 
    ON o.ordsou = s.soucod 
    JOIN items i 
    ON i.itmsou = s.soucod 
GROUP 
    BY sources 
    , description 
    , ordervalue; 
相關問題