0

这是我的查询:

  SELECT 
    [ASACCT].ACCT_MO_I as 'Types'
    ,sum(CASE when [TECH_V].[CLOS_T]='N4'  THEN 1 ELSE 0 END) AS 'N4'
    ,SUM(CASE when [TECH_V].[CLOS_T]='N3'  THEN 1 ELSE 0 END) AS 'N3'
    ,SUM(CASE when [TECH_V].[CLOS_T]='N2'  THEN 1 ELSE 0 END) AS 'N2'
  FROM [supt_oper_anls_dw].[dbo].[TECH_V] as [TECH_V]
   LEFT OUTER JOIN [supt_oper_anls_dw].[dbo].ACCT_DATE_DIM AS [ASACCT]
     ON CONVERT(varchar(10), [ASACCT].GREG_D, 101)
      = CONVERT(varchar(10), [TECH_V].[OPEN_TS], 101)
  WHERE [TECH_INCDT_V].[KGRP_I] ='73fd71ecf84f5080217683869fd819c3'
    and (
          ([ASACCT].ACCT_MO_I > (datepart(MONTH,getdate()))-1-6
       and [ASACCT].ACCT_MO_I <=(datepart(MONTH,getdate()))-1
       and [ASACCT].ACCT_YR_I = (datepart(year,getdate()))
          )
       OR ([ASACCT].ACCT_MO_I > (datepart(MONTH,getdate()))-6-1+12
       and [ASACCT].ACCT_YR_I = (datepart(year,getdate()))-1
          )
        )
    and [TECH_V].Notes like '%MFTFD%'
    and [TECH_V].notes like '%DEV%'
  GROUP BY [ASACCT].ACCT_MO_I,  [ASACCT].ACCT_YR_I

不显示零行。在某些情况下,例如在我的 TECH_V 表中,我提供的 where 子句不可能没有数据。但是我希望输出显示为零..而是省略了整行。

我想要像下面这样的输出

1  1  2  3 
2  4  5  6
3  0  0  0
4  2  5  4
5  2  5  6
6  0  0  0

但在我的实际输出中,第 3 和第 6 被淘汰了。

代码逻辑是正确的..因为它适用于其他 where 子句。

4

1 回答 1

0

您必须交换您的餐桌顺序:

SELECT 
...
FROM [supt_oper_anls_dw].[dbo].ACCT_DATE_DIM AS [ASACCT] 
LEFT OUTER JOIN [supt_oper_anls_dw].[dbo].[TECH_V] as [TECH_V]
...
于 2013-03-17T19:41:36.690 回答