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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:57:56+00:00 2026-06-09T11:57:56+00:00

I’m having trouble formulating a SQL query. I run the following: SELECT * FROM

  • 0

I’m having trouble formulating a SQL query. I run the following:

SELECT *
FROM tasks
LEFT JOIN plans
ON plans.task_id = tasks.id

and get this result set:

task.id task.description  plan.id plan.task_id plan.date
-------|-----------------|-------|------------|------------
   1    Foo                  1         1        1998-01-01
   2    Foobar               2         2        2012-02-25
   2    Foobar               3         2        2012-12-12
   3    Foobass              4         3        2012-12-24
   4    Bassbar
                       ... and lots of more records

Today is 2012-08-03. I want all tasks with the following condition: the task have never been planed or the task have been planed in the past but have no future plans.

In the example above the following tasks meet this condition:

  • 1 Foo
  • 4 Bassbar

Any suggestions? Thanks in advance!

  • 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-09T11:57:57+00:00Added an answer on June 9, 2026 at 11:57 am
    SELECT *
    FROM tasks
    LEFT JOIN plans
    ON plans.task_id = tasks.id
    WHERE plan.week IS NULL OR (plan.week < DATEPART(week, GetDate()) AND plan.Year = YEAR(GETDATE())
    

    You should not use SELECT *, explicitly write out each column you need. You didn’t mention if you had a year column as well, you’ll have to handle the week number and the year. If you just handle the week # you’ll get results across multiple years.

    It sounds like you can just use task description??? Why are you basing it on the week column if you can use the task description?

    SELECT *
    FROM tasks
    LEFT JOIN plans
    ON plans.task_id = tasks.id
    WHERE tasks.description = 'Past' OR tasks.description = 'Not planned'
    

    Aha I understand your data now, you could have multiple tasks but on different week numbers. This is simple, just use a query to find the MAX(Week#) GROUP BY the task and then perform the query, Ill write it up give me a minute…

      CREATE TABLE #MyTest
    (
     TaskID int,
     TaskYear int,
     TaskWeek int
    )
    
    INSERT INTO #MyTest(TaskID,TaskYear,TaskWeek)
    VALUES (1, 2012, 4)
    INSERT INTO #MyTest(TaskID,TaskYear,TaskWeek)
    VALUES (2, 2012, 5)
    INSERT INTO #MyTest(TaskID,TaskYear,TaskWeek)
    VALUES (2, 2012, 36)
    INSERT INTO #MyTest(TaskID,TaskYear,TaskWeek)
    VALUES (3, 2012, 36)
    INSERT INTO #MyTest(TaskID,TaskYear,TaskWeek)
    VALUES (4, 2012, NULL)
    INSERT INTO #MyTest(TaskID,TaskYear,TaskWeek)
    VALUES (6, 2011, 5)
    INSERT INTO #MyTest(TaskID,TaskYear,TaskWeek)
    VALUES (6, 2010, 36)
    
    SELECT
        TaskID,
        MAX(TaskWeek) AS WeekNumber,
        TaskYear
    FROM
      #MyTest
    GROUP BY 
        TaskID,
        TaskWeek,
        TaskYear
    HAVING MAX(TaskWeek) < DatePart(week, GetDate()) OR MIN(TaskWeek) IS NULL
    
    DROP TABLE #MyTest
    

    This will now give you unique rows for each task.id with the max week number.

    Final Result

    Based on your final comments:

    Copy and paste this into sql management studio, I have commented it for you:

    CREATE TABLE #MyTest
    (
     TaskID int,
     TaskDate datetime
    )
    
    --test for only in the past NOTHING in the future
    INSERT INTO #MyTest(TaskID, TaskDate)
    VALUES (1, '1998-01-01')
    
    --test for planned in the future NOTHING in the past
    INSERT INTO #MyTest(TaskID,TaskDate)
    VALUES (3, '2012-12-24')
    
    --test for no plan as all (IS NULL)
    INSERT INTO #MyTest(TaskID,TaskDate)
    VALUES (4, null)
    
    --test for planned in the past but has an upcoming event in the future
    INSERT INTO #MyTest(TaskID,TaskDate)
    VALUES (6, '2011-12-23')
    INSERT INTO #MyTest(TaskID,TaskDate)
    VALUES (6, '2012-12-23')
    INSERT INTO #MyTest(TaskID,TaskDate)
    
    --test for planned in the past, NO upcoming event in the future
    VALUES (8, '2012-1-23')
    INSERT INTO #MyTest(TaskID,TaskDate)
    VALUES (8, '2012-6-23')
    
    --result should show:
    -- task id = 1 (because of: performed in past but nothing in the future)
    -- task id = 4 (because of: no plan at all) 
    -- task id = 8 (because of: only past)
    
    SELECT
        TaskID,
        YEAR(TaskDate) AS TheYear,
        DatePart(week, TaskDate) AS WeekNumber
    FROM
      #MyTest
    WHERE 
    --handle no planning of a task...
    ((TaskDate IS NULL)
    --eliminate any task id that is out in the future
    OR TaskID NOT IN (SELECT TaskID FROM #MyTest WHERE TaskDate > GetDate()))
    GROUP BY
        TaskID,
        Year(TaskDate),
        DatePart(week, TaskDate)
    
    DROP TABLE #MyTest
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string

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.