WAMPserver下的最新PHP
SQL Server 2005
用户将两个变量传递给我的.php报告页面,$ thedate1和$ thedate2,它们是我想从数据库中检索的值的开始和结束日期时间戳。我汇总数据并将其转换为SUM函数以获取我想要的报告,但我不知道如何在日期范围之间包括所有日期以在最终报告中显示为单独的列。
因此,如果$ thedate1 ='2012-02-20'和$ thedate2 ='2012-02-28'我希望在最终报告中动态创建每天(包括)之间的列。目前我必须手动将日期添加到查询中,但我希望有一种方法可以自动添加日期。
有什么想法吗?
WITH T
AS(
SELECT CorrectionUser, CorrectionsCount,
DATEADD(dd, DATEDIFF(d, 0, DateHourCorrected), 0) as [DATE]
FROM ISIImageCorrections
)
SELECT CorrectionUser AS [Employee],
COALESCE([2012-02-20], 0) AS [2012-02-20],
COALESCE([2012-02-21], 0) AS [2012-02-21],
COALESCE([2012-02-22], 0) AS [2012-02-22]
FROM T
PIVOT(SUM(CorrectionsCount) FOR [Date] IN([2012-02-20], [2012-02-21], [2012-02-22]))
AS P
ORDER BY CorrectionUser
答案 0 :(得分:0)
非常简单的方法。希望我帮助过:)
if OBJECT_ID('tempdb..#t1') is not null DROP TABLE #t1;
CREATE TABLE #t1 (d date NOT NULL);
DECLARE @start date;
SET @start = '2012-04-05';
DECLARE @end date;
SET @end = '2012-04-25';
WHILE (@start <= @end)
begin
INSERT INTO #t1 VALUES(DATEADD(day, 1, @start));
SET @start = DATEADD(day, 1, @start);
end;
SELECT * FROM #t1;
DROP TABLE #t1