我如何对一个变量或数组有多个值

时间:2019-05-03 03:06:27

标签: sql arrays variables teradata teradata-sql-assistant

我在下面有一个有效的查询。但是,我需要将OWNRSHP_ID设置为一个变量,以便可以提供一个OWNRSHP_ID列表,以逐一地通过查询运行。

这个想法是要有一个变量,我可以提供一个ID列表,查询将一个接一个地运行。

我当前在Teradata中运行的此SQL。

Select distinct
 inv_corp.OWNRSHP_ID 
,SE.Sending_entity_nm AS Bottler
,snap.FSH_DSTR_OWNRSHP_ID AS DELIVERY_SOURCE
,inv_corp.BTLR_DLVR_PNT_NO AS BTLR_DLVR_PNT_NO
,snap.DLVR_PNT_NM AS BTLR_DLVR_PNT_NM
,snap.CHNL_TYPE_DESC AS MARKET_TYPE
,snap.TRADE_CHNL_DESC AS TRADE_CHNL_DESC
,snap.TCCC_SUB_CHNL_NM AS TCCC_SUB_CHNL_NM
,snap.STATE AS STATE

,CASE   
                WHEN cbs.CBS_BUSINESS_TYPE = 1 THEN 'TRADE SALE' 
                WHEN cbs.CBS_BUSINESS_TYPE = 4 THEN 'FULL SERVE' 
                WHEN cbs.CBS_BUSINESS_TYPE = 5 THEN 'EXTRNL SLS' 
                ELSE NULL 
            END AS CBS_BUSINESS_TYPE 
,ptype.PRD_TYP_NM AS PROD_TYPE
,cal.CUR_YR_AC_PRD AS CAL_YEAR_MONTH
--,cal.CUR_YR_AC_PRD AS CAL_YEAR_MONTH
,inv_corp.Inv_dt

,sum(CASE   
                WHEN (qty) IS NOT NULL THEN (qty* TCCC_192CONV(FLOAT)) 
                ELSE 0 
    END )AS VolEQ   --20 fields

 ,sum(CASE    
                WHEN (qty) IS NOT NULL THEN (cast(qty as FLOAT )* cast(prodet.STD_ADJ_FCT as FLOAT)) 
                ELSE 0 
    END) AS VolStd 



,(sum(Whlsl_Price_Xtnd) - sum(Off_Inv_Cma_Disc))  - (sum(Off_Inv_Ctm_Disc) - sum(On_Inv_Disc) ) as DNSI



FROM CORP_LINES_VIEWS.LINE_CORP line_corp
join corp_invoice_VIEWS.invoice_corp  inv_corp
On line_corp.Tsr_Inv_Ref_No = inv_corp.Tsr_Inv_Ref_No
  and  line_corp.OWNRSHP_ID = inv_corp.OWNRSHP_ID

 Join CBS_VIEWS.CBS_DELIVERY_POINT cbs 
   ON inv_corp.BTLR_DLVR_PNT_NO  = cbs.BTLR_DLVR_PNT_NO
   and inv_corp.OWNRSHP_ID = cbs.OWNRSHP_ID

   Join CHR_VIEWS.DPT_SNAPSHOT_SELECT snap
   ON inv_corp.BTLR_DLVR_PNT_NO  = snap.BTLR_DLVR_PNT_NO
  and inv_corp.OWNRSHP_ID = snap.OWNRSHP_ID 

  Join CBS_VIEWS.sending_entity se
  ON inv_corp.OWNRSHP_ID = se.OWNRSHP_ID

  Join NSR_VIEWS.PRODET prodet
  ON prodet.cce_material_no = line_corp.cce_material_no

  Join DLCCC_CUST_CNTC_CNTR_LAB.TBL_3PY_to_YTD_CAL cal
  ON CAL.fact_dt = line_corp.Inv_dt
Join CORP_LINES.MATL_JOIN_KEY mkey
   ON mkey.cce_material_no = line_corp.cce_material_no
  and  mkey.OWNRSHP_ID = line_corp.OWNRSHP_ID  

  Join IWR_VIEWS.MN_PROD_TYPE ptype
  ON ptype.tsr_prd_typ = prodet.tsr_prd_typ




Where 1=1 
and line_corp.inv_dt>'2018-01-01'  
and line_corp.OWNRSHP_ID IN (10,55,75,110,65,85,95) 

GROUP BY 
 inv_corp.OWNRSHP_ID 
,SE.Sending_entity_nm 
,snap.FSH_DSTR_OWNRSHP_ID 
,inv_corp.BTLR_DLVR_PNT_NO
,snap.DLVR_PNT_NM 
,snap.CHNL_TYPE_DESC 
,snap.TRADE_CHNL_DESC
,snap.TCCC_SUB_CHNL_NM 
,snap.STATE 
,cbs.CBS_BUSINESS_TYPE 
,ptype.PRD_TYP_NM 
,cal.CUR_YR_AC_PRD 
,inv_corp.Inv_dt

1 个答案:

答案 0 :(得分:0)

好吧,您目前正在

中提供OWNRSHP_ID的列表
and line_corp.OWNRSHP_ID IN (10,55,75,110,65,85,95)

但是,假设您希望将此列表作为逗号分隔的字符串传递,则可以应用STRTOK_SPLI_TO_TABLE

and line_corp.OWNRSHP_ID IN
 (
   SELECT Cast(token AS INT) AS OWNRSHP_ID, tokennum
   FROM TABLE (StrTok_Split_To_Table(1, :OWNRSHP_IDs, ',')
        RETURNS (outkey INTEGER,
                 tokennum INTEGER,
                 token VARCHAR(20) CHARACTER SET Unicode)
              ) AS d 
 )

如果将其移动到派生表中,您甚至可以按ID在输入字符串中的位置进行排序:

...
Join IWR_VIEWS.MN_PROD_TYPE ptype
ON ptype.tsr_prd_typ = prodet.tsr_prd_typ
JOIN 
 (
   SELECT Cast(token AS INT) AS OWNRSHP_ID, tokennum
   FROM TABLE (StrTok_Split_To_Table(1, :OWNRSHP_IDs, ',')
        RETURNS (outkey INTEGER,
                 tokennum INTEGER,
                 token VARCHAR(20) CHARACTER SET Unicode)
              ) AS d 
 ) as dt
ON inv_corp.OWNRSHP_ID = dt.OWNRSHP_ID
...
ORDER BY tokennum;

当您外部连接到派生表时,您也可以为缺少的ID返回NULL行,但这可能需要对查询进行一些其他更改。