2017-06-21 4 views
0

Ich versuche, den folgenden Code von SQL Server Teradata mit UNPIVOT Logik zu konvertieren:Konvertieren von SQL Server UNPIVOT zu TERADATA

insert into 
IPData 
    select 
    1, 
ProductCode || '|' || 
LocationCode || '|' || 
TimeCode || '|' || 
MeasureCode || '|' || 
cast(MeasureVal as varchar(12)) 
from 
(
select 
    p.SubsectionCode ProductCode, 
    td_ss.LocationCode, 
    td_ss.TimeCode, 
    sum(
    case 
      when td_ss.Life = 'C' 
and td_ss.Seasonality = 'AW' then td_ss.DepotStockRetail 
      else 0 
     end) StkDep_AW_Act, 
    sum(
case 
      when td_ss.Life = 'C' 
and td_ss.Seasonality = 'C' then td_ss.DepotStockRetail 
      else 0 
     end) StkDep_Cnt_Act, 
    sum(
case 
      when td_ss.Life = 'C' 
and td_ss.Seasonality = 'SS' then td_ss.DepotStockRetail 
      else 0 
     end) StkDep_SS_Act, 
    sum(
    case td_ss.Life 
      when 'T' then td_ss.DepotStockRetail 
      else 0 
     end) StkDep_Trm_Act, 
    sum(td_ss.DepotStockRetail) StkDep_Tot_Act 

from 
    Stock td_ss 
inner join 
    Product p 
on td_ss.Kimball = p.CurrentKimball 
and td_ss.SectArea = p.SectArea 
and td_ss.SeasonID = p.SeasonID 
group by 
    p.SubsectionCode, 
     td_ss.LocationCode, 
     td_ss.TimeCode)x 
UNPIVOT 
    (MeasureVal for MeasureCode in 
     (StkDep_AW_Act, StkDep_Cnt_Act, StkDep_SS_Act, StkDep_Trm_Act, StkDep_Tot_Act)) as unpvt 

where 
    MeasureVal <> 0; 

ich mit dem Teil UNPIVOT bin zu kämpfen. Ab sofort habe ich keine Daten in irgendeiner der Tabellen, so dass jede Hilfe in Bezug auf UNPIVOT sehr geschätzt wird.

+0

Es wäre einfacher, zu verstehen, wenn Sie Beispieldaten zur Verfügung gestellt und die gewünschten Ergebnisse. –

Antwort

0

Teradata unterstützt UNPIVOT in TD16, aber die Syntax ist näher an Oracle als an SQL Server.

Bevor es ein TD_UNPIVOT Tabelle Operator, soll dies die richtige Übersetzung sein:

INSERT INTO 
IPData 
SELECT * FROM TD_UNPIVOT(
ON(
    SELECT 
     1, 
     ProductCode || '|' || 
     LocationCode || '|' || 
     TimeCode || '|' || 
     MeasureCode || '|' || 
     Cast(MeasureVal AS VARCHAR(12)) 
    FROM 

     (
     SELECT 
      p.SubsectionCode ProductCode, 
      td_ss.LocationCode, 
      td_ss.TimeCode, 
      Sum(
     CASE 
        WHEN td_ss.Life = 'C' 
     AND td_ss.Seasonality = 'AW' THEN td_ss.DepotStockRetail 
        ELSE 0 
       end) StkDep_AW_Act, 
      Sum(
     CASE 
        WHEN td_ss.Life = 'C' 
     AND td_ss.Seasonality = 'C' THEN td_ss.DepotStockRetail 
        ELSE 0 
       end) StkDep_Cnt_Act, 
      Sum(
     CASE 
        WHEN td_ss.Life = 'C' 
     AND td_ss.Seasonality = 'SS' THEN td_ss.DepotStockRetail 
        ELSE 0 
       end) StkDep_SS_Act, 
      Sum(
     CASE td_ss.Life 
        WHEN 'T' THEN td_ss.DepotStockRetail 
        ELSE 0 
       end) StkDep_Trm_Act, 
      Sum(td_ss.DepotStockRetail) StkDep_Tot_Act 

     FROM 
      Stock td_ss 
     INNER JOIN 
      Product p 
     ON td_ss.Kimball = p.CurrentKimball 
     AND td_ss.SectArea = p.SectArea 
     AND td_ss.SeasonID = p.SeasonID 
     GROUP BY 
      p.SubsectionCode, 
       td_ss.LocationCode, 
       td_ss.TimeCode)x 
    ) AS dt 
    USING 
    VALUE_COLUMNS('MeasureVal') 
    UNPIVOT_COLUMN('Measures') 
    COLUMN_LIST('StkDep_AW_Act', 'StkDep_Cnt_Act', 'StkDep_SS_Act', 'StkDep_Trm_Act', 'StkDep_Tot_Act') 
) AS unpvt 

WHERE 
    MeasureVal <> 0; 
0
 insert into IPData 
       select 
       1, 
       ProductCode || '|' || 
       LocationCode || '|' || 
     TimeCode || '|' || 
     MeasureCode || '|' || 
     cast(MeasureVal as varchar(12)) 
    from 
    (Select 
      p.SubsectionCode ProductCode, 
      td_ss.LocationCode, 
      td_ss.TimeCode, 
      sum(case 
        when td_ss.Life = 'C' and td_ss.Seasonality = 'AW' then td_ss.DepotStockRetail 
        else 0 
       end) MeasureVal,'StkDep_AW_Act' AS MeasureCode 
     FROM Stock td_ss 
     inner join 
      Product p 
     on td_ss.Kimball = p.CurrentKimball 
     and td_ss.SectArea = p.SectArea 
     and td_ss.SeasonID = p.SeasonID 
     group by 
      p.SubsectionCode, 
      td_ss.LocationCode, 
      td_ss.TimeCode 
       having 
     MeasureVal <> 0 
    UNION ALL 
     Select 
      p.SubsectionCode ProductCode, 
      td_ss.LocationCode, 
      td_ss.TimeCode, 
      sum(case 
        when td_ss.Life = 'C' and td_ss.Seasonality = 'C' then td_ss.DepotStockRetail 
        else 0 
       end) MeasureVal, 'StkDep_Cnt_Act' AS MeasureCode 
     FROM Stock td_ss 
     inner join 
      Product p 
     on td_ss.Kimball = p.CurrentKimball 
     and td_ss.SectArea = p.SectArea 
     and td_ss.SeasonID = p.SeasonID 
     group by 
      p.SubsectionCode, 
      td_ss.LocationCode, 
      td_ss.TimeCode 
       having 
     MeasureVal <> 0 
    UNION ALL 
     Select 
      p.SubsectionCode ProductCode, 
      td_ss.LocationCode, 
      td_ss.TimeCode, 
      sum(case 
        when td_ss.Life = 'C' and td_ss.Seasonality = 'SS' then td_ss.DepotStockRetail 
        else 0 
       end) MeasureVal, 'StkDep_SS_Act' AS MeasureCode 
     FROM Stock td_ss 
     inner join 
      Product p 
     on td_ss.Kimball = p.CurrentKimball 
     and td_ss.SectArea = p.SectArea 
     and td_ss.SeasonID = p.SeasonID 
     group by 
      p.SubsectionCode, 
      td_ss.LocationCode, 
      td_ss.TimeCode 
       having 
     MeasureVal <> 0 
    UNION ALL 
     Select 
      p.SubsectionCode ProductCode, 
      td_ss.LocationCode, 
      td_ss.TimeCode, 
      sum(case td_ss.Life 
        when 'T' then td_ss.DepotStockRetail 
        else 0 
       end) MeasureVal, 'StkDep_Trm_Act' AS MeasureCode 
     FROM Stock td_ss 
     inner join 
      Product p 
     on td_ss.Kimball = p.CurrentKimball 
     and td_ss.SectArea = p.SectArea 
     and td_ss.SeasonID = p.SeasonID 
     group by 
      p.SubsectionCode, 
      td_ss.LocationCode, 
      td_ss.TimeCode 
       having 
     MeasureVal <> 0 
    UNION ALL 
     Select 
      p.SubsectionCode ProductCode, 
      td_ss.LocationCode, 
      td_ss.TimeCode, 
      sum(td_ss.DepotStockRetail) MeasureVal, 'StkDep_Tot_Act' AS MeasureCode 
     FROM Stock td_ss 
     inner join 
      Product p 
     on td_ss.Kimball = p.CurrentKimball 
     and td_ss.SectArea = p.SectArea 
     and td_ss.SeasonID = p.SeasonID 
     group by 
      p.SubsectionCode, 
      td_ss.LocationCode, 
      td_ss.TimeCode 
       having 
     MeasureVal <> 0 
    )x; 
+0

Vielen Dank an alle. Ich fand einen weiteren Ansatz wie gezeigt. – Jan

Verwandte Themen