按代码组合行

时间:2017-06-08 13:40:25

标签: sql-server

数据集:

  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

这看起来很简单,但出于某种原因,当我在SQL Server中的两个表之间通过ItemCode进行内部连接时,正在组合Canceled和Returned表之间的单位,并且返回/取消之间的计数被交叉污染。

我觉得我错过了很简单的事情。

任何帮助将不胜感激。

我正在使用的实际查询在这里。我试图尽可能地将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

2 个答案:

答案 0 :(得分: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 |
+------------+--------+-------+--------+------------+-----------+

答案 1 :(得分: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