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

  • SEARCH
  • Home
  • 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 9243931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:54:52+00:00 2026-06-18T08:54:52+00:00

I have this USERS table with users that can be of two different types

  • 0

I have this USERS table with users that can be of two different types (A and B). I need to show a report with the aggregate per type for each week. The query I have so far works well except some weeks are not grouping properly. In the example below, the week starting Jan 28th should have one line, not two.

Week Starts |Week| Type A | Type B
------------+----+--------+------
2013-02-04  | 14 |  2     | 26
2013-01-28  | 13 |  5     | 191
2013-01-28  | 13 |  0     | 24
2013-01-21  | 12 |  1     | 134
2013-01-21  | 12 |  0     | 20
2013-01-14  | 11 |  1     | 143
2013-01-14  | 11 |  0     | 2
2013-01-07  | 10 |  0     | 233
2013-01-07  | 10 |  0     | 23
2012-12-31  | 9  |  0     | 12
2012-12-31  | 9  |  4     | 164
2012-12-31  | 9  |  0     | 20

SQL

;with cte as
(
select DATEADD(m,-3,GETDATE()) firstday, DATEADD(m,-3,GETDATE()) + 6 - DATEDIFF(day, 0, DATEADD(m,-3,GETDATE())) %7 lastday,  1 week
union all
select lastday + 1, case when GETDATE() < lastday + 7 then GETDATE() else lastday + 7 end,  week + 1
from cte
where lastday < GETDATE()
)
SELECT
    cast(firstday as date) 'Week Starts',
    cte.week as 'Week',
    Sum(CASE WHEN USR_TYPE = 'A' THEN 1 ELSE 0 END) As 'Type A',
    Sum(CASE WHEN USR_TYPE = 'B' THEN 1 ELSE 0 END) As 'Type B'
FROM cte left join USERS
ON cte.firstday <= USERS.CREATED
AND cte.lastday > USERS.CREATED
GROUP BY cte.week, cte.firstday, cte.lastday, DATEPART(YEAR,USERS.CREATED), DATEPART(wk,USERS.CREATED)
ORDER BY week desc

What am I doing wrong?

  • 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-06-18T08:54:54+00:00Added an answer on June 18, 2026 at 8:54 am

    Without seeing any data from your users table I am going to take a guess.

    The list of dates you are generating in the CTE includes the time.

    You might need to cast() your firstday and lastday values as either a date or generate the list with no time.

    See a SQL Fiddle Demo

    Sample from your CTE and the new dates cast:

    | CASTFIRSTDAY | CASTLASTDAY | WEEK |                        FIRSTDAY |                         LASTDAY |
    ---------------------------------------------------------------------------------------------------------
    |   2012-11-05 |  2012-11-11 |    1 | November, 05 2012 20:08:10+0000 | November, 11 2012 20:08:10+0000 |
    |   2012-11-12 |  2012-11-18 |    2 | November, 12 2012 20:08:10+0000 | November, 18 2012 20:08:10+0000 |
    |   2012-11-19 |  2012-11-25 |    3 | November, 19 2012 20:08:10+0000 | November, 25 2012 20:08:10+0000 |
    |   2012-11-26 |  2012-12-02 |    4 | November, 26 2012 20:08:10+0000 | December, 02 2012 20:08:10+0000 |
    |   2012-12-03 |  2012-12-09 |    5 | December, 03 2012 20:08:10+0000 | December, 09 2012 20:08:10+0000 |
    |   2012-12-10 |  2012-12-16 |    6 | December, 10 2012 20:08:10+0000 | December, 16 2012 20:08:10+0000 |
    

    You might want to edit your CTE to return the date only values:

    ;with cte as
    (
        select 
            cast(DATEADD(m,-3,GETDATE()) as date) firstday, 
            cast(DATEADD(m,-3,GETDATE()) + 6 - DATEDIFF(day, 0, DATEADD(m,-3,GETDATE())) %7 as DATE) lastday,  
            1 week
        union all
        select 
            cast(DATEADD(DAY, 1, lastday) as date), 
            case 
                when cast(GETDATE() as date) < cast(DATEADD(DAY, 7, lastday) as date)
                then cast(GETDATE() as date) 
                else cast(DATEADD(DAY, 7, lastday) as date)
            end,  
            week + 1
        from cte
        where cast(lastday as date)  < cast(GETDATE() as date) 
    )
    select *
    from cte
    

    See SQL Fiddle with Demo

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

Sidebar

Related Questions

I have two systems that produce different types of messages that I need to
I have a system where users can complete two types of tests. I need
I have a table which looks like this: mysql> SHOW COLUMNS FROM Users; +------------+--------------+------+-----+---------+----------------+
I have a schema design that looks like this: Table: User Row Column Family
I have a 'users' SQL table structure like this (the ID is randomly generated
I have a 'users' SQL table structure like this (the ID is randomly generated,
Lets say I have a MySQL table, Users, like this - ----------------------------------------- ID |
I have this Points table: =========================================== FIELD : TYPE =========================================== id int(11) user_id int(11)
tl;dr: I need to show data from two different tables in the list view
I have two different forms on one page that dynamically generate inputs. One is

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.