Sql 构造一个用户定义的函数,返回一个表(或其他替代项),以使查询更具可读性

Sql 构造一个用户定义的函数,返回一个表(或其他替代项),以使查询更具可读性,sql,union,sybase,readability,user-defined-functions,Sql,Union,Sybase,Readability,User Defined Functions,我的查询中当前有一个类似于此的部分 , 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 tBef

我的查询中当前有一个类似于此的部分

, 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    
)
它工作得很好,但我想让它看起来更紧凑。有没有一种方法可以构造一个函数
foo
,这样我就可以通过这样做来实现相同的结果

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

这里有另一个选择,但不确定它是否让问题变得更糟。这将增加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
       )

我使用Sybase。但是,如果我看到它是如何在另一个DBMS中完成的,我可能会知道它是如何在Sybase中完成的。这在Sybase上不起作用,因为t3不识别b,c。这是什么DBMS的工作?你是对的。我试图提供帮助,但您必须为t1t2中的字段定义单独的别名。Sybase有unpivot吗?这是解决这个问题的最好方法。看起来没有,但谢谢你提到。这可能会让我找到其他选择。