SQL中最新日期的总和

时间:2019-07-17 08:10:46

标签: sql sql-server

下面是我的SQL查询

Select [Item No_][Expiration Date],[Manufacturing Date]
,([Lot No_] ),
Sum(Quantity)Quantity
from [Warehouse Entry] 
Group by [Lot No_],[Expiration Date],[Manufacturing Date],[Item No_]

数据像这样

+----------+-----------------+--------------------+--------------+--------------------------+
| Item No_ | Expiration Date | Manufacturing Date |   Lot No_    |         Quantity         |
+----------+-----------------+--------------------+--------------+--------------------------+
| BRN00171 | 2020-05-01      | 2019-05-01         | A08E03 X 5   |   7.00000000000000000000 |
| BRN00171 | 2020-05-01      | 2019-05-01         | BLRDUM42 X 1 |   1.00000000000000000000 |
| BRN00171 | NULL            | NULL               | JULY10       | -69.00000000000000000000 |
| BRN00171 | 2011-04-01      | NULL               | JULY10       |  69.00000000000000000000 |
| BRN00171 | 2020-07-01      | 2019-07-01         | A16F15 X 24  |  24.00000000000000000000 |
| BRN00171 | 2020-01-01      | 2019-01-01         | FFWW         |  29.00000000000000000000 |
+----------+-----------------+--------------------+--------------+--------------------------+

如何修改代码以获取如下数据?

+----------+-----------------+--------------------+--------------+----------+
| Item No_ | Expiration Date | Manufacturing Date |   Lot No_    | Quantity |
+----------+-----------------+--------------------+--------------+----------+
| BRN00171 | 01-05-20        | 01-05-19           | A08E03 X 5   |        7 |
| BRN00171 | 01-05-20        | 01-05-19           | BLRDUM42 X 1 |        1 |
| BRN00171 | 01-07-20        | 01-07-19           | A16F15 X 24  |       24 |
| BRN00171 | 01-01-20        | 01-01-19           | FFWW         |       29 |
+----------+-----------------+--------------------+--------------+----------+

4 个答案:

答案 0 :(得分:1)

我认为,每当按[Item No_][Lot No_]分组时,您都在寻找消除的零数量

Select [Item No_],
       max([Expiration Date]) as [Expiration Date],
       max([Manufacturing Date]) as [Manufacturing Date],
       [Lot No_], 
       Sum([Quantity]) as Quantity 
  from [Warehouse Entry] 
 Group by [Item No_],[Lot No_]
 Having Sum([Quantity]) > 0;

Item No_    Expiration Date Manufacturing Date  Lot No_       Quantity
BRN00019    01-07-20        01-07-19            A19F14 X 252  133
BRN00019    01-07-20        01-07-19            A19F14 X 48   1

Demo

答案 1 :(得分:1)

使用此链接进行检查 https://dbfiddle.uk/?rdbms=sqlserver_2016&fiddle=ae7fe036a72f446cb238f0edcca53e6f

以下示例数据:

with [Warehouse Entry]( [Item No_], [Expiration Date], [Manufacturing Date], [Lot No_], [Quantity] ) as
(
select 'BRN00019','01-07-20','01-07-19','A19F14 X 252',133 union all
select 'BRN00019',null,null,'Jun-11',-12  union all
select 'BRN00019','01-03-12',null,'Jun-11',7  union all
select 'BRN00019',null,null,'Jun-11',-7  union all
select 'BRN00019','01-03-12',null,'Jun-11',12  union all
select 'BRN00019','01-07-20','01-07-19','A19F14 X 48',1 
)  

使用此............................................. .......

Select [Item No_],
       max([Expiration Date]) as [Expiration Date],`enter code here`
       max([Manufacturing Date]) as [Manufacturing Date],
       [Lot No_], 
       Sum([Quantity]) as Quantity 
  from [Warehouse Entry]
 Group by [Item No_],[Lot No_]
 Having Sum([Quantity]) > 0
 Order by [Manufacturing Date] DESC

答案 2 :(得分:0)

尝试

SELECT TOP 1 WITH TIES * FROM MyTable
ORDER BY [Expiration Date] DESC

答案 3 :(得分:0)

请检查以下答案是否删除第三和第四列。

with [Warehouse Entry]( [Item No_], [Expiration Date], [Manufacturing Date], 
[Lot No_], [Quantity] ) as
(
select 'BRN00019','2020-05-01','2019-05-01','A19F14 X 252',133 union all
select 'BRN00019','2020-05-01','2019-05-01','Jun-11',-12  union all
select 'BRN00019',null,null,'Jun-11',7  union all
select 'BRN00019','2011-04-01',null,'Jun-11',-7  union all
select 'BRN00019','2020-07-01','2019-07-01','Jun-11',12  union all
select 'BRN00019','2020-07-01','2019-07-01','A19F14 X 48',1 
) 
select [Item No_], [Expiration Date], [Manufacturing Date], [Lot No_], 
[Quantity]  from [Warehouse Entry] where [Expiration Date] in 
(select [Expiration Date] from [Warehouse Entry] where [Expiration Date] is 
not null )
and [Manufacturing Date] in (select [Manufacturing Date] from [Warehouse 
Entry] where [Manufacturing Date] is not null )