如何在发票中汇总相同的产品?
我有这样的查询:
declare @usuario as varchar(50), @fecha as datetime
set @usuario ='angel'
set @fecha ='2011/08/07'
select DetalleCompra .id_ali as [Clave],Alimento .nomAli as [Nombre Alimento],
(select sum (cantidad)
from DetalleCompra
inner join Alimento on DetalleCompra .id_ali =Alimento .id_ali
where DetalleCompra .usuario =@usuario and fecha =@fecha and FolioCompra is null
) as Cantidad,
Alimento .precio as [Precio Unitario], ( sum (Cantidad) * precio ) as [Precio Total]
from DetalleCompra , Alimento
where DetalleCompra .id_ali =Alimento.id_ali
and usuario =@usuario and fecha =CONVERT(datetime, @fecha ) and FolioCompra is null
group by DetalleCompra .id_ali ,Alimento .nomAli , Alimento .precio , DetalleCompra .Cantidad
where:
usuario=user
fecha=date
detallecompra=details buy
idali= id food
nomali=food's name
foliocompra=folio purchases
precio=price
cantidad=quantity
precio total=total price
我想获得一个列表,但是如果产品相同,我想对它们进行求和并得到它们的总价。
它们有一个用户名和一个日期,在用户“接受”它们之前它们都是空的。
我想获得其中 idali
出现两次或多次的列表,他们对该行进行求和,并对总价格进行求和。
I have this query:
declare @usuario as varchar(50), @fecha as datetime
set @usuario ='angel'
set @fecha ='2011/08/07'
select DetalleCompra .id_ali as [Clave],Alimento .nomAli as [Nombre Alimento],
(select sum (cantidad)
from DetalleCompra
inner join Alimento on DetalleCompra .id_ali =Alimento .id_ali
where DetalleCompra .usuario =@usuario and fecha =@fecha and FolioCompra is null
) as Cantidad,
Alimento .precio as [Precio Unitario], ( sum (Cantidad) * precio ) as [Precio Total]
from DetalleCompra , Alimento
where DetalleCompra .id_ali =Alimento.id_ali
and usuario =@usuario and fecha =CONVERT(datetime, @fecha ) and FolioCompra is null
group by DetalleCompra .id_ali ,Alimento .nomAli , Alimento .precio , DetalleCompra .Cantidad
where:
usuario=user
fecha=date
detallecompra=details buy
idali= id food
nomali=food's name
foliocompra=folio purchases
precio=price
cantidad=quantity
precio total=total price
I want to get a list, but if the products are the same, I want to sum them and get the total price of them.
They have a username and a date, and they are null until the user "accepts" them.
I want to get the list where the idali
appears twice or more they sum that row, and sum the total price too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您正在寻找这样的东西。
该查询按 id (
id_ali
)、名称 (nomAli
) 和价格 (precio
) 列对数据进行分组。然后,它对数量 (cantidad) 进行求和,以创建“总数量”列(我只是用英语编写的)。此外,还使用数量和价格列来制定Precio Total
列。如果 fecha 列仅包含日期值而没有任何时间戳,则可以执行简单的等于比较,而不是使用 DATEDIFF。
I believe that you are looking for something like this.
The query groups the data by id (
id_ali
), name (nomAli
) and price (precio
) columns. It then sums up the quantity (cantidad) to create theTotal Quantity
column (I just wrote it in English). Also, formulates thePrecio Total
column using the quantity and price columns.If the column fecha contains only date values without any timestamps, you can do simple equal to comparison instead of using DATEDIFF.