遇到MySQL问题

时间:2014-10-16 07:44:52

标签: mysql

我遇到了MySQL问题,这是查询:

SELECT        JAN.Sales_Phase, JAN.January, FEB.Febuary, MAR.March, APR.April, MAY.May, JUN.June, JUL.July, AUG.August, SEP.September, OCT.October, NOV.November, 
                         DEC.December
FROM            (SELECT        Sales_Phase, COALESCE (SUM(PO_Amount_USD), 0) AS January
                          FROM            MasterTable_TM
                          WHERE        (Sales_Phase = 'Credits Card') OR
                                                    (Sales_Phase = 'PO Received') AND (PO_Month = '1')
                          GROUP BY Sales_Phase) AS JAN LEFT OUTER JOIN
                             (SELECT        Sales_Phase, COALESCE (SUM(PO_Amount_USD), 0) AS Febuary
                               FROM            MasterTable_TM AS MasterTable_TM_1
                               WHERE        (Sales_Phase = 'Credits Card') OR
                                                         (Sales_Phase = 'PO Received') AND (PO_Month = '2')
                               GROUP BY Sales_Phase) AS FEB ON FEB.Sales_Phase = JAN.Sales_Phase LEFT OUTER JOIN
                             (SELECT        Sales_Phase, COALESCE (SUM(PO_Amount_USD), 0) AS March
                               FROM            MasterTable_TM AS MasterTable_TM_2
                               WHERE        (Sales_Phase = 'Credits Card') OR
                                                         (Sales_Phase = 'PO Received') AND (PO_Month = '3')
                               GROUP BY Sales_Phase) AS MAR ON MAR.Sales_Phase = FEB.Sales_Phase LEFT OUTER JOIN
                             (SELECT        Sales_Phase, COALESCE (SUM(PO_Amount_USD), 0) AS April
                               FROM            MasterTable_TM AS MasterTable_TM_3
                               WHERE        (Sales_Phase = 'Credits Card') OR
                                                         (Sales_Phase = 'PO Received') AND (PO_Month = '4')
                               GROUP BY Sales_Phase) AS APR ON APR.Sales_Phase = MAR.Sales_Phase LEFT OUTER JOIN
                             (SELECT        Sales_Phase, COALESCE (SUM(PO_Amount_USD), 0) AS May
                               FROM            MasterTable_TM AS MasterTable_TM_4
                               WHERE        (Sales_Phase = 'Credits Card') OR
                                                         (Sales_Phase = 'PO Received') AND (PO_Month = '5')
                               GROUP BY Sales_Phase) AS MAY ON MAY.Sales_Phase = APR.Sales_Phase LEFT OUTER JOIN
                             (SELECT        Sales_Phase, COALESCE (SUM(PO_Amount_USD), 0) AS June
                               FROM            MasterTable_TM AS MasterTable_TM_5
                               WHERE        (Sales_Phase = 'Credits Card') OR
                                                         (Sales_Phase = 'PO Received') AND (PO_Month = '6')
                               GROUP BY Sales_Phase) AS JUN ON JUN.Sales_Phase = MAY.Sales_Phase LEFT OUTER JOIN
                             (SELECT        Sales_Phase, COALESCE (SUM(PO_Amount_USD), 0) AS July
                               FROM            MasterTable_TM AS MasterTable_TM_6
                               WHERE        (Sales_Phase = 'Credits Card') OR
                                                         (Sales_Phase = 'PO Received') AND (PO_Month = '7')
                               GROUP BY Sales_Phase) AS JUL ON JUL.Sales_Phase = JUN.Sales_Phase LEFT OUTER JOIN
                             (SELECT        Sales_Phase, COALESCE (SUM(PO_Amount_USD), 0) AS August
                               FROM            MasterTable_TM AS MasterTable_TM_7
                               WHERE        (Sales_Phase = 'Credits Card') OR
                                                         (Sales_Phase = 'PO Received') AND (PO_Month = '8')
                               GROUP BY Sales_Phase) AS AUG ON AUG.Sales_Phase = JUL.Sales_Phase LEFT OUTER JOIN
                             (SELECT        Sales_Phase, COALESCE (SUM(PO_Amount_USD), 0) AS September
                               FROM            MasterTable_TM AS MasterTable_TM_8
                               WHERE        (Sales_Phase = 'Credits Card') OR
                                                         (Sales_Phase = 'PO Received') AND (PO_Month = '9')
                               GROUP BY Sales_Phase) AS SEP ON SEP.Sales_Phase = AUG.Sales_Phase LEFT OUTER JOIN
                             (SELECT        Sales_Phase, COALESCE (SUM(PO_Amount_USD), 0) AS October
                               FROM            MasterTable_TM AS MasterTable_TM_9
                               WHERE        (Sales_Phase = 'Credits Card') OR
                                                         (Sales_Phase = 'PO Received') AND (PO_Month = '10')
                               GROUP BY Sales_Phase) AS OCT ON OCT.Sales_Phase = SEP.Sales_Phase LEFT OUTER JOIN
                             (SELECT        Sales_Phase, COALESCE (SUM(PO_Amount_USD), 0) AS November
                               FROM            MasterTable_TM AS MasterTable_TM_10
                               WHERE        (Sales_Phase = 'Credits Card') OR
                                                         (Sales_Phase = 'PO Received') AND (PO_Month = '11')
                               GROUP BY Sales_Phase) AS NOV ON NOV.Sales_Phase = OCT.Sales_Phase LEFT OUTER JOIN
                             (SELECT        Sales_Phase, COALESCE (SUM(PO_Amount_USD), 0) AS December
                               FROM            MasterTable_TM AS MasterTable_TM_11
                               WHERE        (Sales_Phase = 'Credits Card') OR
                                                         (Sales_Phase = 'PO Received') AND (PO_Month = '12')
                               GROUP BY Sales_Phase) AS DEC ON DEC.Sales_Phase = NOV.Sales_Phase

9月和10月内有数据记录,但另一个月没有数据记录。 我试图运行此查询但返回零结果。如果我删除其他月份并且仅在查询中留下9月和10月,则会有结果回馈给我。

如果显示另一个月,即使它是null,怎么办呢? 使用COALESCE()函数,但它不起作用。

1 个答案:

答案 0 :(得分:1)

你真的不需要那么多的连接来实现数据透视,只需像这样分组:

SELECT Sales_Phase
     , SUM(case PO_Month when 1 then PO_Amount_USD else 0 end) AS January
     , SUM(case PO_Month when 2 then PO_Amount_USD else 0 end) AS Febuary
     , SUM(case PO_Month when 3 then PO_Amount_USD else 0 end) AS March
     , SUM(case PO_Month when 4 then PO_Amount_USD else 0 end) AS April
     , SUM(case PO_Month when 5 then PO_Amount_USD else 0 end) AS May
     , SUM(case PO_Month when 6 then PO_Amount_USD else 0 end) AS June
     , SUM(case PO_Month when 7 then PO_Amount_USD else 0 end) AS July
     , SUM(case PO_Month when 8 then PO_Amount_USD else 0 end) AS August
     , SUM(case PO_Month when 9 then PO_Amount_USD else 0 end) AS September
     , SUM(case PO_Month when 10 then PO_Amount_USD else 0 end) AS October
     , SUM(case PO_Month when 11 then PO_Amount_USD else 0 end) AS November
     , SUM(case PO_Month when 12 then PO_Amount_USD else 0 end) AS December
  FROM MasterTable_TM
 WHERE (Sales_Phase = 'Credits Card') 
    OR (Sales_Phase = 'PO Received') 
   AND (PO_Month between '1' and '12') -- these conditions do really make no sense, but leave it as the original sample code
 GROUP BY Sales_Phase

您的查询中有两个问题:

  1. 如果1月没有数据,第一个子查询将不返回任何内容,因此其他左连接将不会返回任何内容。
  2. (Sales_Phase = 'Credits Card') OR (Sales_Phase = 'PO Received') AND (PO_Month = '1')如此令人困惑,如果Sales_Phase ='Credits Card',您想要从所有月份返回数据吗? AND的优先级高于OR