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 8896295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:00:23+00:00 2026-06-15T00:00:23+00:00

I have the code below created that provides me with a count of unique

  • 0

I have the code below created that provides me with a count of unique users broken down by company. I would like to add a date modifier that would have two columns, one for “Last Week” and one for “2 Weeks Ago”. I would like these columns to respectively show active users for the previous week’s dates and the week prior to that based off the field “users.date”. Any help would be fantastic. I am using SQL Server Management Studio 2008 R2.

 --Active Users
    SELECT company.companyName as 'Group Name',
    COUNT(distinct users.userid) [Count]
    FROM Users, Company 
    WHERE 
    Jobstate = '6'
    and company.companyID = users.companyid
    and company.companyID in (1,4,31)
    GROUP BY company.companyName

Currently I am receiving this as a result:

Group Name | Count     |
------------------------
Company 1  | 104       |
Company 2  | 74        |
Company 3  | 46        |

What I would like to see would be:

Group Name | Last Week | 2 Weeks Ago |
--------------------------------------
Company 1  | 14        | 16          |
Company 2  | 7         | 12          |
Company 3  | 4         | 8           |
  • 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-15T00:00:24+00:00Added an answer on June 15, 2026 at 12:00 am

    For SQL Server it would look something like this.

    SELECT
      Company.CompanyName AS [Group Name],
      COUNT(DISTINCT Users.UserId) AS [Count],
      SUM (CASE 
             WHEN DATEDIFF(day, Users.Date, GETDATE()) < 7 THEN 1
             ELSE 0
           END
          ) AS [Last Week]
      -- Additional columns for last 2 weeks, etc.
    FROM Users, Company 
    
    WHERE 
      Jobstate = '6'
      AND Company.CompanyId = Users.CompanyId
      AND Company.CompanyId in (1, 4, 31)
    GROUP BY
      Company.CompanyName
    

    EDIT: The query above counts users, not distinct users. If you want distinct users for the last week it can be done very simply by modifying your original query.

    SELECT
      Company.CompanyName AS [Group Name],
      COUNT(DISTINCT Users.UserId) AS [Last Week]
    FROM Users, Company 
    
    WHERE 
      Jobstate = '6'
      AND Company.CompanyId = Users.CompanyId
      AND Company.CompanyId in (1, 4, 31)
      AND DATEDIFF(day, Users.Date, GETDATE()) < 7
    GROUP BY
      Company.CompanyName
    

    If you want to combine the two so you have all the columns in a single dataset it can be done by combining the two approaches like this:

    SELECT
      Company.CompanyName AS [Group Name],
      COUNT(DISTINCT Users.UserId) AS [Count],
      COUNT (DISTINCT
               CASE 
                 WHEN DATEDIFF(day, Users.Date, GETDATE()) < 7 THEN Users.UserId
                 ELSE NULL
               END
            ) AS [Last Week]
      -- Additional columns for last 2 weeks, etc.
    FROM Users, Company 
    
    WHERE 
      Jobstate = '6'
      AND Company.CompanyId = Users.CompanyId
      AND Company.CompanyId in (1, 4, 31)
    GROUP BY
      Company.CompanyName
    

    SQLFiddle here

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

Sidebar

Related Questions

I have code that looks more or less like the code below but it
I just have the below code that was provides as hMailServer's DCOM API at
In my code I have 2 ComboBox DropDownLists, created by the code below. The
I have a dialog box, created with the code javascript and HTML generated below.
I've created buttons that have been edited with css to look like hyperlinks so
I have a working JavaScript code below which dynamically creates JSON object using JSON.parse
I have code below: <select id=testSelect> <option value=1>One</option> <option value=2>Two</option> </select> <asp:Button ID=btnTest runat=server
I have code as below: var s : String = hello world var xml
I have the code below. It looks long and complicated and now I have
I have the code below. void *timer1_function(void * eit); pthread_t timer1; int thread_check1 =

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.