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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:08:39+00:00 2026-05-31T05:08:39+00:00

I am trying to write an SQL (Server) query which will return all events

  • 0

I am trying to write an SQL (Server) query which will return all events on a current day, and for all events where the column recurring= 1, I want it to return this event on the day it is being held and for the subsequent 52 weeks following the event.

My tables are structured as followed :

Event
{
    event_id (PK)
    title,
    description,
    event_start DATETIME,
    event_end DATETIME,
    group_id,
    recurring
}

Users
{
    UserID (PK)
    Username
}

Groups
{
    GroupID (PK)
    GroupName
}

Membership
{
    UserID (FK)
    GroupID (FK)
}

The code I have thus far is as follows :

     var db = Database.Open("mPlan");
    string username = HttpContext.Current.Request.Cookies.Get("mpUsername").Value;
    var listOfGroups = db.Query("SELECT GroupID FROM Membership WHERE UserID = (SELECT UserID from Users WHERE Username = @0 )",  username);
    foreach(var groupID in listOfGroups)
        {
            int newGroupID = groupID.GroupID;
            var result = db.Query(
                @"SELECT e.event_id, e.title, e.description, e.event_start, e.event_end, e.group_id, e.recurring
                FROM   event e
                JOIN   Membership m ON m.GroupID = e.group_id
                WHERE  e.recurring = 0
                AND    m.GroupID = @0
                AND    e.event_start >= @1
                AND    e.event_end <= @2
                UNION ALL
                SELECT e.event_id, e.title, e.description, DATEADD(week, w.weeks, e.event_start), DATEADD(week, w.weeks, e.event_end), e.group_id, e.recurring
                FROM   event e
            JOIN   Membership m ON m.GroupID = e.group_id
            CROSS JOIN 
                ( SELECT  row_number() OVER (ORDER BY Object_ID) AS weeks
                FROM SYS.OBJECTS
                ) AS w
                WHERE  e.recurring = 1
                AND    m.GroupID = @3
                AND DATEADD(WEEK, w.Weeks, e.event_start) >= @4 
                AND DATEADD(WEEK, w.Weeks, e.event_end) <= @5", newGroupID, start, end, newGroupID, start, end
            );

This results in when one queries for the date of the event stored in the database, this event and 52 weeks of events are returned. When one queries for the event the week after this one, nothing is returned.

  • 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-31T05:08:40+00:00Added an answer on May 31, 2026 at 5:08 am

    The simplest solution would be to alter the following 2 lines

    AND    e.event_start >= @4
    AND    e.event_end <= @5"
    

    to

    AND    DATEADD(WEEK, w.Weeks, e.event_start) >= @4
    AND    DATEADD(WEEK, w.Weeks, e.event_end) <= @5"
    

    However, I’d advise putting all this SQL into a stored procedure, SQL-Server will cache the execution plans and it will result in (slightly) better performance.

    CREATE PROCEDURE dbo.GetEvents @UserName VARCHAR(50), @StartDate DATETIME, @EndDate DATETIME
    AS
    BEGIN
    -- DEFINE A CTE TO GET ALL GROUPS ASSOCIATED WITH THE CURRENT USER
    ;WITH Groups AS 
    (   SELECT  GroupID 
        FROM    Membership  m
                INNER JOIN Users u
                    ON m.UserID = u.UserID
        WHERE   Username = @UserName
        GROUP BY GroupID
    ),
    -- DEFINE A CTE TO GET ALL EVENTS FOR THE GROUPS DEFINED ABOVE
    AllEvents AS
    (   SELECT  e.*
        FROM    event e
                INNER JOIN Groups m 
                    ON m.GroupID = e.group_id
        UNION ALL
        SELECT  e.event_id, e.title, e.description, DATEADD(WEEK, w.weeks, e.event_start), DATEADD(WEEK, w.weeks, e.event_end), e.group_id, e.recurring
        FROM    event e
                INNER JOIN Groups m 
                    ON m.GroupID = e.group_id
                CROSS JOIN 
                (   SELECT  ROW_NUMBER() OVER (ORDER BY Object_ID) AS weeks
                    FROM    SYS.OBJECTS
                ) AS w
        WHERE  e.recurring = 1
    )   
    -- GET ALL EVENTS WHERE THE EVENTS FALL IN THE PERIOD DEFINED
    SELECT  *
    FROM    AllEvents
    WHERE   Event_Start >= @StartDate
    AND     Event_End <= @EndDate
    
    END
    

    Then you can call this with

    var result = db.Query("EXEC dbo.GetEvents @0, @1, @2", username, start, end);
    

    This elimates the need to iterate over groups in your code behind. If this is actually a requirement then you could modify the stored procedure to take @GroupID as a parameter, and change the select statements/where clauses as necessary.

    I have assumed knowledge of Common Table Expressions. They are not required to make the query work, they just make things slightly more legible in my opinion. I can rewrite this without them if required.

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

Sidebar

Related Questions

I am trying to write a query for SQL Server 2005 but I can't
I'm trying to write a Microsoft SQL Server 2005 query that counts a total
In Sql server, i am trying to write a query, if the value is
I am trying to write a SQL Server query but have had no luck
I am trying to write a SQL query (for SQl Server), and am curious
I am trying to write my SQL Server 2008 query in such a way
I am trying to write a recursive CTE query in SQL Server 2005 but
I am trying to write a Microsoft SQL Server query for retrieving the oldest
I'm trying to write a recursive query in SQL Server that basically lists a
I am trying to write some integration tests for some SQL server stored procedures

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.