2012-07-17 1 views
0

J'ai actuellement une section dans ma requête qui ressemble à ceLa construction d'une fonction définie par l'utilisateur qui retourne une table (ou d'autres solutions de rechange), pour rendre la requête plus lisible

, 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  
) 

il fonctionne très bien, mais Je voudrais le faire paraître plus compact. Est-il possible de construire une fonction foo telle que je coul obtenir les mêmes résultats en faisant quelque chose qui pourrait ressembler à ceci

, 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

J'utilise Sybase. Mais si je vois comment cela se passe dans un autre SGBD, je pourrais peut-être comprendre comment cela se fait dans Sybase. –

Répondre

0

Voici une autre option, mais pas sûr si elle fait qu'aggraver le problème. Cela augmente le nombre d'alias dans la clause with. Notez cependant que lorsque vous utilisez UNION ou UNION ALL, il vous suffit de spécifier le nouveau nom de colonne dans la première sous-requête.

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

Cela ne fonctionnera pas sur sybase puisque b, c ne sont pas reconnus par t3. Pour quel SGBD cela fonctionne-t-il? –

+0

Vous avez raison. . . J'essayais d'être utile, mais vous auriez à définir des alias séparés pour les champs de t1t2. Est-ce que Sybase a unpivot? C'est la meilleure approche pour résoudre cela. –

+0

Il ne semble pas y en avoir, mais merci de le mentionner. Cela pourrait me conduire à des alternatives. –

Questions connexes