如何写一个SQL来实现“目标”部分?选择所有选定模块和选定年份的费率,无论他们是否获得当年的费率。
年份和模块将由用户输入,然后 SQL 将返回所选年份和所选模块的总费率。
例如,查找2008-2012年ASD、EB、GOF模块的速率,如上
WITH finanimpact
AS (SELECT Extract(YEAR FROM fiwm.create_dt) AS years,
m.code AS mcode,
SUM(fiwm.rate) AS rate
FROM master.financial_impact_wfins_mapping fiwm
inner join master.wf_instance wf
ON wf.sysid = fiwm.wf_instance
AND wf.template_id = 4
inner join master.wf_data_cr wdc
ON wdc.instance_id = fiwm.wf_instance
inner join parameter.module m
ON m.sysid = wdc.module
AND m.sysid IN ( 4, 15, 6 )
GROUP BY Extract(YEAR FROM fiwm.create_dt),
m.code
UNION
(SELECT NULL,
m1.code,
0
FROM parameter.module m1
WHERE m1.sysid IN ( 4, 15, 6 )
MINUS
SELECT NULL,
m.code AS mcode,
0
FROM master.financial_impact_wfins_mapping fiwm
inner join master.wf_instance wf
ON wf.sysid = fiwm.wf_instance
AND wf.template_id = 4
inner join master.wf_data_cr wdc
ON wdc.instance_id = fiwm.wf_instance
inner join parameter.module m
ON m.sysid = wdc.module
AND m.sysid IN ( 4, 15, 6 )
GROUP BY Extract(YEAR FROM fiwm.create_dt),
m.code)) SELECT fi.years,
fi.mcode,
fi.rate
FROM finanimpact fi
UNION
(SELECT LEVEL + 2008 - 1,
'',
0 AS id
FROM dual
CONNECT BY LEVEL <= 2012 - 2008 + 1
MINUS
(SELECT DISTINCT lfi.years,
'',
0
FROM finanimpact lfi))
returned result:
=========================
2008 null 0
2009 null 0
2010 EB 34640
2010 GOF 8660
2011 EB 103920
2011 GOF 12990
2012 null 0
null ASD 0
=========================
goal:
=========================
2008 ASD 0
2009 ASD 0
2010 EB 34640
2010 GOF 8660
2011 EB 103920
2011 GOF 12990
2012 ASD 0
=========================
这看起来像是一个家庭作业问题,它正在调查您对外部联接以及 NVL 函数(而不是您当前拥有的等值联接)的理解程度。