SQL Server查询中的聚合和

SQL Server查询中的聚合和,sql,sql-server-2008,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)

我有以下疑问:

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
而不是

Customer1   500.00   120.00
有什么建议吗

尝试以下选择语法:

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


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

为什么不尝试使用派生表呢。例如,您将选择CustomerID、amount,然后在新的select语句中进行聚合。另外,在select的末尾进行分组,而不是将其写入派生表或cte中。 目前,您将为每个客户获得2个结果,因为您同时按customerID和departmentID进行分组,这意味着每个组合都是由查询执行的,即它就像交叉连接,这就是为什么您两次获得相同的customerID。一旦您有了部门ID为'B00H'的customerID,然后又有了dedpartmentid为'A00G'的相同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

使用子查询?将所有内容包装在SELECT*FROM中。。。GROUP BY customer只需确保将*替换为所有列名,并对所有应添加在一起的字段求和即可。这是在将inventorydepartmentid添加到GROUP BY之后。
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
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