我有一个非常简单的表格,其中包含user_id
,_project_id
,day
和hours
列。一个简单的数据透视查询用于计算每天在每个项目上花费的小时数:
SELECT *
FROM
(SELECT
hours, project_id,
LEFT(DATENAME(dw, day), 3) AS workday
FROM
activities
WHERE
user_id = '" . $user . "'
AND DATEPART(wk, day) = '" . $week . "'
AND DATEPART(yyyy, day) = '" . $year . "') temp
PIVOT (
sum([hours])
for workday in ([mon],[tue],[wed],[thu],[fri])
) AS pvt
ORDER BY project;
$ user,$week
和$year
参数是HTML表单参数。由于DATEPART
子句中的WHERE
函数,它可以提高性能,以便为表创建额外的列“week
”和额外的列“year
”并添加两列的索引?或者在日期栏中添加索引是否足够?