查询SQL Server中的聚合SUM

时间:2013-10-31 20:32:01

标签: sql sql-server-2008

我有以下查询:

select 
     ii.customer
     , CONVERT(DECIMAL(20,2),ISNULL((Select SUM(gld.amount) where inv.inventorydepartmentid='B00H'),0.00)) AS CD
     ,CONVERT(DECIMAL(20,2),ISNULL((Select SUM(gld.amount) where inv.inventorydepartmentid='A00G'),0.00)) AS Cam
from 
    invoiceitemview ii with (nolock)
inner join 
    master m with(nolock) on m.masterid=ii.masterid
inner join 
    warehouse w with (nolock) on w.warehouseid=ii.warehouseid
inner join 
    category c on c.categoryid=m.categoryid
inner join 
    gl on gl.invoiceitemid=ii.invoiceitemid
inner join 
    inventorydepartment inv on inv.inventorydepartmentid = c.inventorydepartmentid
inner join 
    gldetail gld on gld.warehouseid = w.warehouseid and gl.glid = gld.glid 
                 and m.masterid = gld.masterid
where 
    gl.gldate between @StartDate and @EndDate
    and ii.status IN ('CLOSED', 'PROCESSED')
    and w.warehouseid = '01'
    and w.inactive <> 'T'
    and ii.customerno = 'T1'
group by 
    ii.customer

但是,这不会运行,因为我没有在group by子句中包含inv.inventorydepartmentid。但是,如果我这样做,那么它会显示同一客户的两行,如下所示:

Customer1     0.00   120.00
Customer1   500.00     0.00

而不是

Customer1   500.00   120.00

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

试试这个select语法:

select ii.customer,
       CONVERT(DECIMAL(20,2),
               ISNULL(SUM(case when inv.inventorydepartmentid='B00H' then gld.amount end),
                      0.00
                     )
              ) AS CD,
       CONVERT(DECIMAL(20,2),
               ISNULL(SUM(case when inv.inventorydepartmentid='A00G' then gld.amount end),
                      0.00
                     )
              ) AS Cam

您正在进行条件聚合。您不需要嵌套select。您只需要正确使用case语句。

顺便说一句,我从未见过带有where子句但没有from的嵌套子查询。一种聪明的方法(即使它在这种情况下不起作用)。我没有意识到这是允许的语法。

答案 1 :(得分:0)

为什么不尝试使用派生表。例如,您将选择CustomerID,amount,然后在New SELECT语句中执行聚合。另外,在select的末尾进行分组,而不是在派生表(或cte)中写入。 目前,每个客户获得2个结果,因为您按BOTH customerID和departmentID进行分组,这意味着查询正在执行每个组合,即它的作用类似于交叉连接,这就是为什么两次获得相同的customerID的原因。一旦您拥有了带有departmentid =&#39; B00H&#39;然后使用dedpartmentid =&#39; A00G&#39;

使用相同的customerID
with cte as (
select 
ii.customer
,gld.amount

from 
invoiceitemview ii with (nolock)
inner join master m with(nolock) on m.masterid=ii.masterid
inner join warehouse w with (nolock) on w.warehouseid=ii.warehouseid
inner join category c on c.categoryid=m.categoryid
inner join gl on gl.invoiceitemid=ii.invoiceitemid
inner join inventorydepartment inv on inv.inventorydepartmentid = c.inventorydepartmentid
inner join gldetail gld on gld.warehouseid=w.warehouseid and gl.glid=gld.glid and  m.masterid=gld.masterid
where 
gl.gldate between @StartDate and @EndDate
and ii.status IN ('CLOSED', 'PROCESSED')
and w.warehouseid='01'
and w.inactive<>'T'
and ii.customerno='T1'
)

select customer
,(select CONVERT(DECIMAL(20,2),ISNULL((Select SUM(gld.amount) from inventorydepartment inv (SOME KIND OF JOIN IS REQUIRED) where inv.inventorydepartmentid='B00H'),0.00))) AS CD
,(select CONVERT(DECIMAL(20,2),ISNULL((Select SUM(gld.amount) from inventorydepartment inv (SOME KIND OF JOIN IS REQUIRED) where inv.inventorydepartmentid='A00G'),0.00))) AS Cam
  from cte
  group by customer