用于信息源限定符转换的递归DB2 Sql查询

时间:2018-06-18 09:24:55

标签: sql db2

我需要计算具有相同计算模式的50列的度量值。我不能对所有50列使用Union Query,因为Informatica设计器中的Source Qualifier最多可容纳32767个字符。任何人都建议如何实现这一目标?

    SELECT djm.jobcode|| '_' || djm.job_region || '_' || 
      md.srvy_country_cd || '_' || djm.match_year AS srvy_label, 
           djm.match_year, 
           'NA'            AS SRVY_PUBLISHER, 
           'Blend'         AS cut, 
           'MI_UNIV_BLEND' AS srvy_name, 
           md.srvy_country_cd, 
     ( md.base_25 ) * ( 1 + djm.adjustment ) * ( djm.geo_diff ) AS adj_base_25, 
( md.base_50 ) * ( 1 + djm.adjustment ) * ( djm.geo_diff )    adj_base_50, 
( md.base_75 ) * ( 1 + djm.adjustment ) * ( djm.geo_diff ) AS adj_base_75, 
           NULL  AS adj_COMM_25, 
           NULL   AS adj_COMM_50, 
           NULL AS adj_COMM_75, 
           djm.weight, 
           Sum(djm.weight) OVER (partition BY 
djm.jobcode,djm.job_region,djm.match_year) sum_weight, 
           djm.weight*100 / Sum(djm.weight) OVER (partition BY 
djm.jobcode,djm.job_region,djm.match_year) adj_weight 
FROM       md 
INNER JOIN djm 
ON         djm.sk = md.sk 
AND        djm.jobcode ='1234' 
AND        djm.job_region ='USAB' 
AND        djm.match_year ='2016' 
AND        ( ( Nvl(md.base_25, 0) > 0 AND Nvl(md.base_50, 0) > 0 
            AND Nvl(md.base_75, 0) > 0 ) ) 
UNION 
SELECT djm.jobcode|| '_' || djm.job_region || '_' || md.srvy_country_cd || 
'_' || djm.match_year AS srvy_label,  
           djm.match_year, 
           'NA'            AS SRVY_PUBLISHER, 
           'Blend'         AS cut, 
           'MI_UNIV_BLEND' AS srvy_name, 
           md.srvy_country_cd, 
           NULL AS adj_base_25, 
           NULL AS adj_base_50, 
           NULL  AS adj_base_75, 
 ( md.comm_25) * ( 1 + djm.adjustment ) * ( djm.geo_diff ) AS adj_COMM_25, 
 ( md.comm_50) * ( 1 + djm.adjustment ) * ( djm.geo_diff ) AS adj_COMM_50, 
 ( md.comm_75) * ( 1 + djm.adjustment ) * ( djm.geo_diff ) AS adj_COMM_75, 
           djm.weight, 
           Sum(djm.weight) OVER (partition BY 
 djm.jobcode,djm.job_region,djm.match_year)sum_weight, 
           djm.weight*100 / Sum(djm.weight) OVER (partition BY 
djm.jobcode,djm.job_region,djm.match_year) adj_weight 
FROM       md 
INNER JOIN djm 
ON         djm.sk = md.sk 
AND        djm.jobcode ='12178' 
AND        djm.job_region ='USAB' 
AND        djm.match_year ='2016' 
AND        ( (  Nvl(md.comm_25, 0) > 0 AND Nvl(md.comm_50, 0) > 0 
            AND Nvl(md.comm_75, 0) > 0 ) )  

1 个答案:

答案 0 :(得分:0)

我不确定您是需要50列还是50行。如果你在SELECT djm.jobcode|| '_' || djm.job_region || '_' || md.srvy_country_cd || '_' || djm.match_year AS srvy_label, djm.match_year, 'NA' AS SRVY_PUBLISHER, 'Blend' AS cut, 'MI_UNIV_BLEND' AS srvy_name, md.srvy_country_cd, ( md.base_25 ) * ( 1 + djm.adjustment ) * ( djm.geo_diff ) AS adj_base_25, ( md.base_50 ) * ( 1 + djm.adjustment ) * ( djm.geo_diff ) adj_base_50, ( md.base_75 ) * ( 1 + djm.adjustment ) * ( djm.geo_diff ) AS adj_base_75, NULL AS adj_COMM_25, NULL AS adj_COMM_50, NULL AS adj_COMM_75, djm.weight, Sum(djm.weight) OVER (partition BY djm.jobcode,djm.job_region,djm.match_year) sum_weight, djm.weight*100 / Sum(djm.weight) OVER (partition BY djm.jobcode,djm.job_region,djm.match_year) adj_weight FROM md INNER JOIN djm ON djm.sk = md.sk AND ( ( Nvl(md.base_25, 0) > 0 AND Nvl(md.base_50, 0) > 0 AND Nvl(md.base_75, 0) > 0 ) ) INNER JOIN TABLE( VALUES ( '1234', 'USAB', '2016') , ( '12178', 'USAB', '2016') -- etc etc ) AS s(jobcode,job_region,match_year) ON djm.jobcode = s.jobcode AND djm.job_region = s.job_region AND djm.match_year = s.match_year 子选择中放入所有50行,这将给你50行(我猜)。 (你也可以在那里使用表格)

{{1}}

如果您确实需要50列,则可以随时将它们移出对于

下面的结果集中的每一列的SUM(CASE WHEN ...)`