1

我只玩了几个月的 SQL,所以请放轻松。

所以我目前有以下将返回如下内容:

SELECT   WorkItems.workDate
       , SUM(WorkItems.hours)as [hours]
       , WorkItems.techID
FROM         Jobs
INNER JOIN WorkItems
ON Jobs.jobID = WorkItems.jobID
INNER JOIN Techs
ON Jobs.techID = Techs.techID
WHERE (WorkItems.workDate BETWEEN @startdate AND @enddate)
AND (WorkItems.techID = 41)
GROUP BY WorkItems.workDate
, WorkItems.techID

回报:

2013-06-03 00:00:00.000 7.00    41
2013-06-05 00:00:00.000 7.00    41
2013-06-06 00:00:00.000 7.50    41
2013-06-07 00:00:00.000 1.00    41

我正在尝试填补查询留下的日期空白,以获得如下信息:

2013-06-03 00:00:00.000 7.00    41
2013-06-04 00:00:00.000 0       41
2013-06-05 00:00:00.000 7.00    41
2013-06-06 00:00:00.000 7.50    41
2013-06-07 00:00:00.000 1.00    41

我尝试通过在定义的范围内生成所有内容来添加日期,但现在我得到了重复。

Declare @t table(Date datetime, hoursWorked int, tech int)

Declare @startdate datetime
Declare @enddate datetime
declare @techID int
Set @startdate='06/02/2013'
Set @enddate='06/07/2013'
Set @techID='41'

Select dateadd(day,number,@startdate) as workDate, 0 as [hours], @techID from master.dbo.spt_values 
where master.dbo.spt_values.type='p' and dateadd(day,number,@startdate)<=@enddate
and dateadd(day,number,@startdate) not in 
(select date from @t)
UNION
SELECT   WorkItems.workDate, SUM(WorkItems.hours)as [hours], WorkItems.techID
FROM         Jobs INNER JOIN
                  WorkItems ON Jobs.jobID = WorkItems.jobID INNER JOIN
                  Techs ON Jobs.techID = Techs.techID
WHERE     (WorkItems.workDate BETWEEN @startdate AND @enddate) AND (WorkItems.techID =     41)
GROUP BY WorkItems.workDate, WorkItems.techID

回报:

2013-06-02 00:00:00.000 0.00    41
2013-06-03 00:00:00.000 0.00    41
2013-06-04 00:00:00.000 0.00    41
2013-06-04 00:00:00.000 7.00    41
2013-06-05 00:00:00.000 0.00    41
2013-06-05 00:00:00.000 7.00    41
2013-06-06 00:00:00.000 0.00    41
2013-06-06 00:00:00.000 7.50    41
2013-06-07 00:00:00.000 0.00    41
2013-06-07 00:00:00.000 1.00    41

对不起,如果这个查询值得畏缩,但我只是想了解 SQL。

谢谢。

4

0 回答 0