PHP/SQL查询的透视/交叉表结果
在最新版本的WAMPserver下运行最新版本的PHP 数据库是SQLServer2005 以下是我当前运行的有效查询。我要做的是将输出更改为:PHP/SQL查询的透视/交叉表结果,php,sql,sql-server,sql-server-2005,pivot,Php,Sql,Sql Server,Sql Server 2005,Pivot,在最新版本的WAMPserver下运行最新版本的PHP 数据库是SQLServer2005 以下是我当前运行的有效查询。我要做的是将输出更改为: CorrectionsCount , Employee , Date 1 , Joe , 02/12/2012 31 , Barbara , 02/13/2012 12 , Paula , 02/16/2012 像这样的事情
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
代码:
这里我使用的是
SUM
,选择您喜欢的任何聚合函数:
WITH T AS(
SELECT Employee, CorrectionsCount, [Date]
FROM @yourTable
)
SELECT Employee,
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;
若要修复错误,请从中删除
前面的“,”,以备将来通知。如果有人发布了答案,而您对答案有疑问,您可以在他们的答案下方添加注释,他们将收到通知。非常感谢!终于开始工作了。
$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"');
WITH T AS(
SELECT Employee, CorrectionsCount, [Date]
FROM @yourTable
)
SELECT Employee,
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;