0

我有以下查询:

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

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

Customer1     0.00   120.00
Customer1   500.00     0.00

代替

Customer1   500.00   120.00

有什么建议么?

4

2 回答 2

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. 一种聪明的方法(即使在这种情况下不起作用)。我没有意识到这是允许的语法。

于 2013-10-31T20:37:40.313 回答
0

你为什么不尝试使用派生表。例如,您将选择 CustomerID、金额,然后在 New SELECT 语句中进行聚合。此外,在您的选择结束时进行分组,而不是将其写入派生表(或 cte)中。目前,您为每个客户获得 2 个结果,因为您同时按 customerID 和 departmentID 进行分组,这意味着每个组合都由查询执行,即它的作用类似于交叉连接,这就是为什么您两次获得相同的 customerID 的原因。一旦你有一个带有 departmentid='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
于 2013-10-31T21:00:16.327 回答