Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
KiwiinAussie
Starting Member
5 Posts |
Posted - 2008-06-13 : 03:08:25
|
| Here's the query:create table #weeks( WeekNumber int)insert into #weeks (WeekNumber) values (0)insert into #weeks (WeekNumber) values (1)insert into #weeks (WeekNumber) values (2)insert into #weeks (WeekNumber) values (3)insert into #weeks (WeekNumber) values (4)insert into #weeks (WeekNumber) values (5)insert into #weeks (WeekNumber) values (6)insert into #weeks (WeekNumber) values (7)insert into #weeks (WeekNumber) values (8)declare @startDate datetimeset @startdate = '2008-06-13'select case w.WeekNumber when 0 then 'Up to 1wk old' when 1 then '1wk to 2wks old' when 2 then '2wks to 3wks old' when 3 then '3wks to 4wks old' when 4 then '4wks to 5wks old' when 5 then '5wks to 6wks old' when 6 then '6wks to 7wks old' when 7 then '7wks to 8wks old' when 8 then '8wks old' end as Age, isNull(t.Total, 0) as Totalfrom #weeks wleft outer join ( select round(datediff(dd, ReceivedDate, @startDate) / 7, 1) as NumWeeks, count(*) as Total from Log where ReceivedDate between dateadd(wk, -8, @startDate) and @startDate and Status = 'Open' group by round(datediff(dd, ReceivedDate, @startDate) / 7, 1) ) ton t.NumWeeks = w.WeekNumberorder by w.WeekNumberCurrent Results which are correct except for "8wks or more" which should return 102, but I can't seem to figure out a way of doing it. Can someone help me out please to do this, and/or a better way of doing it?Age TotalUp to 1wk old 601wk to 2wks old 302wks to 3wks old 213wks to 4wks old 154wks to 5wks old 95wks to 6wks old 66wks to 7wks old 27wks to 8wks old 18wks or more 0Data: (these are all the open Status records).ReceivedDate2006-05-032006-10-062006-10-162006-11-162006-12-152007-01-232007-03-122007-03-122007-04-032007-05-212007-06-192007-07-092007-08-012007-08-092007-08-142007-08-172007-08-202007-09-032007-09-122007-09-182007-10-092007-10-102007-10-172007-11-052007-11-162007-11-192007-11-232007-11-232007-11-282007-12-112007-12-112007-12-142007-12-172007-12-192007-12-212007-12-212007-12-242008-01-022008-01-082008-01-222008-01-232008-01-242008-01-252008-01-292008-01-292008-01-312008-01-312008-02-012008-02-012008-02-052008-02-052008-02-062008-02-062008-02-112008-02-192008-02-192008-02-202008-02-222008-02-262008-02-272008-02-292008-02-292008-03-032008-03-042008-03-052008-03-052008-03-072008-03-102008-03-102008-03-102008-03-122008-03-122008-03-132008-03-192008-03-252008-03-272008-03-272008-03-272008-04-012008-04-022008-04-022008-04-022008-04-032008-04-042008-04-072008-04-072008-04-072008-04-092008-04-102008-04-102008-04-112008-04-112008-04-142008-04-142008-04-142008-04-152008-04-152008-04-162008-04-162008-04-172008-04-172008-04-172008-04-212008-04-292008-05-022008-05-052008-05-062008-05-072008-05-092008-05-092008-05-092008-05-122008-05-122008-05-122008-05-132008-05-132008-05-142008-05-142008-05-152008-05-162008-05-192008-05-202008-05-202008-05-202008-05-202008-05-212008-05-212008-05-212008-05-212008-05-212008-05-212008-05-222008-05-222008-05-232008-05-232008-05-262008-05-262008-05-272008-05-272008-05-272008-05-272008-05-282008-05-282008-05-292008-05-292008-05-292008-05-292008-05-292008-05-292008-05-302008-05-302008-05-302008-05-302008-05-302008-05-302008-05-302008-06-022008-06-022008-06-022008-06-022008-06-022008-06-022008-06-022008-06-022008-06-032008-06-032008-06-032008-06-032008-06-032008-06-032008-06-032008-06-032008-06-042008-06-042008-06-042008-06-042008-06-042008-06-042008-06-052008-06-052008-06-052008-06-052008-06-062008-06-062008-06-062008-06-062008-06-072008-06-072008-06-102008-06-102008-06-102008-06-102008-06-102008-06-102008-06-102008-06-102008-06-102008-06-102008-06-102008-06-102008-06-112008-06-112008-06-112008-06-112008-06-112008-06-112008-06-112008-06-112008-06-112008-06-122008-06-122008-06-122008-06-122008-06-122008-06-122008-06-122008-06-122008-06-122008-06-122008-06-122008-06-122008-06-122008-06-122008-06-122008-06-122008-06-122008-06-122008-06-122008-06-122008-06-122008-06-122008-06-122008-06-122008-06-122008-06-122008-06-122008-06-132008-06-132008-06-132008-06-132008-06-132008-06-132008-06-132008-06-132008-06-132008-06-13 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-13 : 03:32:19
|
| Its working as expected. Inside derived table you've added the conditionReceivedDate between dateadd(wk, -8, @startDate) and @startDateThis will take data only from 18/04/2008 onwards. so first record picked will be one with date 21/04/2008. and when you take round(datediff(dd, ReceivedDate, @startDate) / 7, 1) for this it will be 7 so you wont have any records with NumWeeks 8 and so count value will return null for it which is set to 0 by isnull.b/w i didnt understand the use of round() there as even without it you will get only integer result as both the operands involved are integers (datediff returns int & also 7 is int) |
 |
|
|
|
|
|
|
|