如何查找复杂SQL查询返回的列数

时间:2016-05-11 10:16:57

标签: sql unix informatica-powercenter

采取以下示例查询。我想查找查询返回的列数

SELECT '29-JAN-16' AS AS_OF_DATE,
       WBD.CASHPOOL_TREAS_CODE,
       WBD.CNTRPART_TREAS_CODE,
       WBD.PRIN_BAL_AMT,
       (SELECT EX.EOD_SPOT_CNV_RATE
          FROM EDW.T_FACT_EXCH_RATE_LONG EX,
               EDW.T_DIM_DATE            DT,
               EDW.T_DIM_CURR            FC,
               EDW.T_DIM_CURR            TC
         WHERE EX.FIN_DATE_KEY = DT.DATE_KEY
           AND EX.FROM_CURR_KEY = FC.CURR_KEY
           AND EX.TO_CURR_KEY = TC.CURR_KEY
           AND EXISTS (SELECT DT.CAL_DATE
                  FROM EDW.T_FACT_EXCH_RATE_LONG EX,
                       EDW.T_DIM_DATE            DT,
                       EDW.T_DIM_CURR            FC,
                       EDW.T_DIM_CURR            TC
                 WHERE EX.FIN_DATE_KEY = DT.DATE_KEY
                   AND EX.FROM_CURR_KEY = FC.CURR_KEY
                   AND EX.TO_CURR_KEY = TC.CURR_KEY
                   AND TC.CURR_CODE = 'USD')
           AND EXISTS (SELECT FC.CURR_CODE
                  FROM EDW.T_FACT_EXCH_RATE_LONG EX,
                       EDW.T_DIM_DATE            DT,
                       EDW.T_DIM_CURR            FC,
                       EDW.T_DIM_CURR            TC
                 WHERE EX.FIN_DATE_KEY = DT.DATE_KEY
                   AND EX.FROM_CURR_KEY = FC.CURR_KEY
                   AND EX.TO_CURR_KEY = TC.CURR_KEY
                   AND TC.CURR_CODE = 'USD')) AS EOD_SPOT_CNV_RATE,

       (SELECT EX.MOR_CURR_CNV_RATE
          FROM EDW.T_FACT_EXCH_RATE_LONG EX,
               EDW.T_DIM_DATE            DT,
               EDW.T_DIM_CURR            FC,
               EDW.T_DIM_CURR            TC
         WHERE EX.FIN_DATE_KEY = DT.DATE_KEY
           AND EX.FROM_CURR_KEY = FC.CURR_KEY
           AND EX.TO_CURR_KEY = TC.CURR_KEY
           AND EXISTS (SELECT DT.CAL_DATE
                  FROM EDW.T_FACT_EXCH_RATE_LONG EX,
                       EDW.T_DIM_DATE            DT,
                       EDW.T_DIM_CURR            FC,
                       EDW.T_DIM_CURR            TC
                 WHERE EX.FIN_DATE_KEY = DT.DATE_KEY
                   AND EX.FROM_CURR_KEY = FC.CURR_KEY
                   AND EX.TO_CURR_KEY = TC.CURR_KEY
                   AND TC.CURR_CODE = 'USD')
           AND EXISTS (SELECT FC.CURR_CODE
                  FROM EDW.T_FACT_EXCH_RATE_LONG EX,
                       EDW.T_DIM_DATE            DT,
                       EDW.T_DIM_CURR            FC,
                       EDW.T_DIM_CURR            TC
                 WHERE EX.FIN_DATE_KEY = DT.DATE_KEY
                   AND EX.FROM_CURR_KEY = FC.CURR_KEY
                   AND EX.TO_CURR_KEY = TC.CURR_KEY
                   AND TC.CURR_CODE = 'USD')) MOR_CURR_CNV_RATE,
       (SELECT EX.MNTHLY_GAP_CURR_CNV_RATE
          FROM EDW.T_FACT_EXCH_RATE_LONG EX,
               EDW.T_DIM_DATE            DT,
               EDW.T_DIM_CURR            FC,
               EDW.T_DIM_CURR            TC
         WHERE EX.FIN_DATE_KEY = DT.DATE_KEY
           AND EX.FROM_CURR_KEY = FC.CURR_KEY
           AND EX.TO_CURR_KEY = TC.CURR_KEY
           AND EXISTS (SELECT DT.CAL_DATE
                  FROM EDW.T_FACT_EXCH_RATE_LONG EX,
                       EDW.T_DIM_DATE            DT,
                       EDW.T_DIM_CURR            FC,
                       EDW.T_DIM_CURR            TC
                 WHERE EX.FIN_DATE_KEY = DT.DATE_KEY
                   AND EX.FROM_CURR_KEY = FC.CURR_KEY
                   AND EX.TO_CURR_KEY = TC.CURR_KEY
                   AND TC.CURR_CODE = 'USD')
           AND EXISTS (SELECT FC.CURR_CODE
                  FROM EDW.T_FACT_EXCH_RATE_LONG EX,
                       EDW.T_DIM_DATE            DT,
                       EDW.T_DIM_CURR            FC,
                       EDW.T_DIM_CURR            TC
                 WHERE EX.FIN_DATE_KEY = DT.DATE_KEY
                   AND EX.FROM_CURR_KEY = FC.CURR_KEY
                   AND EX.TO_CURR_KEY = TC.CURR_KEY
                   AND TC.CURR_CODE = 'USD')) MNTHLY_GAP_CURR_CNV_RATE,
       (SELECT EX.QTRLY_GAP_CURR_CNV_RATE
          FROM EDW.T_FACT_EXCH_RATE_LONG EX,
               EDW.T_DIM_DATE            DT,
               EDW.T_DIM_CURR            FC,
               EDW.T_DIM_CURR            TC
         WHERE EX.FIN_DATE_KEY = DT.DATE_KEY
           AND EX.FROM_CURR_KEY = FC.CURR_KEY
           AND EX.TO_CURR_KEY = TC.CURR_KEY
           AND EXISTS (SELECT DT.CAL_DATE
                  FROM EDW.T_FACT_EXCH_RATE_LONG EX,
                       EDW.T_DIM_DATE            DT,
                       EDW.T_DIM_CURR            FC,
                       EDW.T_DIM_CURR            TC
                 WHERE EX.FIN_DATE_KEY = DT.DATE_KEY
                   AND EX.FROM_CURR_KEY = FC.CURR_KEY
                   AND EX.TO_CURR_KEY = TC.CURR_KEY
                   AND TC.CURR_CODE = 'USD')
           AND EXISTS (SELECT FC.CURR_CODE
                  FROM EDW.T_FACT_EXCH_RATE_LONG EX,
                       EDW.T_DIM_DATE            DT,
                       EDW.T_DIM_CURR            FC,
                       EDW.T_DIM_CURR            TC
                 WHERE EX.FIN_DATE_KEY = DT.DATE_KEY
                   AND EX.FROM_CURR_KEY = FC.CURR_KEY
                   AND EX.TO_CURR_KEY = TC.CURR_KEY
                   AND TC.CURR_CODE = 'USD')) QTRLY_GAP_CURR_CNV_RATE
  FROM EDW.T_ICF_CASHPOOL_ACCT_DETL ICAD
  LEFT OUTER JOIN (SELECT MLC.PD_TO_DATE,
                          ICBD.BAL_RPTG_DATE,
                          ICBD.IHB_ACCT_ID,
                          ICBD.BAL_CURR_CODE,
                          ICBD.OPEN_PRIN_BAL_AMT,
                          ICBD.PRIN_ADDN_AMT,
                          ICBD.PRIN_RPYMT_AMT,
                          ICBD.CLOSG_PRIN_BAL_AMT,
                          ICBD.OPEN_INT_BAL_AMT,
                          ICBD.ACCR_INT_CHRG_AMT,
                          ICBD.INT_SETL_AMT,
                          ICBD.CLOSG_INT_BAL_AMT,
                          ICBD.CASHPOOL_TREAS_CODE,
                          ICBD.CNTRPART_TREAS_CODE,
                          ICBD.PRIN_BAL_AMT
                     FROM EDW.T_ICF_CASHPOOL_BAL_DETL ICBD
                     JOIN EDW.T_MD_LOAD_CNTL MLC
                       ON MLC.SRCE_SYS_NM = 'EDW'
                      AND ICBD.BAL_RPTG_DATE = MLC.PD_TO_DATE) WBD
    ON ICAD.IHB_ACCT_ID = WBD.IHB_ACCT_ID

我尝试过使用Informatica中的逻辑: 在'SELECT'和'FROM'之间计算逗号的数量+ 1。但是当存在像上面的查询这样的派生列时,此逻辑会失败。这个问题可以解决什么问题?解决方案可以来自Informatica,UNIX脚本。不建议直接在数据库中运行查询

PS:我知道这里的列数是8。这个查询只是一个示例。我希望通过Expression转换中的某些逻辑计算列数,甚至UNIX脚本都可以

2 个答案:

答案 0 :(得分:1)

数数吧。

有8列。

答案 1 :(得分:0)

根据CathalMF的回答手动计算它们,或者使用Select Into将此查询的结果插入到新表中,并使用以下查询查找列数:

SELECT COUNT(*)
FROM sys.columns c
WHERE c.object_id = OBJECT_ID('MyTable')

如果执行查询不是一个选项,那么你必须修改某种代码来解析sql Query并找出列数。就像你说的计算逗号数量一样。对于派生列,您需要匹配开始和结束括号,而不计算这些括号中的逗号。这是一种方法。