我被要求更改一个数据透视查询,该查询当前连续一周显示一周的结束日期,类别和按小时的小时数。我被要求在行的末尾显示一个注释字段,我无法弄清楚如何更改查询来执行此操作。
表的结构如下
Category Date Comments Hours
test 8/2/2010 myComment 2
test 8/3/2010 8
test 8/4/2010 4
test 8/5/2010 3
test 8/6/2010 5
我希望数据显示如下。我有一个查询将覆盖除评论之外的所有这些。在前端我只允许每周发表一条评论,并将其添加到表中的每周/类别组合的星期一日期行。
WeekEnding Category SunHrs MonHrs TuesHrs WedHrs ThuHrs FriHrs SatHrs Comment
8/7/2010 test 0 1 1 1 1 1 1 myComment
这是添加注释字段之前的查询,该字段工作正常。
DECLARE @WeekEnding datetime
DECLARE @UserName nvarchar(245)
SET @WeekEnding = '09/04/2010'
SET @UserName = 'brogers'
SELECT
@WeekEnding WeekEnding
,CategoryID
,isnull([1], 0) SunHrs
,isnull([2], 0) MonHrs
,isnull([3], 0) TueHrs
,isnull([4], 0) WedHrs
,isnull([5], 0) ThuHrs
,isnull([6], 0) FriHrs
,isnull([7], 0) SatHrs
from (select CategoryID, Datepart(dw, TimeEntryDate) DOW, TimeEntryDuration Hours
from dbo.aspnet_starterkits_TimeEntry
where TimeEntryDate between dateadd(dd, -6, @WeekEnding) and @WeekEnding) Source
pivot (max(Hours) for DOW in ([1],[2],[3],[4],[5],[6],[7]) ) as pvt
我不确定如何将注释字段添加到行的末尾。当我添加它时,我得到一个像这样的结果
WeekEnding Category SunHrs MonHrs TuesHrs WedHrs ThuHrs FriHrs SatHrs Comment
8/7/2010 test 0 0 1 1 1 1 0
8/7/2010 test 0 1 0 0 0 0 0 myComment
我只希望每个周末/类别组合有一行,输出中每行有一个注释。 以下是我添加注释字段并显示错误的查询。
任何人都可以指出如何每周显示一条评论/分类行吗?
DECLARE @WeekEnding datetime
DECLARE @UserName nvarchar(245)
SET @WeekEnding = '09/04/2010'
SET @UserName = 'brogers'
SELECT
@WeekEnding WeekEnding
,TimeEntryDescription
,CategoryID
,isnull([1], 0) SunHrs
,isnull([2], 0) MonHrs
,isnull([3], 0) TueHrs
,isnull([4], 0) WedHrs
,isnull([5], 0) ThuHrs
,isnull([6], 0) FriHrs
,isnull([7], 0) SatHrs
from (select
CategoryID,
Datepart(dw, TimeEntryDate) DOW,
TimeEntryDuration Hours,
TimeEntryDescription
from dbo.aspnet_starterkits_TimeEntry
where TimeEntryDate between dateadd(dd, -6, @WeekEnding) and @WeekEnding) Source
pivot (max(Hours) for DOW in ([1],[2],[3],[4],[5],[6],[7]) ) as pvt
即使源表有一周的每一天的评论字段,我只想要每周一个评论和每周一行/类别组合。
我将输入限制为每周只允许一个(例如星期一),并希望在输出查询的行末显示这一条注释。
答案 0 :(得分:1)
使用旧式支点
可能更容易;With [Source] As
(
select
CategoryID,
Datepart(dw, TimeEntryDate) DOW,
TimeEntryDuration Hours,
TimeEntryDescription
from dbo.aspnet_starterkits_TimeEntry
where TimeEntryDate between dateadd(dd, -6, @WeekEnding) and @WeekEnding
)
SELECT
@WeekEnding WeekEnding
,TimeEntryDescription
,CategoryID
,max(case when DOW = 1 then [Hours] else 0 end) SunHrs
,max(case when DOW = 2 then [Hours] else 0 end) MonHrs
,max(case when DOW = 3 then [Hours] else 0 end) TueHrs
,max(case when DOW = 4 then [Hours] else 0 end) WedHrs
,max(case when DOW = 5 then [Hours] else 0 end) ThuHrs
,max(case when DOW = 6 then [Hours] else 0 end) FriHrs
,max(case when DOW = 7 then [Hours] else 0 end) SatHrs
,max(comment) as comment
from [Source]
group by
TimeEntryDescription
,CategoryID
答案 1 :(得分:0)
使用源表加入数据透视表,并检索min(comment)
。
DECLARE @WeekEnding datetime
DECLARE @UserName nvarchar(245)
SET @WeekEnding = '09/04/2010'
SET @UserName = 'brogers'
;with Report as (
SELECT
@WeekEnding WeekEnding
,TimeEntryDescription
,CategoryID
,isnull([1], 0) SunHrs
,isnull([2], 0) MonHrs
,isnull([3], 0) TueHrs
,isnull([4], 0) WedHrs
,isnull([5], 0) ThuHrs
,isnull([6], 0) FriHrs
,isnull([7], 0) SatHrs
from (select
CategoryID,
Datepart(dw, TimeEntryDate) DOW,
TimeEntryDuration Hours,
TimeEntryDescription
from dbo.aspnet_starterkits_TimeEntry
where TimeEntryDate between dateadd(dd, -6, @WeekEnding) and @WeekEnding) Source
pivot (max(Hours) for DOW in ([1],[2],[3],[4],[5],[6],[7]) ) as pvt
)
select *
,(select min(comment) from dbo.aspnet_starterkits_TimeEntry te where TimeEntryDate between dateadd(dd, -6, @WeekEnding) and @WeekEnding)) [Comment]
from Report r