我编写了一个SQL查询,它将结果格式化为
ResourceName ProjectName TaskName Timesheet Period ActualWork TimeByDay DayName
Jim Carry Project A Task A 2011-12 40 09/10/2011 Monday
Jim Carry Project A Task A 2011-12 70 09/10/2011 Monday
Jim Carry Project B Task A 2011-12 80 09/12/2011 Tue.
Perth S. Project A Task A 2011-12 35 09/10/2011 Mon.
我想编写查询,以便它给我一些类似的东西;
ResourceName ProjectName TaskName Timesheet Period Monday Tue
Jim Carry Project A Task A 2011-12 110 80
正如您所看到的那样,新列在第二个查询名称中创建为星期一,星期二等等。加上总数也是根据日计算的,因此星期一列的结果是40 + 70,而星期二的结果是80 + 0。
以下是带来代码段1的查询
DECLARE @startdate DATETIME
DECLARE @enddate DATETIME
SET @startdate='1/1/2011'
SET @enddate='12/31/2011'
SELECT TOP 100 [IR.TimesheetProjectHoursByDay].ResourceName,
[IR.TimesheetProjectHoursByDay].ProjectName,
[IR.TimesheetProjectHoursByDay].TaskName,
[IR.TimesheetProjectHoursByDay].PeriodName + ' ' + '(' + CONVERT(NVARCHAR, [IR.TimesheetProjectHoursByDay].PeriodStartDate, 101) + ' - ' + CONVERT(NVARCHAR, [IR.TimesheetProjectHoursByDay].PeriodEndDate, 101) + ')' AS [Timesheet Period],
[IR.TimesheetProjectHoursByDay].ActualWork,
[IR.TimesheetProjectHoursByDay].TimeByDay,
DATENAME(dw,TimeByDay) as [DayName]
FROM [IR.TimesheetProjectHoursByDay]
WHERE [IR.TimesheetProjectHoursByDay].TimeByDay >= @StartDate
AND [IR.TimesheetProjectHoursByDay].TimeByDay <= @EndDate
AND [IR.TimesheetProjectHoursByDay].[Project Departments] LIKE N'CSENG'
AND [IR.TimesheetProjectHoursByDay].ActualWork > 0
AND [IR.TimesheetProjectHoursByDay].ResourceName LIKE '%Jim%'
GROUP BY [IR.TimesheetProjectHoursByDay].TimeByDay,
[IR.TimesheetProjectHoursByDay].ResourceName,
[IR.TimesheetProjectHoursByDay].ProjectName,
[IR.TimesheetProjectHoursByDay].TaskName,
[IR.TimesheetProjectHoursByDay].[Project Departments],
[IR.TimesheetProjectHoursByDay].ResourceStandardRate,
[IR.TimesheetProjectHoursByDay].PeriodName,
[IR.TimesheetProjectHoursByDay].PeriodStartDate,
[IR.TimesheetProjectHoursByDay].PeriodEndDate,
[IR.TimesheetProjectHoursByDay].ActualWork
ORDER BY [IR.TimesheetProjectHoursByDay].TimeByDay DESC
您能否建议我在SQL查询中需要做哪些更改,以便我可以获得代码片段2中提到的结果?
让我知道所需的任何输入。请查看测试数据表,使here的内容更加清晰 请指导!
谢谢!
答案 0 :(得分:0)
鉴于你的建议并不重要,因为你只是在一天的资源工作总时数之后,你可以尝试类似的东西(注意:这是未经测试的,但是只是一个例子);
SELECT TOP 100 [IR.TimesheetProjectHoursByDay].ResourceName,
[IR.TimesheetProjectHoursByDay].TimeByDay,
SUM([IR.TimesheetProjectHoursByDay].ActualWork) OVER(PARTITION BY [IR.TimesheetProjectHoursByDay].TimeByDay) AS "Total Hours"
然后将其用作派生表,按天组织。最近简单的谈话网站上有一个很棒的post on Window functions。请仔细阅读。这很有启发性。
答案 1 :(得分:0)
听起来你可以使用PIVOT;看看this example。
答案 2 :(得分:0)
如果我理解你是对的,你可以试试这个:
create table #tbl (ResourceName varchar(100), ProjectName varchar(100), TaskName varchar(100), ActualWork int, TimeByDay date)
insert into #tbl values ('Jim Carry','Project A','Task A',40,'20111009'),
('Jim Carry','Project A','Task A',70,'20111009'),
('Jim Carry','Project B','Task B',80,'20111209'),
('Perth S.','Project A','Task A',35,'20111209')
select ResourceName,ProjectName, year(TimeByDay), MONTH(TimeByDay),
sum(case when datename(weekday, TimeByDay)='Monday' then ActualWork else 0 end) Monday,
sum(case when datename(weekday, TimeByDay)='Tuesday' then ActualWork else 0 end) Tuesday,
sum(case when datename(weekday, TimeByDay)='Wednesday' then ActualWork else 0 end) Wednesday,
sum(case when datename(weekday, TimeByDay)='Thursday' then ActualWork else 0 end) Thursday,
sum(case when datename(weekday, TimeByDay)='Thursday' then ActualWork else 0 end) Thursday,
sum(case when datename(weekday, TimeByDay)='Friday' then ActualWork else 0 end) Friday,
sum(case when datename(weekday, TimeByDay)='Saturday' then ActualWork else 0 end) Saturday,
sum(case when datename(weekday, TimeByDay)='Sunday' then ActualWork else 0 end) Sunday
from #tbl
group by ResourceName,ProjectName, year(TimeByDay), MONTH(TimeByDay)
drop table #tbl
您也可以使用PIVOT作为CJM建议。