Unix Technical Forum

Need help with query

This is a discussion on Need help with query within the Sybase forums, part of the Database Server Software category; --> I am trying to write a query that will capture data for a particular date range and then group ...


Go Back   Unix Technical Forum > Database Server Software > Sybase

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-08-2008, 02:40 PM
William
 
Posts: n/a
Default Need help with query

I am trying to write a query that will capture data for a particular
date range and then group the data by week to be graphed. The script
below works well now but it will not be correct starting in January
because I am adding the number of weeks for the date. So in January
the dates in December will be around 50 so when I add that to the
first Monday of the year it will come back with December of 2004
instead of December 2003. I can not think of any way around this,
does anyone have a better solution? Any help would be great...

Query:

--Get date of first monday of the year
DECLARE @startDate datetime
select @startDate = '1/1/2003'

while (select DATENAME(dw, @startDate)) <> "Monday"
begin
select @startDate = (dateadd(day, 1, @startDate))
end

--Find out what week the date in the Date column is and
--add it to the first Monday of the year to get the date
--of that week
select
dateadd(wk,datepart(wk, Date),@startDate),
Explanation,
sum(TotalCount)
from
DSL_OrderHist
where
Date between '3/1/03' and '6/6/03'
group by
datepart(wk, Date),
Explanation

Sample of data returned:
3/10/03 Awaiting Disconnect 33
3/10/03 Cancel Completed 24
3/10/03 Disconnect Acknowledged Received 576
3/10/03 Disconnect Completed 64
3/17/03 Awaiting Disconnect 356
3/17/03 Cancel Completed 786
3/17/03 Disconnect Acknowledged Received 554
3/17/03 Disconnect Completed 3324
3/24/03 Awaiting Disconnect 56
3/24/03 Cancel Completed 874
3/24/03 Disconnect Acknowledged Received 343
3/24/03 Disconnect Completed 3567
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-08-2008, 02:40 PM
Anthony Mandic
 
Posts: n/a
Default Re: Need help with query

William wrote:
>
> I am trying to write a query that will capture data for a particular
> date range and then group the data by week to be graphed. The script
> below works well now but it will not be correct starting in January
> because I am adding the number of weeks for the date. So in January
> the dates in December will be around 50 so when I add that to the
> first Monday of the year it will come back with December of 2004
> instead of December 2003. I can not think of any way around this,
> does anyone have a better solution? Any help would be great...


I'm not sure if I understand you correctly, but it looks to me
that you just need to group by the year and the week of the
year. Something like datepart(yy, date) and datepart(wk, date).
That would give you results like -

2003, 7
2003, 9
2003, 37
3004, 1
etc.

Is that what you want?

-am © 2003
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-08-2008, 02:40 PM
nnnnnn
 
Posts: n/a
Default Re: Need help with query

search google for "characteristic encoded functions" - WARNING: they are
not very intuitive


"William" <william@williamcole.org> wrote in message
news:2975e5a1.0306300739.47ec0253@posting.google.c om...
> I am trying to write a query that will capture data for a particular
> date range and then group the data by week to be graphed. The script
> below works well now but it will not be correct starting in January
> because I am adding the number of weeks for the date. So in January
> the dates in December will be around 50 so when I add that to the
> first Monday of the year it will come back with December of 2004
> instead of December 2003. I can not think of any way around this,
> does anyone have a better solution? Any help would be great...
>
> Query:
>
> --Get date of first monday of the year
> DECLARE @startDate datetime
> select @startDate = '1/1/2003'
>
> while (select DATENAME(dw, @startDate)) <> "Monday"
> begin
> select @startDate = (dateadd(day, 1, @startDate))
> end
>
> --Find out what week the date in the Date column is and
> --add it to the first Monday of the year to get the date
> --of that week
> select
> dateadd(wk,datepart(wk, Date),@startDate),
> Explanation,
> sum(TotalCount)
> from
> DSL_OrderHist
> where
> Date between '3/1/03' and '6/6/03'
> group by
> datepart(wk, Date),
> Explanation
>
> Sample of data returned:
> 3/10/03 Awaiting Disconnect 33
> 3/10/03 Cancel Completed 24
> 3/10/03 Disconnect Acknowledged Received 576
> 3/10/03 Disconnect Completed 64
> 3/17/03 Awaiting Disconnect 356
> 3/17/03 Cancel Completed 786
> 3/17/03 Disconnect Acknowledged Received 554
> 3/17/03 Disconnect Completed 3324
> 3/24/03 Awaiting Disconnect 56
> 3/24/03 Cancel Completed 874
> 3/24/03 Disconnect Acknowledged Received 343
> 3/24/03 Disconnect Completed 3567



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-08-2008, 02:48 PM
Chuck Fox
 
Posts: n/a
Default Re: Need help with query

William,

If I understand your results correctly what you want to do is something
like this:

SELECT
DATEADD( dd, -1 * DATEPART( dw, Date ), Date ),
Explanation,
SUM( TotalCount )
FROM
DSL_History
WHERE
Date BETWEEN '3/1/03' AND '6/6/03'
GROUP BY
DATEADD( dd, -1 * DATEPART( dw, Date ), Date ),
Explanation,


Regards,

Chuck Fox

nnnnnn wrote:
> search google for "characteristic encoded functions" - WARNING: they are
> not very intuitive
>
>
> "William" <william@williamcole.org> wrote in message
> news:2975e5a1.0306300739.47ec0253@posting.google.c om...
>
>>I am trying to write a query that will capture data for a particular
>>date range and then group the data by week to be graphed. The script
>>below works well now but it will not be correct starting in January
>>because I am adding the number of weeks for the date. So in January
>>the dates in December will be around 50 so when I add that to the
>>first Monday of the year it will come back with December of 2004
>>instead of December 2003. I can not think of any way around this,
>>does anyone have a better solution? Any help would be great...
>>
>>Query:
>>
>>--Get date of first monday of the year
>>DECLARE @startDate datetime
>>select @startDate = '1/1/2003'
>>
>>while (select DATENAME(dw, @startDate)) <> "Monday"
>>begin
>> select @startDate = (dateadd(day, 1, @startDate))
>>end
>>
>>--Find out what week the date in the Date column is and
>>--add it to the first Monday of the year to get the date
>>--of that week
>>select
>> dateadd(wk,datepart(wk, Date),@startDate),
>> Explanation,
>> sum(TotalCount)
>>from
>> DSL_OrderHist
>>where
>> Date between '3/1/03' and '6/6/03'
>>group by
>> datepart(wk, Date),
>> Explanation
>>
>>Sample of data returned:
>>3/10/03 Awaiting Disconnect 33
>>3/10/03 Cancel Completed 24
>>3/10/03 Disconnect Acknowledged Received 576
>>3/10/03 Disconnect Completed 64
>>3/17/03 Awaiting Disconnect 356
>>3/17/03 Cancel Completed 786
>>3/17/03 Disconnect Acknowledged Received 554
>>3/17/03 Disconnect Completed 3324
>>3/24/03 Awaiting Disconnect 56
>>3/24/03 Cancel Completed 874
>>3/24/03 Disconnect Acknowledged Received 343
>>3/24/03 Disconnect Completed 3567

>
>
>


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:23 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com