查询省略零行

时间:2013-03-17 19:26:05

标签: mysql sql

这是我的问题:

  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子句。

1 个答案:

答案 0 :(得分: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]
...