Sql-Server

如何連接多行的一列(查詢中使用的group by子句)

  • May 25, 2018

根據我的要求填充一些數據時遇到了一些問題,如下所示。

表數據:

---------------------------------------------
EMPID  PROJ_TYPE  HOURS  ST_DATE    ED_DATE
---------------------------------------------
1      DEV        10     2013-1-1   2013-5-1
1      DEV        34     2013-5-2   2013-8-1
1      DEV        23     2013-8-2   2013-10-1
1      TEST       12     2014-1-1   2014-3-1
1      TEST       25     2014-3-2   2014-6-1

在這裡我想用小時來獲取員工數據,所以我寫的很簡單

SELECT EMPID,
      PROJ_TYPE, 
      SUM(CASE WHEN PROJ_TYPE='DEV' THEN HOURS ELSE 0 END) DEV, 
      SUM(CASE WHEN PROJ_TYPE='TEST' THEN HOURS ELSE 0 END) TEST
FROM TABLE1
GROUP BY EMPID, PROJ_TYPE

因此,它按預期返回兩行,但在這裡我只希望每位員工一行。如果員工有多個PROJ_TYPE,那麼它應該結合兩個項目值並只返回一行。

最後我的結果集應該是這樣的:

----------------------------
EMPID  PROJ     DEV  TEST
----------------------------
1     DEV/TEST  67   37

希望我很清楚,如果對我的問題有任何疑問,請發表評論。

一個支點將為此工作

測試數據:

Declare @Test Table
(
EMPID          Int
,PROJ_TYPE      NVarchar(4)
,HOURS          Int
,ST_DATE        Date
,ED_DATE        Date
);
Insert Into @Test
(EMPID, PROJ_TYPE, HOURS, ST_DATE, ED_DATE)
Values ('1', 'DEV','10','2013-1-1','2013-5-1')
       ,('1', 'DEV','34','2013-5-2','2013-8-1')
       ,('1', 'DEV','23','2013-8-2','2013-10-1')
       ,('1', 'TEST','12','2014-1-1','2014-3-1')
       ,('1', 'TEST','25','2014-3-2','2014-6-1')
       ,('2', 'DEV','7','2013-5-2','2013-8-1')
       ,('2', 'DEV','14','2013-8-2','2013-10-1')
       ,('2', 'TEST','12','2014-1-1','2014-3-1')
       ,('2', 'TEST','19','2014-3-2','2014-6-1')
       ,('3', 'DEV','34','2013-5-2','2013-8-1')
       ,('3', 'DEV','8','2013-8-2','2013-10-1')
       ,('3', 'TEST','12','2014-1-1','2014-3-1')
       ,('3', 'TEST','7','2014-3-2','2014-6-1')
       ,('3', 'DEV','23','2013-8-2','2013-10-1')
       ,('3', 'TEST','18','2014-1-1','2014-3-1')
       ,('4', 'TEST','25','2014-3-2','2014-6-1');

詢問:

Select      EMPID
           ,IsNull(DEV,0) As DEV
           ,IsNull(TEST,0) As TEST
From        (
           Select      EMPID
                       ,PROJ_TYPE
                       ,HOURS
           From        @Test
           ) sq
   Pivot   (
           Sum(HOURS)
           For PROJ_TYPE In ([DEV],[TEST])
           ) piv;

SQL小提琴: https ://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=6d9a0ceb9dbf13e8af351822733c4607

只是不要按項目類型分組(也不要在沒有分組功能的情況下選擇它)

SELECT EMPID,         
      TOTAL_HOURS = SUM(HOURS), -- Just for cross-check
      PROJ = CASE WHEN SELECT (COUNT (DISTINCT PROJ_TYPE) 
                       FROM TABLE1 E 
                       WHERE E.EMPID = EE.EMPID 
                       GROUP BY E.EMPID) > 1 
                  THEN 'DEV/TEST' 
                  ELSE SELECT (DISTINCT PROJ_TYPE 
                               FROM TABLE1 TE 
                               WHERE TE.EMPID = EE.EMPID 
                               GROUP BY E.EMPID) 
             END,
    SUM(CASE WHEN PROJ_TYPE='DEV' THEN HOURS ELSE 0 END) AS DEV, 
    SUM(CASE WHEN PROJ_TYPE='TEST' THEN HOURS ELSE 0 END) AS TEST
FROM TABLE1 EE
GROUP BY EMPID

它不優雅,但應該可以工作

引用自:https://dba.stackexchange.com/questions/207738