0
在最新版本的WAMPserver下運行最新版本的PHP。PHP/SQL查詢中的Pivot/Cross選項卡結果
數據庫是SQL Server 2005
這是我正在運行的查詢的作品。我想要做的是改變從輸出:
CorrectionsCount , Employee , Date
1 , Joe , 02/12/2012
31 , Barbara , 02/13/2012
12 , Paula , 02/16/2012
要像這樣
[EMPLOYEE NAME], 02/12/2012, 02/13/2012, 02/16/2012
Joe , 31 , 0 , 0
Barbara , 0 , 31 , 0
Paula , 0 , 0 , 12
代碼:
<?php
// connect to DSN
$DP2connect = odbc_connect("DB", "USER", "PWORD") or die ("Could not connect to
server");
$DP2query = "
SELECT SUM(CorrectionsCount),CorrectionUser,
convert(char(10), DateHourCorrected, 120)
FROM ISIImageCorrections
WHERE DateHourCorrected BETWEEN '$theDate1' AND '$theDate2'
GROUP BY CorrectionsCount,CorrectionUser,DateHourCorrected
";
$DP2result2 = odbc_exec($DP2connect, $DP2query);
odbc_result_all($DP2result2, 'id="results"');
?>
$ thedate1和$ thedate2是變量被派駐到這個頁面通過另一個PHP頁面的日曆選擇器。
更新:我嘗試運行下面描述的COALESCE函數,但它在關鍵字'FROM'(在此語句中是「FROM」的第二個實例)附近生成語法錯誤。我輸入正確嗎?
$DP2connect = odbc_connect("dp2_database", "DP2", "DP2Express!") or die ("Could not connect to
server");
$DP2query =
"WITH T
AS(
SELECT CorrectionUser, CorrectionsCount, DateHourCorrected
FROM ISIImageCorrections
)
SELECT CorrectionUser,
COALESCE([02/12/2012], 0) AS [02/12/2012],
COALESCE([02/13/2012], 0) AS [02/13/2012],
COALESCE([02/16/2012], 0) AS [02/16/2012],
FROM T
PIVOT(SUM(CorrectionsCount) FOR [Date] IN([02/12/2012], [02/13/2012], [02/16/2012])) AS P";
$DP2result2 = odbc_exec($DP2connect, $DP2query);
odbc_result_all($DP2result2, 'id="results"');
修復錯誤,刪除「」在'FROM'之前出現 - 爲未來的通知,如果某人發佈了答案,並且您對答案有問題,您可以在其答案下方發表評論,他們會收到通知。 – 2012-02-28 20:19:35
非常感謝!最後讓它工作。 – Cory 2012-02-29 13:48:39