0

以下是我的查询:

select employid,deprtmnt,jobtitle,[H-RT],[H-OT]
from 
(                  
 SELECT employid,deprtmnt,jobtitle,chekdate,UNTSTOPY,Category                  
 FROM 
 (                  
   SELECT employid,deprtmnt,jobtitle,chekdate,UNTSTOPY,payrolcd as     category              
   FROM [upr30300]    
   where DEPRTMNT like '600%'          
 ) t                  
 where chekdate = '2012-03-09' and Category in ('H-RT','H-OT')                  
) d             
PIVOT
(
  SUM(UNTSTOPY) 
  FOR Category IN ([H-RT],[H-OT])
)p

结果显示包含两个透视(H-RT 和 H-OT)的列。我可以按 chekdate 过滤并获得过滤器的结果,但我试图弄清楚如何获得两个显示 H-RT、H-OT 的 EXTRA 列,这将显示过滤后的 chekdate 加上 14 天。

我试图使用 dateadd 函数,但无法弄清楚如何获得 1 个结果集,其中包括....H-RT(period1),H-OT(period1),H-RT(period2),H-OT( period2)...有没有人知道如何完成这项工作的方向或想法?

先感谢您!

这是示例数据:

EMPLOYID    DEPRTMNT    JOBTITLE    H-RT    H-OT
ABC123          600-20  CA      77.78000    7.47000
ABC124          600-80  CSA     55.65000    0.58000
ABC125          600-70  RAA     77.68000    0.03000

这是所需的数据:

EMPLOYID    DEPRTMNT    JOBTITLE    H-RT1   H-OT1      H-RT2      H-OT2
ABC123          600-20  CA      77.78000    7.47000    80.00      12.00
ABC124          600-80  CSA     55.65000    0.58000    74.00      16.00
ABC125          600-70  RAA     77.68000    0.03000    48.00       2.00

所需的数据列 H-RT1,H-OT1 是 chekdate = '2012-03-09' 和列 H-RT2,H-OT2 将是 chekdate = '2012-03-09' + 14 天。

4

1 回答 1

0

您应该能够使用类似的东西:

select employid,
  deprtmnt,
  jobtitle,
  [H-RT_1],
  [H-OT_1], 
  [H-RT_2],
  [H-OT_2]
from 
(                  
 SELECT employid,
    deprtmnt,
    jobtitle,
    chekdate,
    UNTSTOPY,
    Category,
    case when chekdate = '2012-03-09' then Category+'_1' 
      when chekdate = dateadd(d, 14, '2012-03-09') then Category+'_2' end period
 FROM 
 (                  
   SELECT employid,
     deprtmnt,
     jobtitle,
     chekdate,
     UNTSTOPY,
     payrolcd as category              
   FROM [upr30300]    
   where DEPRTMNT like '600%'
 ) t                  
 where (chekdate = '2012-03-09' or chekdate = dateadd(d, 14, '2012-03-09'))
    and Category in ('H-RT','H-OT')                  
) d             
PIVOT
(
  SUM(UNTSTOPY) 
  FOR Category IN ([H-RT_1],[H-OT_1], [H-RT_2],[H-OT_2])
)p
于 2012-12-06T21:19:30.567 回答