2014-07-04 46 views
4

我當前的查詢拉這樣的事情:即使在沒有行返回的情況下如何拉取摘要數據?

╔════════════════════════════════════════╗ 
║ A Monthly 123 123 123 123 123 123 ║ 
║ B Quarterly 123 123 123 123 123 123 ║ 
║ C SemiAnnual 123 123 123 123 123 123 ║ 
║ D Annual  123 123 123 123 123 123 ║ 
╚════════════════════════════════════════╝ 

的問題出現時,我沒有得到一個特定模式的行返回。當發生這種情況時,它只會下降模式。
我想讓它顯示:

╔════════════════════════════════════════╗ 
║ A Monthly 123 123 123 123 123 123 ║ 
║ B Quarterly 0 0 0 0 0 0 ║ 
║ C SemiAnnual 123 123 123 123 123 123 ║ 
║ D Annual  123 123 123 123 123 123 ║ 
╚════════════════════════════════════════╝ 

我知道有一種方法可以做到這一點。只是畫空白。就像分別拉模式然後拉和數?

這裏是我當前的查詢:

SELECT MODE, 
     SUM(POLCT)  AS POLCT, 
     SUM(RDRCNT)  AS RDRCNT, 
     SUM(INCCNT)  AS INCCNT, 
     SUM(INCINS)  AS INCINS, 
     SUM(INS_AMOUNT) AS INS_AMOUNT, 
     SUM (PREM)  AS PREM 
FROM (SELECT CASE 
       WHEN MODE = '12' THEN 'D Annual' 
       WHEN MODE = '03' THEN 'B Quarterly' 
       WHEN MODE = '06' THEN 'C SemiAnnual' 
       WHEN MODE = '01' THEN 'A Monthly' 
       ELSE ' ' 
       END       AS MODE, 
       POLICY_COUNT * NEGATIVE  AS POLCT, 
       RIDER_COUNT * NEGATIVE  AS RDRCNT, 
       INCNT * NEGATIVE    AS INCCNT, 
       INS_AMOUNT * NEGATIVE * INCNT AS INCINS, 
       INS_AMOUNT * NEGATIVE   AS INS_AMOUNT, 
       PRSC * NEGATIVE    AS PREM 
     FROM DIST_OF_ISSUES AS a 
       LEFT OUTER JOIN DIST_OF_ISSUES_PLANS AS b 
          ON a.PLANID = b.PLANID 
       LEFT OUTER JOIN DIST_OF_ISSUES_TYPE_ORDER AS c 
          ON b.TYPE = c.TYPE 
     WHERE SUBSTRING(a.PLANID, 1, 4) NOT IN ( 
       '1020', '2599', '1600', '1601', 
       '2597', '2598') 
       AND (a.MONTH < 4 
        AND a.MONTH > 0) 
       AND a.YEAR = 2014) AS A 
GROUP BY MODE 
ORDER BY MODE 

回答

0

這最終解決該問題:

Select 
MODE, 
SUM(POLCT) AS POLCT, 
SUM(RDRCNT) AS RDRCNT, 
SUM(INCCNT) AS INCCNT, 
SUM(INCINS) AS INCINS, 
SUM(INS_AMOUNT) AS INS_AMOUNT, 
SUM (PREM) AS PREM FROM 
(SELECT CASE    
when mode = '12' then 'D Annual' 
when mode = '03' then 'B Quarterly' 
when mode = '06' then 'C SemiAnnual'  
when mode = '01' then 'A Monthly'  
else ' ' end as MODE,  
POLICY_COUNT * NEGATIVE AS POLCT,  
RIDER_COUNT * NEGATIVE AS RDRCNT, 
Incnt * NEGATIVE AS INCCNT, 
INS_AMOUNT * NEGATIVE * Incnt AS INCINS, 
INS_AMOUNT * NEGATIVE AS INS_AMOUNT, 
PRSC * NEGATIVE AS PREM 
FROM Dist_Of_Issues as a 
Left Outer Join Dist_Of_Issues_Plans as b on a.PlanID = b.PlanID 
Left Outer Join Dist_Of_Issues_Type_Order as c on b.Type = c.Type 
where substring(a.PlanID,1,4) not in ('1020','2599','1600','1601','2597','2598') and 
(a.Month < 4 and a.Month > 0) and a.Year = 2014 
UNION ALL 
Select distinct (CASE    
when mode = '12' then 'D Annual' 
when mode = '03' then 'B Quarterly' 
when mode = '06' then 'C SemiAnnual'  
when mode = '01' then 'A Monthly'  
else ' '   end) as MODE, 0 as POLCT, 0 as RDRCNT, 0 as INCCNT, 0 as INCINS, 0 as 
INS_AMOUNT, 0 as PREM from Dist_Of_Issues) as A 
Group BY Mode ORDER BY Mode' 
0

你需要創建一個僞表,要在所有的值:

SELECT * 
FROM ( SELECT 'D Annual' AS Mode UNION ALL 
      SELECT 'B Quarterly' AS Mode UNION ALL 
      SELECT 'C SemiAnnual' AS Mode UNION ALL 
      SELECT 'A Monthly' AS Mode UNION ALL 
      SELECT ' ' AS Mode 
     ) AS m 

然後你就可以LEFT JOIN的主查詢到這一點,即使沒有記錄,也會返回行:

SELECT m.MODE, 
     SUM(a.POLCT) AS POLCT, 
     SUM(a.RDRCNT) AS RDRCNT, 
     SUM(a.INCCNT) AS INCCNT, 
     SUM(a.INCINS) AS INCINS, 
     SUM(a.INS_AMOUNT) AS INS_AMOUNT, 
     SUM(a.PREM) AS PREM 
FROM ( SELECT 'D Annual' AS Mode UNION ALL 
      SELECT 'B Quarterly' AS Mode UNION ALL 
      SELECT 'C SemiAnnual' AS Mode UNION ALL 
      SELECT 'A Monthly' AS Mode UNION ALL 
      SELECT ' ' AS Mode 
     ) AS m 
     ( SELECT CASE    
         WHEN mode = '12' THEN 'D Annual' 
         WHEN mode = '03' THEN 'B Quarterly' 
         WHEN mode = '06' THEN 'C SemiAnnual' 
         WHEN mode = '01' THEN 'A Monthly'  
         ELSE ' '   
        END AS MODE,  
        POLICY_COUNT * NEGATIVE AS POLCT, 
        RIDER_COUNT * NEGATIVE AS RDRCNT, 
        Incnt * NEGATIVE AS INCCNT, 
        INS_AMOUNT * NEGATIVE * Incnt AS INCINS, 
        INS_AMOUNT * NEGATIVE AS INS_AMOUNT, 
        PRSC * NEGATIVE AS PREM 
      FROM Dist_Of_Issues AS a 
        LEFT OUTER JOIN Dist_Of_Issues_Plans AS b 
         ON a.PlanID = b.PlanID 
        LEFT OUTER JOIN Dist_Of_Issues_Type_Order AS c 
         ON b.Type = c.Type 
      WHERE SUBSTRING(a.PlanID,1,4) NOT IN ('1020','2599','1600','1601','2597','2598') 
      AND  (a.Month < 4 AND a.Month > 0) 
      AND  a.Year = 2014 
     ) AS a 
      ON a.Mode = m.Mode 
GROUP BY m.Mode  
ORDER BY m.Mode; 
+0

這似乎像什麼,我攻向但是當我試圖用這個代碼,它不工作。看到我的答案是什麼工作。你的路線雖然看起來比較清潔 – user3805697

0

將「空白數據集」添加到您的隊列末尾用rownumber表示並用內部連接使用正確的行。

是這樣的:

WITH a 
    AS (SELECT * 
     FROM table2 
     UNION 
     SELECT * 
     FROM table1) 
SELECT t1.* 
FROM a T1 
     INNER JOIN (SELECT mode, 
          Min(rn) RN 
        FROM a 
        GROUP BY mode)T2 
       ON T1.mode = T2.mode 
        AND T1.rn = T2.rn 

Ypou可以SQL Fiddle玩弄我的榜樣。

相關問題