2017-06-08 21 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