Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3676676
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:07:41+00:00 2026-05-19T03:07:41+00:00

I have created a report that gets week-ending figures for a given account number.

  • 0

I have created a report that gets week-ending figures for a given account number. I used the datepart function to group by week, which was fine, but a ‘bug’ (quoted because I’m not sure if it’s a bug or correct behavour, but I didn’t want the behavour) would occur at the end of the year. The grouping would split the last week in two, and show it as two rows (both with the same week end date, but one was week 53, and the other week 1) So I modified the group by clause accordingly…

    group by 

(case when datepart(wk,dbdate) = 53 then (year(dbdate) + 1) else year(dbdate) end)

,(case when datepart(wk,dbdate) = 53 then 1 else datepart(wk,dbdate) end)

It seems to work. And I tested it out with a few examples. But I just want to be sure that a) it will work in all conditions and b) is there a better way?

If it helps, here is the entire query…

set datefirst 1
select 
convert(varchar,min(DATEADD(dd, 7-(DATEPART(dw, dbdate)), dbdate)),106) [Week<br />Ending],
'$' + convert(VARCHAR,sum(handle),1) as Handle,

'$' + convert(varchar,sum(case when pool_type in ('WN','PL','SH','WP','WS','PS','WPS') then handle else 0 end),1) as WPS,

'$' + convert(varchar,sum(case when pool_type not in ('WN','PL','SH','WP','WS','PS','WPS') then handle else 0 end),1) as Exotics

 from amtoteaccountactivity 

where accountnumber ='$account'
and (_date >='$datestart' and _date <= '$dateend') 
and pool_type not in ('TT2','TS2','BQ2','SS2') and transaction_type ='Bet'
group by 

(case when datepart(wk,dbdate) = 53 then (year(dbdate) + 1) else year(dbdate) end)

,(case when datepart(wk,dbdate) = 53 then 1 else datepart(wk,dbdate) end)


order by min(DATEADD(dd, 7-(DATEPART(dw, dbdate)), dbdate))

Edit: what this essentially does is combine week 53’s results with week 1 of the following year’s results, thus recombining the split week. Because the SQL engine is counting the last week of the first year as ‘week 1’ of the following year AND week 53 of the preceding year.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T03:07:42+00:00Added an answer on May 19, 2026 at 3:07 am

    we use another table to join; in the other table we have the calendarweek (both iso and us), quarter, month etc.
    the join is done with the date-part only. group by is then done via one of the additionalcolumns, that depends on the report (by week, by month etc)

    the other table is filled one time with values for 100 years, thats enough for our requirements.

    table schema:

    CREATE TABLE [dbo].[PeriodeDate](
    [Id] [int] NOT NULL IDENTITY(1, 1),
    [periodeDate] [datetime] NOT NULL,
    [periodQuarter] [int] NULL,
    [periodYear] [int] NULL,
    [periodMonth] [int] NULL,
    [calendarWeekISO] [int] NULL,
    [YearOfCalendarWeekISO] [int] NULL,
    [YearOfCalendarWeekUS] [int] NULL,
    [calendarWeekUS] [int] NULL,
    )
    

    and keep in mind, there are years which have 53! calendarweeks (both, ISO and US)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a report in SSRS that contains 2 matrices (not tables/lists), each
I have a report that I created with Crystal Reports 2008. This report uses
I have two report parameters that were set up automatically when I created their
I have created a .Net application to run on an App Server that gets
I have created a report in MS Access report and write some VBA code
in .net I have created an excel report which takes a datagrid's render output
I have created a one-time subscription in SSRS report manager 2008. However I keep
I have a report that uses a TChart that I am maintaining. One of
I have an access 2007 Database that outputs a report in excel format, the
I am trying to troubleshoot a particular 'report' that gets generated on a PHP

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.