如何将这些更改为列?

时间:2016-06-24 14:11:45

标签: sql sql-server database

我尝试过使用CTE,但一直无法弄明白。我想将多行更改为一行,其中包含多个列。任何方式去做...

当前结果

ern CostPool
111 NULL
111 5

期望的结果

ern CostPool1   CostPool2
111 NULL        5



SELECT first_name, last_name, ern, pc, title, TitleDesc, OfficeTitle, SUM(earned) earned, fypaydate, q
, [RMTS, Support, or Partial Quarter], SUM(CAST([Staff Employment Status] AS INT)) sumEmpStatus, SUM(CAST(countisMHy AS INT)) sumcountisMHy, SUM(CAST(sumisMHy AS INT)) sumisMHy1
, SUM(CAST(sumEligible AS INT)) sumEligible1
, CostPool
FROM (
SELECT
  e.first_name, e.last_name
  , fss.ern
  , fss.pc
  , fss4.title
  , u.OfficeTitle
  , t.TitleDesc
  , SUM(fss.gross*fss.[percent]) earned
  , fss.fypaydate, fss.q
  , cpayfss3, cpaypdq
  , CASE WHEN cpayfss3 = cpaypdq THEN '1' --full q
    WHEN cpayfss3 < cpaypdq THEN '2' --part q
    ELSE 'Check'
    END [RMTS, Support, or Partial Quarter]
  , CASE WHEN COUNT(DISTINCT rc.isMHy) = 2 THEN '9' --split
    WHEN fss.pc = 'A' AND COUNT(DISTINCT rc.isMHy) = 1 AND SUM(CAST(rc.isMHy AS INT)) > 0 THEN '1' --full
    WHEN fss.pc IN ('X', 'O', 'W', 'L', 'K') AND SUM(CAST(rc.isMHy AS INT)) > 0 THEN '2' --part
    ELSE '0'
    END [Staff Employment Status]
  , COUNT(DISTINCT rc.isMHy) countisMHy
  , SUM(CAST(rc.isMHy AS INT)) sumisMHy
  , SUM(CAST(rc.Eligible AS INT)) sumEligible --count distinct too?
  , rc.CostPool
  FROM [FIN_DataMart].[dbo].[FSSpaydetl_NEW] fss
  LEFT JOIN FIN_DataMart.[dbo].[FSSRC_NEW] rc
  ON fss.newrc = rc.RC
  AND fss.fypaydate = rc.FY
  INNER JOIN 
  (
   SELECT ern, fypaydate, q, COUNT(DISTINCT paydate) cpayfss3
   FROM [FIN_DataMart].[dbo].[FSSpaydetl_NEW]
   GROUP BY fypaydate, q, ern
  ) fss3
  ON fss.ern = fss3.ern 
  AND fss.fypaydate = fss3.fypaydate
  AND fss.q = fss3.q
  LEFT JOIN
  (
   SELECT COUNT(DISTINCT paydate) cpaypdq, fy, q
   FROM [FIN_DataMart].[dbo].[FSSPayDateQ]
   GROUP BY fy, q
  ) pdq
  ON fss.fypaydate = pdq.fy
  AND fss.q = pdq.q
  LEFT JOIN
  [FIN_DataMart].[dbo].FSSEmployeeInfo e
  ON fss.ern = e.ern
  INNER JOIN
  (
   SELECT DISTINCT ern, title, tlvl, fy, q, ROW_NUMBER() OVER (PARTITION BY ern, fy, q ORDER BY title DESC) rn
   FROM [FIN_DataMart].[dbo].[FSSpaydetl_NEW]
   ) fss4
   ON fss.ern = fss4.ern
   AND fss.fypaydate = fss4.fy
   AND fss.q = fss4.q
   AND fss4.rn = '1'
  LEFT JOIN
  (
   SELECT TCode, TLevel, GenericTDesc as TitleDesc, EED,
   ROW_NUMBER() OVER (PARTITION BY TCode, TLevel ORDER BY EED DESC) rn
   FROM [FIN_DataMart].[dbo].FSATitle
  ) t
   ON fss4.title = t.TCode
   AND case when fss4.tlvl = '' THEN '01' WHEN fss4.tlvl IS NULL THEN '01' ELSE fss4.tlvl END = t.TLevel
   AND t.rn = 1
  LEFT JOIN FIN_DataMart.[dbo].[FSSUserERN] u
  ON fss.ern = u.ern
  WHERE fss.fypaydate = 2015 
  AND fss.q = 1 
  AND fss.obj <> '049' AND fss.obj NOT LIKE 'x%'
  AND fss.title NOT IN ('10209', '1020B', '10234', '10232', '20202', '21205', '30086', '34201')
  GROUP BY
   fss.ern
  , fss.pc
  , fss4.title
  , fss.fypaydate, fss.q
  , cpayfss3, cpaypdq
  , rc.CostPool
  , t.TitleDesc
  , e.first_name
  , e.last_name
  , u.OfficeTitle
  HAVING SUM(CAST(rc.Eligible AS INT)) >=1
  ) A
  GROUP BY
   first_name, last_name, ern, pc, title, fypaydate, q
  , [RMTS, Support, or Partial Quarter]
  , OfficeTitle
  , TitleDesc
  , CostPool
  ORDER BY first_name, last_name, fypaydate, q

1 个答案:

答案 0 :(得分:2)

您可以尝试使用 Pivot 运算符将行转换为列。

https://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx

该方法可行,尽管您的示例数据和预期结果与您发布的大量查询不完全匹配,因此在您提供更多测试数据/预期输出(和表模式)之前,我无法提供更具体的示例。 p>