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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:29:36+00:00 2026-05-27T02:29:36+00:00

I have the following two tables activity(activity_id, title, description, group_id) statistic(statistic_id, activity_id, date, user_id,

  • 0

I have the following two tables

activity(activity_id, title, description, group_id)
statistic(statistic_id, activity_id, date, user_id, result)

group_id and user_id come from active directory. Result is an integer.

Given a user_id and a date range of 6 days (Mon – Sat) which I’ve calculated on the business logic side, and the fact that some of the dates in the date range may not have a statistic result for the particular date (ie. day1 and day 4 may have entered statistic rows for a particular activity, but there may not be any entries for days 2, 3, 5 and 6) how can I get a SQL result with the following format? Keep in mind that if a particular activity doesn’t have a record for the particular date in the statistics table, then that day should return 0 in the SQL result.

activity_id    group_id    day1result    day2result    day3result    day4result    day5result    day6 result
-----------    --------    ----------    ----------    ----------    ----------    ----------    -----------
sample1        Secured     0             5             1             0             2             1
sample2        Unsecured   1             0             0             4             3             2

Note: Currently I am planning on handling this in the business logic, but that would require multiple queries (one to create a list of distinct activities for that user for the date range, and one for each activity looping through each date for a result or lack of result, to populate the 2nd dimension of the array with date-related results). That could end up with 50+ queries for each user per date range, which seems like overkill to me.

I got this working for 4 days and I can get it working for all 6 days, but it seems like overkill. Is there a way to simplify this?:

SELECT d1d2.activity_id, ISNULL(d1d2.result1,0) AS day1, ISNULL(d1d2.result2,0) AS day2, ISNULL(d3d4.result3,0) AS day3, ISNULL(d3d4.result4,0) AS day4
FROM
    (SELECT ISNULL(d1.activity_id,0) AS activity_id, ISNULL(result1,0) AS result1, ISNULL(result2,0) AS result2
     FROM
         (SELECT ISNULL(statistic_result,0) AS result1, ISNULL(activity_id,0) AS activity_id
          FROM statistic 
              WHERE user_id='jeremiah' AND statistic_date='11/22/2011'
          ) d1
          FROM JOIN
          (SELECT ISNULL(statistic_result,0) AS result2, ISNULL(activity_id,0) AS activity_id
           FROM statistic WHERE user_id='jeremiah' AND statistic_date='11/23/2011'
          ) d2
          ON d1.activity_id=d2.activity_id
     ) d1d2
     FULL JOIN
     (SELECT d3.activity_id AS activity_id, ISNULL(d3.result3,0) AS result3, ISNULL(d4.result4,0) AS result4
      FROM
          (SELECT ISNULL(statistic_result,0) AS result3, ISNULL(activity_id,0) AS activity_id
           FROM statistic WHERE user_id='jeremiah' AND statistic_date='11/24/2011'
          ) d3
          FULL JOIN
          (SELECT ISNULL(statistic_result,0) AS result4, ISNULL(activity_id,0) AS activity_id
           FROM statistic WHERE user_id='jeremiah' AND statistic_date='11/25/2011'
          ) d4
          ON d3.activity_id=d4.activity_id
     ) d3d4
     ON d1d2.activity_id=d3d4.activity_id
ORDER BY d1d2.activity_id
  • 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-27T02:29:37+00:00Added an answer on May 27, 2026 at 2:29 am

    Here is a typical approach for this kind of thing:

    DECLARE @minDate DATETIME,
            @maxdate DATETIME,
            @userID VARCHAR(200)
    
    SELECT  @minDate = '2011-11-15 00:00:00',
            @maxDate = '2011-11-22 23:59:59',
            @userID = 'jeremiah'
    
    SELECT A.activity_id, A.group_id, 
            SUM(CASE WHEN DATEDIFF(day, @minDate, S.date) = 0 THEN S.Result ELSE 0 END) AS Day1Result,
            SUM(CASE WHEN DATEDIFF(day, @minDate, S.date) = 1 THEN S.Result ELSE 0 END) AS Day2Result,
            SUM(CASE WHEN DATEDIFF(day, @minDate, S.date) = 2 THEN S.Result ELSE 0 END) AS Day3Result,
            SUM(CASE WHEN DATEDIFF(day, @minDate, S.date) = 3 THEN S.Result ELSE 0 END) AS Day4Result,
            SUM(CASE WHEN DATEDIFF(day, @minDate, S.date) = 4 THEN S.Result ELSE 0 END) AS Day5Result,
            SUM(CASE WHEN DATEDIFF(day, @minDate, S.date) = 5 THEN S.Result ELSE 0 END) AS Day6Result
    FROM activity A 
        LEFT OUTER JOIN statistic S 
            ON A.activity_id = S.activity_ID
                AND S.user_id = @userID
    WHERE S.date between @minDate AND @maxDate 
    GROUP BY A.activity_id, A.group_id 
    

    First, I’m using group by to reduce the resultset to one row per activity_id/group_id, then I’m using CASE to separate values for each individual column. In this case I’m looking at which day in the last seven, but you can use whatever logic there to determine what date. The case statements will return the value of S.result if the row is for that particular day, or 0 if it’s not. SUM will add up the individual values (or just the one, if there is only one) and consolidate that into a single row.

    You’ll also note my date range is based on midnight on the first day in the range and 11:59PM on the last day of the range to ensure all times are included in the range.

    Finally, I’m performing a left join so you will always have a 0 in your columns, even if there are no statistics.

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

Sidebar

Related Questions

I have following two tables Table name: thread pk-> thread_id | thread_title | description
I have these following two tables: Job Title | PostDate | CompanyId Assitant |
I have the following two tables (basic outline): Tbl_CategoryType ID LevelID Description Tbl_Levels ID
I have the following two tables, affiliates and referrers. affiliates Table id loginid 3
I have the following two tables: Customer { int Id int Name } Bills
I have the following two tables: Table1 ---------- ID Name 1 A 2 B
1I have the following two tables (sample data) and need to be able to
I have the following two tables: auth_user id username is_active (boolean) ... useprofile_userprofile id
Say for example, I have the following two tables: TableA { _id } TableB
I am using VB.NET with LINQ to MS SQL. I have two following tables.

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.