我有这个查询
select dbo.CLOI_ClientOrderItems.cl_id,
count(dbo.IN_Invoices.MasterOrderId) as Orders,
OverallTotal = (select sum(in_total) from IN_Invoices where datepart(mm,in_date_issued)=2 and datepart(yyyy,in_date_issued)=2014)
from IN_Invoices
inner join CLOI_ClientOrderItems on IN_Invoices.MasterOrderId=CLOI_ClientOrderItems.MasterOrderId
where datepart(mm,in_date_issued)=2 and datepart(yyyy,in_date_issued)=2014
group by cl_id
结果
cl_id orders overalltotoal
100000pro 5 39949
100001pro 64 39949
结果显示两个订单都相同我需要显示特定cl_id订单的总计..
答案 0 :(得分:0)
试试这个
select dbo.CLOI_ClientOrderItems.cl_id,f.MasterOrderId as orders ,f.in_total as total
from(select sum(in_total),count(MasterOrderId) from IN_Invoices where datepart(mm,in_date_issued)=2 and datepart(yyyy,in_date_issued)=2014 group by cl_id)as f,IN_Invoices
inner join CLOI_ClientOrderItems on IN_Invoices.MasterOrderId=CLOI_ClientOrderItems.MasterOrderId
where datepart(mm,in_date_issued)=2 and datepart(yyyy,in_date_issued)=2014
group by cl_id
答案 1 :(得分:0)
如果没有架构来测试它,很难看出是否有效,但是看到你已经在OverallTotal的计算中添加了WHERE子句,这可能会对你有所帮助:
select
dbo.CLOI_ClientOrderItems.cl_id,
count(dbo.IN_Invoices.MasterOrderId) as Orders,
sum(in_total) AS OverallTotal
from IN_Invoices
inner join CLOI_ClientOrderItems on
IN_Invoices.MasterOrderId=CLOI_ClientOrderItems.MasterOrderId
where datepart(mm,in_date_issued)=2 and datepart(yyyy,in_date_issued)=2014
group by cl_id
如果没有,请提供您的架构和一些数据的方式。