3

基本的に、各従業員の各コストコードごとにtotal_work_hoursを表示するピボットクエリを作成しました。

これは私の望ましい出力です:

employeeno  8322.170    10184.2648    8321.169    10184.2649 <- costcodes
--------------------------------------------------------------------------
080418         10.00          1.50        NULL          NULL
080441          6.50          NULL        1.00          3.00

しかし、これは私のクエリの結果です:

employeeno  8322.170    10184.2648    8321.169    10184.2649 <- costcodes
--------------------------------------------------------------------------
080418         10.00          NULL        NULL          NULL
080418          NULL          1.50        NULL          NULL
080441          NULL          NULL        1.00          NULL
080441          6.50          NULL        NULL          NULL
080441          NULL          NULL        NULL          3.00

これは私の内部クエリの結果です:

      employeeno    costcoding  hour_per_costcode        
      --------------------------------------------------
      PH080418   8322.170       10.00
      PH080418   10184.2648     1.50
      PH080441   8321.169       1.00
      PH080441   8322.170       6.50
      PH080441   10184.2649     3.00

これは私の質問です:

WITH PivotData AS  
    (SELECT wa.id,wa.sitecode, wa.companycode, wa.startdate, wa.enddate,  
            wa.description, wa.ratetypeid, wa_details.employeeno,  
            CAST(wa_details.costcode AS NVARCHAR(MAX)) + '.' +
            CAST(wa_details.subcostcode AS NVARCHAR(MAX)) costcoding ,
            wa_details.subcostcode, wa_details.hrswork [hour_per_costcode],  
            view_ttl_hours.ttl_work_hrs 
       FROM workallocation wa  
      INNER JOIN workallocation_details wa_details 
         ON wa.id = wa_details.workallocationid   
      INNER JOIN 
            (SELECT SUM(ttl_work_hrs) ttl_work_hrs, employee_id 
               FROM vwu_SUM_TIMESHEET_DAILY  
              WHERE TKSDATE BETWEEN '02-09-2012' AND '02-09-2012' 
              GROUP BY employee_id
            ) view_ttl_hours 
         ON wa_details.employeeno=view_ttl_hours.employee_id  WHERE wa.id=99
    )
SELECT employeeno, [8322.170], [10184.2648], [8321.169], [10184.2649]
  FROM PivotData   
 PIVOT (MAX([hour_per_costcode])          
        FOR costcoding         
         IN ([8322.170], [10184.2648], [8321.169], [10184.2649])
        ) AS PivotResult  
 ORDER BY employeeno;

私の質問は、私の希望の出力が達成されるように、どのようにして複数の行の値を統合できるかということです。クロスタブクエリも試してみましたが、結果も同じです。

これを解決する方法についてのガイダンスが必要です。手伝ってくれますか?

4

1 に答える 1

4

不要な列を CTE から削除します。このようなものがうまくいくはずです。

WITH PivotData AS  
    (SELECT wa_details.employeeno,  
            CAST(wa_details.costcode AS NVARCHAR(MAX)) + '.' +
            CAST(wa_details.subcostcode AS NVARCHAR(MAX)) costcoding ,
            wa_details.subcostcode, wa_details.hrswork [hour_per_costcode]
       FROM workallocation wa  
      INNER JOIN workallocation_details wa_details 
         ON wa.id = wa_details.workallocationid   
      INNER JOIN 
            (SELECT SUM(ttl_work_hrs) ttl_work_hrs, employee_id 
               FROM vwu_SUM_TIMESHEET_DAILY  
              WHERE TKSDATE BETWEEN '02-09-2012' AND '02-09-2012' 
              GROUP BY employee_id
            ) view_ttl_hours 
         ON wa_details.employeeno=view_ttl_hours.employee_id  WHERE wa.id=99
    )
SELECT employeeno, [8322.170], [10184.2648], [8321.169], [10184.2649]
  FROM PivotData   
 PIVOT (MAX([hour_per_costcode])          
        FOR costcoding         
         IN ([8322.170], [10184.2648], [8321.169], [10184.2649])
        ) AS PivotResult  
 ORDER BY employeeno;

データを使用した上記のクエリ:

WITH PivotData AS  
    (select 'PH080418' as employeeno,   8322.170 as costcoding, 10.00 as hour_per_costcode union all
     select 'PH080418', 10184.2648, 1.50 union all
     select 'PH080441', 8321.169,   1.00 union all
     select 'PH080441', 8322.170,   6.50 union all
     select 'PH080441', 10184.2649, 3.00
    )
SELECT employeeno, [8322.170], [10184.2648], [8321.169], [10184.2649]
  FROM PivotData   
 PIVOT (MAX([hour_per_costcode])          
        FOR costcoding         
         IN ([8322.170], [10184.2648], [8321.169], [10184.2649])
        ) AS PivotResult  
 ORDER BY employeeno;

結果:

employeeno 8322.170   10184.2648   8321.169   10184.2649
---------- ---------- ------------ ---------- -----------
PH080418   10.00      1.50         NULL       NULL
PH080441   6.50       NULL         1.00       3.00
于 2012-05-07T06:07:43.393 に答える