繁体   English   中英

SQL查询求和唯一记录

[英]SQL query sum unique records

我试图将某辆车在一段时间内的所有销售额进行汇总。 问题在于,售出的每种产品都是一排白色的数量和价格,以及帐单和帐单号的总数。

因此,我有2个选择:乘以已售出的产品数量和价格并将其相加。 或拿这笔账单去掉两行并求和。 我选择了第二个选项。

因此,现在我有一个[位置代码](销售车),[账单号]和一个[总价]。

所以我得到:

0001    0001/00277343   10,26000000000000000000
0001    0001/00277343   10,26000000000000000000
0001    0001/00277343   10,26000000000000000000
0001    0001/00277343   10,26000000000000000000
0001    0001/00277345   10,33000000000000000000
0001    0001/00277345   10,33000000000000000000
0001    0001/00277345   10,33000000000000000000
0001    0001/00277347   24,35000000000000000000
0001    0001/00277348   30,31000000000000000000
0001    0001/00277348   30,31000000000000000000
0001    0001/00277349   2,69000000000000000000

如您所见,两次输入是因为一张账单上有多个项目。 所以现在我只想对唯一价格求和,以便得到

0001 1822,50

目前,我只涉及到这一点:

select [Location Code], [Bill No_] , Price from [Item Ledger Entry] 
where [Location Code] = '0001' and [Document Date] = '01.04.2015'

我尝试了几次,但都没有用。 最好的结果给出了这个,但不总结

select distinct[Bill No_], [Location Code] , Price from [Item Ledger Entry] 
where [Location Code] = '0001' and [Document Date] = '01.04.2015'
select [Location Code], [Bill No_] , SUM(Price) from [Item Ledger Entry] 
where [Location Code] = '0001' and [Document Date] = '01.04.2015'
group by [Location Code], [Bill No_]

我认为您正在寻找:

SELECT [Location Code], [Bill No_], SUM(Price) AS Price
FROM (SELECT  DISTINCT [Location Code], [Bill No_] , Price from [Item Ledger Entry] 
      WHERE [Location Code] = '0001' and [Document Date] = '01.04.2015') t
GROUP BY [Location Code], [Bill No_]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM