2017-06-08 64 views
0

數據集:結合行通過代碼

CREATE TABLE Returned(
    Code varchar(20) not null, 
    RUnits int not null, 
    RCost int not null, 
    RPrice int not null, 
    RDate date not null); 


    Insert into Returned(Code, Runits, rcost, rprice, rdate) 
    values 
    ('ORANGES123', 10, 200, 500, '2017-04-01'), 
    ('BANANAS123', 15, 350, 900, '2017-04-01'), 
    ('APPLES123', 7, 234, 756, '2017-04-01'), 
    ('ORANGES123', 10, 200, 500, '2017-04-02'), 
    ('BANANAS123', 15, 350, 900, '2017-04-02'), 
    ('APPLES123', 7, 234, 756, '2017-04-02'); 



    CREATE TABLE Cancelled(
    Code varchar(20) not null, 
    CUnits int not null, 
    CCost int not null, 
    CPrice int not null, 
    CDate date not null 
); 

    Insert into Cancelled(Code, Cunits, Ccost, Cprice, Cdate) 
    values 
    ('ORANGES123', 3, 100, 200, '2017-04-01'), 
    ('BANANAS123', 5, 243, 500, '2017-04-01'), 
    ('APPLES123', 10, 235, 537, '2017-04-01'), 
    ('ORANGES123', 3, 100, 200, '2017-04-02'), 
    ('BANANAS123', 5, 243, 500, '2017-04-02'), 
    ('APPLES123', 10, 235, 537, '2017-04-02'); 

Sqlfiddle這裏:

http://sqlfiddle.com/#!9/f10634

背景:

我有2個表。返回表格和取消表格。我希望在過去一週內得到給定物料代碼的總單位數/成本/價格總和。例如,從sqlfiddle,ORANGES123,我想我的查詢返回:

ItemCode , TotalReturnedUnits, TotalReturnedCost, TotalReturnedPrice,TotalCancelledUnits, TotalCancelledCost, TotalCancelledPrice 
ORANGES123,     20,    400,    1000,     6,    200,     400 

這看似簡單,但由於某種原因,當我在做的ItemCode我的兩個表之間在SQL Server內部聯接,單位之間「已取消」和「已退回」表格正在合併,計數在「已退回/已取消」之間受到交叉污染。

我覺得我錯過了很簡單的東西。

任何幫助,將不勝感激。

我正在使用的實際查詢是在這裏。我試圖儘可能接近的sqlfiddle型號:

SELECT sales.Code AS Code, 
     sales.Quantity AS QtyReturned, 
     price.WghtAvgCost * sales.Quantity AS ReturnedCost, 
     price.CurrentPrice * sales.Quantity AS ReturnedPrice, 
     orders.CancelledUnits, 
     orders.CancelledCost, 
     orders.CancelledPrice, 
     price.CurrentPriceType AS PriceType 
FROM salesTable sales 
    INNER JOIN costPriceTable price ON sales.Code= price.Code 
                AND (sales.BusinessDate BETWEEN price.StartDate AND price.EndDate) 
                AND sales.LocationId = price.LocationId 
    INNER JOIN ordersTable orders 
     ON sales.Code= orders.Code 
+0

我們可以看到您的選擇語句嗎? – jimmy8ball

+0

你能否包含你的查詢? –

+0

如果是我,我只有一張桌子,而不是兩張桌子。 – Strawberry

回答

1

開始用這個 - 我想你自己看着辦休息了...

SELECT *, 'returned' source FROM returned 
UNION ALL 
SELECT *, 'cancelled' FROM cancelled; 
+------------+--------+-------+--------+------------+-----------+ 
| Code  | RUnits | RCost | RPrice | RDate  | source | 
+------------+--------+-------+--------+------------+-----------+ 
| ORANGES123 |  10 | 200 | 500 | 2017-04-01 | returned | 
| BANANAS123 |  15 | 350 | 900 | 2017-04-01 | returned | 
| APPLES123 |  7 | 234 | 756 | 2017-04-01 | returned | 
| ORANGES123 |  10 | 200 | 500 | 2017-04-02 | returned | 
| BANANAS123 |  15 | 350 | 900 | 2017-04-02 | returned | 
| APPLES123 |  7 | 234 | 756 | 2017-04-02 | returned | 
| ORANGES123 |  3 | 100 | 200 | 2017-04-01 | cancelled | 
| BANANAS123 |  5 | 243 | 500 | 2017-04-01 | cancelled | 
| APPLES123 |  10 | 235 | 537 | 2017-04-01 | cancelled | 
| ORANGES123 |  3 | 100 | 200 | 2017-04-02 | cancelled | 
| BANANAS123 |  5 | 243 | 500 | 2017-04-02 | cancelled | 
| APPLES123 |  10 | 235 | 537 | 2017-04-02 | cancelled | 
+------------+--------+-------+--------+------------+-----------+ 
+0

謝謝,這讓我足夠了。 – jackfrost5234

0

您可以使用連接查詢來實現這一點,遵循相同的結構來取消您的取消。

JOIN 
(SELECT 
Code, sum(RUnits) as TotalReturnedUnits, sum(RCost) as TotalReturnedCost, 
sum(RPrice) as TotalReturnedPrice 
from Returned 
where 

--parameterised dates can be used 
--RDate between @Start and @End 

group by Code 
)returns 
ON salesTable.Code = returns.Code