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!
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?
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…
This will now give you unique rows for each
task.idwith 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: