2012-07-17 1 views
0

나는 현재 잘 작동이테이블 (또는 다른 대안)을 반환하는 사용자 정의 기능, 쿼리를 더 읽기 쉽게하기를 구축

, T3 AS (
      select 'FSA'   as tType, b.fsacd as tBefore, c.fsacd as tAfter from T1 as a , T2 as b,T2 as c where a.beforeID = b.tID and a.afterID =c.tID 
    union select 'Scale'   as tType, b.scd as tBefore, c.scd as tAfter from T1 as a , T2 as b,T2 as c where a.beforeID = b.tID and a.afterID =c.tID 
    union select 'Retail Source' as tType, b.rsc as tBefore, c.rsc as tAfter from T1 as a , T2 as b,T2 as c where a.beforeID = b.tID and a.afterID =c.tID 
    union select 'Mix Match'  as tType, b.mmcd as tBefore, c.mmcd as tAfter from T1 as a , T2 as b,T2 as c where a.beforeID = b.tID and a.afterID =c.tID 
    union select 'Price Entry' as tType, b.pecd as tBefore, c.pecd as tAfter from T1 as a , T2 as b,T2 as c where a.beforeID = b.tID and a.afterID =c.tID 
    union select 'Qntty Entry' as tType, b.qecd as tBefore, c.qecd as tAfter from T1 as a , T2 as b,T2 as c where a.beforeID = b.tID and a.afterID =c.tID  
    union select 'Price 3 Decs' as tType, b.p3d as tBefore, c.p3d as tAfter from T1 as a , T2 as b,T2 as c where a.beforeID = b.tID and a.afterID =c.tID  
    union select 'Tare Entry'  as tType, b.tecd as tBefore, c.tecd as tAfter from T1 as a , T2 as b,T2 as c where a.beforeID = b.tID and a.afterID =c.tID  
    union select 'Undiscountable' as tType, b.undsc as tBefore, c.undsc as tAfter from T1 as a , T2 as b,T2 as c where a.beforeID = b.tID and a.afterID =c.tID  
    union select 'Foodstamp'  as tType, b.fds as tBefore, c.fds as tAfter from T1 as a , T2 as b,T2 as c where a.beforeID = b.tID and a.afterID =c.tID  
    union select 'WIC'   as tType, b.wic as tBefore, c.wic as tAfter from T1 as a , T2 as b,T2 as c where a.beforeID = b.tID and a.afterID =c.tID  
) 

과 유사 내 쿼리의 섹션을 가지고 있지만, 좀 더 작게 보이게하고 싶습니다. 내가 여기에이

, T3 AS (
      foo('FSA'    ,fsacd ,T1, T2) 
    union foo('Scale'   ,scd ,T1, T2) 
    union foo('Retail Source' ,rsc ,T1, T2) 
    union foo('Mix Match'  ,mmcd ,T1, T2) 
    union foo('Price Entry'  ,pecd ,T1, T2) 
    union foo('Qntty Entry'  ,qecd ,T1, T2) 
    union foo('Price 3 Decs' ,p3d ,T1, T2) 
    union foo('Tare Entry'  ,tecd ,T1, T2) 
    union foo('Undiscountable' ,undsc ,T1, T2) 
    union foo('Foodstamp'  ,fds ,T1, T2) 
    union foo('WIC'    ,wic ,T1, T2) 
) 
+0

저는 Sybase를 사용합니다. 그러나 다른 DBMS에서 어떻게 완료되는지 알게되면 Sybase에서 어떻게 완료되었는지 파악할 수 있습니다. –

답변

0

처럼 보일 수 있습니다 무언가를 수행하여 동일한 결과를 달성 coul foo되도록 기능을 구성 할 수있는 방법은 다른 옵션하지만 문제가 악화되는 경우 확실하지가 있습니다. 이것은 with 절에서 별칭 수를 늘리고 있습니다. 그러나 UNION 또는 UNION ALL을 사용할 때 첫 번째 하위 쿼리에서 새 열 이름 만 지정하면됩니다.

tbefore as (select * 
      from T1 a join 
       T2 b 
       on a.beforeID = b.tID 
      ), 
tafter as (select * 
      from T1 a join 
       T2 c 
       on a.afterID = b.tID 
      ), 
t3b as (select 'FSA' as tType, fsacd as tBefore from tbefore union all 
     select 'Scale'as tType, scd from tbefore union all 
     ... 
     ), 
t3a as (select 'FSA' as tType, fsacd as tAfter from tafter union all 
     select 'Scale'as tType, scd from tafter union all 
     ... 
     ), 
t3 as (select t3b.ttype, t3b.tbefore, t3a.tafter 
     from t3b join t3a on t3b.ttype = t3a.ttype 
     ) 
+0

b, c는 t3에서 인식되지 않으므로 sybase에서는 작동하지 않습니다. 어떤 DBMS가이 기능을합니까? –

+0

당신 말이 맞아요. . . 도움이 되려고했지만 t1t2의 필드에 별개의 별칭을 정의해야했습니다. Sybase에 unpivot이 있습니까? 이것이 바로이 문제를 해결하는 가장 좋은 방법입니다. –

+0

거기에있는 것처럼 보이지 않지만 언급 해 주셔서 감사합니다. 그것은 나를 몇 가지 대안으로 이끌 수 있습니다. –

관련 문제