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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:35:17+00:00 2026-05-28T19:35:17+00:00

I have this query that returns the correct data, but I would like to

  • 0

I have this query that returns the correct data, but I would like to filter it.

SELECT     TOP (100) PERCENT dbo.Reg_Master.id, dbo.Cart_Programs.cartid, dbo.Reg_Master.F_ID,   dbo.BlockPeriod.profileid, dbo.Reg_Master.FirstName, 
                      dbo.Reg_Master.LastName, dbo.BlockPeriod.startdate, dbo.Cart_Programs.blockid
FROM         dbo.Cart_Programs LEFT OUTER JOIN
                      dbo.Reg_Master ON dbo.Cart_Programs.cartid = dbo.Reg_Master.cartid LEFT OUTER JOIN
                      dbo.BlockPeriod ON dbo.Cart_Programs.blockid = dbo.BlockPeriod.id
WHERE     (dbo.BlockPeriod.profileid = xxx) AND (dbo.Reg_Master.F_ID = xxxx)
ORDER BY dbo.BlockPeriod.startdate

For each dbo.Reg_Master.id, I would like to return only the earliest dbo.BlockPeriod.startdate (that is today or later – in other words ignoring dates that have already passed) for each dbo.Reg_Master.id, I cannot seem to get it formatted correctly.

  • 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-28T19:35:18+00:00Added an answer on May 28, 2026 at 7:35 pm

    First of all, TOP 100 Percent does nothing, the optimizer will just ignore it.
    Also, your left joins do not serve any purpose because your WHERE condition, so I have edited the SQL to use an inner join + cross apply vs an outer join + outer apply.

    If I understand you correctly for each Reg_Master record, you want at most 1 record from BlockPeriod, where that 1 record is the closest date that is greater than today’s date.

    If so, then what you are looking for is an APPLY table operator combined with TOP (1) as shown below:

    UPDATED:

    SELECT Reg_Master.id, 
           Cart_Programs.cartid, 
           Reg_Master.F_ID,   
           T.profileid, 
           Reg_Master.FirstName, 
           Reg_Master.LastName, 
           T.startdate, 
           Cart_Programs.blockid
    FROM Cart_Programs 
    JOIN Reg_Master  ON Cart_Programs.cartid  = Reg_Master.cartid
    CROSS APPLY(
       SELECT TOP 1 * FROM BlockPeriod
        WHERE BlockPeriod.id = Cart_Programs.blockid
          AND BlockPeriod.profileid = xxx AND Reg_Master.F_ID = xxxx 
          AND BlockPeriod.startdate >= GETDATE()
     ORDER BY BlockPeriod.startdate ASC 
    ) AS T
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a LINQ query that returns some object like this... var query =
I have an NHibernate query that looks like this: var query = Session.CreateQuery(@ select
I have a query that originally looks like this: select c.Id, c.Name, c.CountryCode, c.CustomerNumber,
I have a query like the following that returns the correct number of rows
So I have this query that works perfectly: SELECT users.*, GROUP_CONCAT(categories.category_name) AS categories FROM
I have a Linq query that looks something like this: var query = from
I have this linq query that works well (although it may be written better,
This query that I have is returning therapists whose 'therapistTable.activated' is equal to false
I have this query to fetch the total OrderStatus that have values 1 and
I have this query statement and want to only get records that has a

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.