从瘦表旋转

时间:2014-05-28 20:23:34

标签: sql pivot max

我目前正在开发一个项目,并希望根据包含多个值的一列来轮换值,例如jobtitle和department_number。运行这个声明,我得到:

select * from trudates_repository where recordtype in ('DEPARTMENT_NUMBER', 'JOB_TITLE')     and EMP_ID = '1851'

EMP_ID  PERSON_ID  RECORDTYPE          RECORDVALUE      START_DATE  END_DATE
1851    417315     BASE_ROLE           Program Manager  12/29/2013  12/31/4712
1851    417315     BASE_ROLE           Project Manager  11/12/2007  12/28/2013
1851    417315     DEPARTMENT_NUMBER    10112           2/23/2014   12/31/4712
1851    417315     DEPARTMENT_NUMBER    10113           12/15/2013  2/22/2014
1851    417315     DEPARTMENT_NUMBER    11416           11/12/2007  12/22/2007
1851    417315     DEPARTMENT_NUMBER    11447           3/13/2011   12/14/2013
18511   417315     DEPARTMENT_NUMBER    11461           12/23/2007  3/12/2011

运行此pivot语句:

  select * from (
  select * from trudates_repository where recordtype in ('DEPARTMENT_NUMBER',      'BASE_ROLE') and HISL_ID = '188451'
)
pivot
(
MAX(RECORDVALUE)
FOR RECORDTYPE IN('BASE_ROLE', 'DEPARTMENT_NUMBER')
)  

我得到了

EMP_ID  PERSON_ID   START_DATE  END_DATE    'JOB_TITLE' 'DEPARTMENT_NUMBER'
1851    417315     2/23/2014    12/31/4712                    10112
1851    417315     12/29/2013   12/31/4712   Program Manager    
1851    417315     11/12/2007   12/22/2007                    11416
1851    417315     12/15/2013   2/22/2014                     10113
1851    417315     12/23/2007   3/12/2011                     11461
1851    417315     11/12/2007   12/28/2013   Project Manager    
1851    417315     3/13/2011    12/14/2013                    11447

我的问题是 - 有没有办法用通过sql逻辑对应的job_title填充这些空值?我正在为每个员工的每个数据开始日期尝试一个实线。

1 个答案:

答案 0 :(得分:0)

因此,这不是一个需要转轴的解决方案,它是一个分组和分区问题。

最大的问题是您必须合成新分区,因为某些现有(START_DATE, END_DATE)分区跨越(JOB_TITLE, DEPARTMENT_NUMBER)类别,并且无法满足。

例如,2014年之前和之后MAX(JOB_TITLE)的{​​{1}}是什么,这是否有意义?

这可能比一个支点更适合你想要实现的目标:

DEPARTMENT_NUMBER = '10113'

这会产生这样的结果:

SELECT x.EMP_ID, x.PERSON_ID,
   MAX(x.START_DATE) START_DATE, MIN(x.END_DATE) END_DATE,
   p.RECORDVALUE JOB_TITLE, q.RECORDVALUE DEPARTMENT_NUMBER
FROM trudates_repository x
CROSS APPLY (VALUES (START_DATE), (END_DATE)) y(PARTITION_DATE)
JOIN trudates_repository p
  ON DATEDIFF(day, y.PARTITION_DATE, p.START_DATE) <= 0
 AND DATEDIFF(day, y.PARTITION_DATE, p.END_DATE) >= 0
 AND p.EMP_ID = x.EMP_ID AND p.PERSON_ID = x.PERSON_ID
 AND p.RECORDTYPE = 'BASE_ROLE'
JOIN trudates_repository q
  ON DATEDIFF(day, y.PARTITION_DATE, q.START_DATE) <= 0
 AND DATEDIFF(day, y.PARTITION_DATE, q.END_DATE) >= 0
 AND q.EMP_ID = x.EMP_ID AND q.PERSON_ID = x.PERSON_ID
 AND q.RECORDTYPE = 'DEPARTMENT_NUMBER'
WHERE x.HISL_ID = '188451'
  AND x.EMP_ID = '1851'
  AND x.RECORDTYPE IN ('DEPARTMENT_NUMBER', 'BASE_ROLE')
GROUP BY x.EMP_ID, x.PERSON_ID, p.RECORDVALUE, q.RECORDVALUE
ORDER BY CAST(MAX(x.START_DATE) AS DATE),CAST(MAX(x.END_DATE) AS DATE)
;

...它仍然存在不切断开放EMP_ID PERSON_ID START_DATE END_DATE JOB_TITLE DEPARTMENT_NUMBER 1851 417315 11/12/2007 12/22/2007 Project Manager 11416 1851 417315 12/23/2007 3/12/2011 Project Manager 11461 1851 417315 3/13/2011 12/14/2013 Project Manager 11447 1851 417315 12/15/2013 12/28/2013 Project Manager 10113 1851 417315 12/29/2013 12/31/4712 Program Manager 10113 1851 417315 2/23/2014 12/31/4712 Program Manager 10112 值的结束日期的问题,但这是一个开始(我可能会在以后再回来修复它)。 / em>的

哦,我假设你在这里使用T-SQL。