Sql server 按代码合并行

Sql server 按代码合并行,sql-server,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,

数据集:

  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在此:

背景:

我有两张桌子。返回表和取消表。我想得到过去一周内给定商品代码的总单位数/成本/价格的总和。例如,从sqlfiddle,ORANGES123,我希望我的查询返回:

ItemCode  , TotalReturnedUnits, TotalReturnedCost, TotalReturnedPrice,TotalCancelledUnits, TotalCancelledCost, TotalCancelledPrice 
ORANGES123,                 20,               400,               1000,                  6,                200,                 400
这看起来很简单,但出于某种原因,当我在SQL Server中的两个表之间通过ItemCode进行内部联接时,已取消表和返回表之间的单位被合并,计数在已返回/已取消表之间被交叉污染

我觉得我错过了一些非常简单的事情

任何帮助都将不胜感激

我正在处理的实际查询在这里。我尝试尽可能接近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

从这个开始-我想你可以解决剩下的问题

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 |
+------------+--------+-------+--------+------------+-----------+

您可以使用联接查询来实现这一点,取消也可以遵循相同的结构

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

我们能看看你的select语句吗?你能包括你的查询吗?如果是我,我只有一个表,而不是两个表。由于使用它们的现有进程,当前表设计无法更改。所以请使用UNION。谢谢,这让我走得够远了。