[quoted text, click to view] "Ant" <Ant@discussions.microsoft.com> wrote in message
news:036F28CF-5A39-4D12-9F29-290E889767EB@microsoft.com...
> Hi Liz,
>
> Thanks for your help with this, I've decided on your solution.
> I noticed that it sometimes shows 53 weeks per year as you warned. What
> can
> I do about this? I'm using this to compare to another table so if I used
> the
> same query for the other tbl, will this be an issue?
there will ALWAYS be 53 "weeks" per year (52 * 7 = 364); the solution
depends on what exactly the business logic requires ... you might have
another look at what David W posted and flesh it out a bit more ... you may
have preferred mine because it exposed the date boundaries but that doesn't
mean it's better, or even suitable for your requirements
as David suggested, you could work up a dates table, which might look
something like this:
CREATE TABLE weeksTable (
theDate DateTime,
xWeek int)
-- I'll leave you to figure out how to populate it but the data would look
like:
1/1/2007 1
1/2/2007 1
.....
1/8/2007 2
....
1/15/2007 3
and then JOIN it ... that way you can cut your weeks off at the end of the
calendar year or simply make them continuous regardless of year boundaries
..... again, depends on the business logic
something like this should work for the JOIN:
SELECT b.xWeek, SUM(a.Cost)
FROM myTransactions a
JOIN weeksTable b ON a.trxDate = b.theDate
GROUP BY b.xWeek
this is not a new problem; you can probably Google up a complete solution
in reasonably short order if you can't make sense of the input here ...
maybe SQLServerCentral.com has something ... good luck
L
[quoted text, click to view] > Many thanks
>
> Ant
>
> "Liz" wrote:
>
>>
>>
>> "Ant" <Ant@discussions.microsoft.com> wrote in message
>> news:DC90F7E3-D0C5-4C65-8E4C-B8F136EB13C8@microsoft.com...
>>
>> > I have a table which includes a cost column & a date column. The dates
>> > are
>> > roughly one or two days apart.
>> > I need to sum the costs into weekly groups.
>> >
>> > How can I do this?
>> >
>> > Many thanks in advance for any ideas on this
>>
>> this should get you started:
>>
>> select min(someDate), max(someDate), datepart(yy, someDate), datepart(wk,
>> someDate), sum(CostColumn), count(*) FROM tickets
>> GROUP BY datepart(yy, someDate), datepart(wk, tick_date) order by 3, 4
>>
>> Be careful about week numbers at year boundaries; results may not be as
>> you
>> expect
>>
>>
>>