简体   繁体   English

SQL查询求和唯一记录

[英]SQL query sum unique records

I'm trying to sum all sales of one period of time of a selling vehicle. 我试图将某辆车在一段时间内的所有销售额进行汇总。 The problem is that every product sold is one row whit amount and price and a total of the bill and the bill number. 问题在于,售出的每种产品都是一排白色的数量和价格,以及帐单和帐单号的总数。

So I have 2 options: multiply ever sold product amount whit the price and sum that. 因此,我有2个选择:乘以已售出的产品数量和价格并将其相加。 Or take the bill remove double rows and sum that. 或拿这笔账单去掉两行并求和。 I chosen for the second option. 我选择了第二个选项。

So now I have a [Location Code] (selling vehicle), [Bill No] and a [Total Price]. 因此,现在我有一个[位置代码](销售车),[账单号]和一个[总价]。

So I get: 所以我得到:

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

As you see double entries, because on one bill there are more than one item. 如您所见,两次输入是因为一张账单上有多个项目。 So now I just want to sum the unique price so that I get 所以现在我只想对唯一价格求和,以便得到

0001 1822,50 0001 1822,50

At this moment I'm only as far as this: 目前,我只涉及到这一点:

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

I tried several but none is working. 我尝试了几次,但都没有用。 Best result gives this, but not summed 最好的结果给出了这个,但不总结

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_]

I think you are looking for this: 我认为您正在寻找:

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