我当前的查询是这样的:
╔════════════════════════════════════════╗
║ 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 :(得分: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;
答案 1 :(得分: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上使用我的示例。
答案 2 :(得分: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'