0
腳本(下面列出)給出了結果(見截圖01)Screenshot 01 但結果需要顯示爲(請參閱screenshot02)Screenshot02。如何將行轉換爲TSQL中的列?
work_date中的日期(請參見屏幕截圖01)需要顯示爲單個日期列,並且work_hour中的小時數(請參閱屏幕截圖01)需要在各個日期列下顯示。
DECLARE @01_STARTDATE VARCHAR(100);
DECLARE @02_ENDDATE VARCHAR(100);
DECLARE @STAFF INT;
SET @01_STARTDATE = '2017-07-06';
SET @02_ENDDATE = '2017-07-10';
SET @STAFF = 8;
SELECT
@01_STARTDATE + ' - ' + @02_ENDDATE AS [Date Range],
SU.FIRST_NAME + ' ' + SU.LAST_NAME AS Staff,
f.FACILITY_NAME AS Site_Name,
P.PRJ_PROJECT_NAME AS Project_Name,
R.PROJECT_TYPE_NAME AS Project_Type_Brownfield,
'' AS Project_Type_VRP,
st.work_hour, st.work_date
FROM
FAC_FACILITY AS f
INNER JOIN
GOV.PRJ_PROJECT AS P ON f.FACILITY_RID = p.FACILITY_RID
INNER JOIN
GOV.SYS_TIME_LOG AS ST ON ST.PRJ_PROJECT_RID = P.PRJ_PROJECT_RID
LEFT JOIN
SEC_USER AS SU ON SU.USER_RID = ST.USER_RID
LEFT JOIN
GOV.REF_PROJECT_TYPE AS R ON P.PROJECT_TYPE_RID = R.PROJECT_TYPE_RID
WHERE
ST.WORK_DATE BETWEEN CAST(@01_STARTDATE AS DATE) AND CAST(@02_ENDDATE AS DATE)
AND ST.USER_RID = @STAFF
AND R.PROJECT_TYPE_RID = 3
GROUP BY
f.FACILITY_NAME, P.PRJ_PROJECT_NAME, SU.FIRST_NAME,
st.work_date, SU.LAST_NAME, R.PROJECT_TYPE_NAME,
st.work_hour
UNION ALL
SELECT
@01_STARTDATE + ' - ' + @02_ENDDATE AS [Date Range],
SU.FIRST_NAME + ' ' + SU.LAST_NAME AS Staff,
f.FACILITY_NAME AS Site_Name,
P.PRJ_PROJECT_NAME AS Project_Name,
'' AS Project_Type_Brownfield,
R.PROJECT_TYPE_NAME AS Project_Type_VRP,
st.work_hour, st.work_date
FROM
FAC_FACILITY AS f
INNER JOIN
GOV.PRJ_PROJECT AS P ON f.FACILITY_RID = p.FACILITY_RID
INNER JOIN
GOV.SYS_TIME_LOG AS ST ON ST.PRJ_PROJECT_RID = P.PRJ_PROJECT_RID
LEFT JOIN
SEC_USER AS SU ON SU.USER_RID = ST.USER_RID
LEFT JOIN
GOV.REF_PROJECT_TYPE AS R ON P.PROJECT_TYPE_RID = R.PROJECT_TYPE_RID
WHERE
ST.WORK_DATE BETWEEN CAST(@01_STARTDATE AS DATE) AND CAST(@02_ENDDATE AS DATE)
AND ST.USER_RID = @STAFF
AND R.PROJECT_TYPE_RID = 2
GROUP BY
f.FACILITY_NAME, P.PRJ_PROJECT_NAME, SU.FIRST_NAME,
st.work_date, SU.LAST_NAME, R.PROJECT_TYPE_NAME,
st.work_hour
UNION ALL
SELECT
@01_STARTDATE + ' - ' + @02_ENDDATE AS [Date Range],
SU.FIRST_NAME + ' ' + SU.LAST_NAME AS Staff,
g.grant_name AS Site_Name,
'' AS project_name,
'' AS Project_Type_Brownfield,
'' AS Project_Type_VRP,
st.work_hour, st.work_date
FROM
(SELECT
SUM(work_hour) AS work_hour, user_rid,
grant_rid, work_date
FROM
GOV.SYS_TIME_LOG
WHERE
WORK_DATE BETWEEN CAST(@01_STARTDATE AS DATE) AND CAST(@02_ENDDATE AS DATE)
AND USER_RID = @STAFF
AND grant_rid = 1
GROUP BY
grant_rid, user_rid, work_date) AS ST
LEFT JOIN
SEC_USER AS SU ON SU.USER_RID = ST.USER_RID
LEFT JOIN
SYS_GRANT AS G ON st.grant_rid = g.grant_rid
WHERE
ST.WORK_DATE BETWEEN CAST(@01_STARTDATE AS DATE) AND CAST(@02_ENDDATE AS DATE)
AND ST.USER_RID = @STAFF
AND st.grant_rid = 1
GROUP BY
SU.FIRST_NAME, st.work_date, SU.LAST_NAME,
G.Grant_name, st.work_hour;
可能重複[SQL Server數據透視表與多個日期列](https://stackoverflow.com/questions/31300884/sql-server-pivot-table-with-multiple-column-with-dates) – JNevill
如果您正在使用支持PIVOT的RDBMS,然後查看有關該命令的供應商文檔。既然你標記TSQL,那麼我會建議查看第三方報告工具。這種彙總不適合標準的SQL,可能會出現笨拙和容易出錯的情況。 –
爲什麼你的日期變量varchar?列中的數據類型是什麼?動態的PIVOT或動態交叉標籤可能是您在這裏需要的。不太確定你爲什麼在這裏使用UNION ALL。你可以用一個查詢來做到這一點。 –