2013-08-19 61 views
2

嗨我創建了一個MS SQL查詢,它正在生成完美結果。但我需要更改行和列的值。我通過互聯網搜索並瞭解到PIVOT改變方向。我已經創建了一些小的查詢,但我無法通過任何幫助與連接和組一起使用它。如何使用Joins和Groupby編寫PIVOT查詢

這裏是我的查詢和輸出

SELECT AttendanceDate,count(attendance)AS attendanceCount,AttOPt.Name AS AttendanceStatus,emp.Name AS MarkedBy FROM Attendance 
LEFT OUTER JOIN AttendanceOption AttOPt 
ON AttOPt.ID=Attendance 
LEFT OUTER JOIN Employee emp 
on MarkedBy=emp.Id 
GROUP BY AttendanceDate,Attendance,MarkedBy,AttOPt.Name,emp.Name 

enter image description here

我喜歡這樣的輸出:

AttendanceDate Present Absent Half Day WithoutNotification MarkedBy 
14-08-2013  11  0  0   0     Anuj Koundal 
30-08-2013  4  3  2   2     Anuj Koundal 
+0

參考:[鏈接](http://stackoverflow.com/questions/9802057/pivot-data-in-sql-server?rq = 1) –

回答

2

嘗試這一個 -

SELECT * 
FROM ( 
    SELECT AttendanceDate 
     , attendanceCount = COUNT(attendance) 
     , AttendanceStatus = AttOPt.name 
     , MarkedBy = emp.name 
    FROM Attendance 
    LEFT JOIN AttendanceOption AttOPt ON AttOPt.id = Attendance 
    LEFT JOIN Employee emp ON MarkedBy = emp.id 
    GROUP BY 
     AttendanceDate 
    , Attendance 
    , AttOPt.name 
    , emp.name 
) t 
PIVOT 
(
    SUM(attendanceCount) 
    FOR AttendanceStatus IN ([Present], [Absent], [Half Day], [WithoutNotification]) 
) p 
ORDER BY AttendanceDate DESC 

更新:

-- variant #1 

SELECT 
     AttendanceDate 
    , MarkedBy 
    , [Present] = ISNULL([Present], 0) 
    , [Absent] = ISNULL([Absent], 0) 
    , [Half Day] = ISNULL([Half Day], 0) 
    , [WithoutNotification] = ISNULL([WithoutNotification], 0) 
FROM ( 
    ... 
) t 
PIVOT 
(
    ... 
) p 

-- variant #2 

SELECT * 
FROM ( 
    SELECT 
      AttendanceDate 
     , attendanceCount = COUNT(attendance) 
     , AttendanceStatus = AttOPt.name 
     , MarkedBy = emp.name 
    FROM Attendance 
    JOIN AttendanceOption AttOPt ON AttOPt.id = Attendance 
    JOIN Employee emp ON MarkedBy = emp.id 
    GROUP BY ALL 
      AttendanceDate 
     , Attendance 
     , AttOPt.name 
     , emp.name 
) t 
PIVOT 
(
    ... 
) p 

更新2:

DECLARE @Columns NVARCHAR(MAX) 
SELECT @Columns = STUFF((
    SELECT DISTINCT ', [' + a.name + ']' 
    FROM dbo.Attendance t 
    JOIN dbo.AttendanceOption a ON a.id = t.Attendance 
    FOR XML PATH(''), TYPE).value('.', 'VARCHAR(MAX)'), 1, 2, '') 

DECLARE @ColumnsNULLs NVARCHAR(MAX) 
SELECT @ColumnsNULLs = STUFF((
    SELECT DISTINCT ', [' + a.name + '] = ISNULL([' + a.name + '], 0)' 
    FROM dbo.Attendance t 
    JOIN dbo.AttendanceOption a ON a.id = t.Attendance 
    FOR XML PATH(''), TYPE).value('.', 'VARCHAR(MAX)'), 1, 2, '') 

DECLARE @SQL NVARCHAR(MAX) 
SELECT @SQL = ' 
SELECT AttendanceDate, ' + @ColumnsNULLs + ', MarkedBy 
FROM ( 
    SELECT AttendanceDate 
     , attendanceCount = COUNT(attendance) 
     , AttendanceStatus = AttOPt.name 
     , MarkedBy = emp.name 
    FROM Attendance 
    LEFT JOIN AttendanceOption AttOPt ON AttOPt.id = Attendance 
    LEFT JOIN Employee emp ON MarkedBy = emp.id 
    GROUP BY 
     AttendanceDate 
    , Attendance 
    , AttOPt.name 
    , emp.name 
) t 
PIVOT 
(
    SUM(attendanceCount) 
    FOR AttendanceStatus IN (' + @Columns + ') 
) p 
ORDER BY AttendanceDate DESC' 

PRINT @SQL 
EXEC sys.sp_executesql @SQL  

輸出 -

AttendanceDate   Absent  Half Day Present  Without Notification MarkedBy 
----------------------- ----------- ----------- ----------- -------------------- --------------- 
2013-08-30 00:00:00.000 3   2   4   2     Anuj Kaundal 
2013-08-14 00:00:00.000 0   0   11   0     Anuj Kaundal 
+0

Great soluti只留下小小的東西,如何用0替換NULL 0 in Present,Absent ..列 – MaxPayne

+0

請參閱更新後的答案。 – Devart

+0

Wohoooooooooo像IRON MAN工作 – MaxPayne