2

I have this table:

TableNumber(Int 0 to 25)|Name(varchar 100)|Project(varchar 15)|StartDate(Datetime)
1                       |David            |P1                 |'2015-02-06 08:00:00'    
2                       |Sebastien        |P2                 |'2015-02-06 08:00:00'    
1                       |David            |P4                 |'2015-02-06 12:00:00'    
2                       |Sebastien        |P3                 |'2015-02-07 08:00:00'

And I am looking to get the latest job for each person on a set day.

I would like to have:

TableNumber(Int 0 to 25)|Name(varchar 100)|Project(varchar 15)|StartDate(Datetime)    
2                       |Sebastien        |P2                 |'2015-02-06 08:00:00'    
1                       |David            |P4                 |'2015-02-06 12:00:00'   

So I want to exclude P3 since its not '2015-02-06' and I want to exclude P1 cause its not the latest job for David (its P4).

Please consider that NOW() returns '2015-02-06 15:00:00' in the following exemples.

Here is what I tried:

SELECT * FROM MyTable WHERE DATEDIFF(startdate, NOW()) = 0 ORDER BY tablenum DESC;

But this only excluded P3

So I tried this instead:

SELECT * FROM MyTable AS p WHERE DATEDIFF(p.startdate, NOW()) = 0 AND TIMEDIFF(p.startdate, NOW()) = (SELECT MAX(TIMEDIFF(p2.startdate, NOW())) FROM MyTable AS p2 WHERE p2.startdate = p.startdate) ORDER BY tablenum DESC;

But Its still doesn't exclude P1.

Anyone know how I could achieve this? BTW startdate will always be a round hour (08:00:00 or 12:00:00 or 22:00:00...)

UPDATE

Yeah since it wasn't very clear what I wanted I will clarify here:

I need to know the last project worked on by every person.

so in my table I need to know that Sebastien work on P2 on table number 2 and that David work on P4 on table number 1. I don't want P1 because its not the last project that David worked on (by last project I also include the project he is working on right now). I also want to rule out everything in the future so P3 (who is tomorrow) must not display.

Sebastien
  • 1,308
  • 2
  • 15
  • 39
  • possible duplicate of [SQL Select only rows with Max Value on a Column](http://stackoverflow.com/questions/7745609/sql-select-only-rows-with-max-value-on-a-column) – Conspicuous Compiler Feb 06 '15 at 20:51
  • `select user, max(startdate) ... group by user`? – Marc B Feb 06 '15 at 20:51
  • By latest, I understood the the "most late" project (from the context of your question) – Tarik Feb 06 '15 at 21:10
  • On a set day, do you mean that as of a given date/time what were the jobs in incurred the most delays for each name? Or do you mean to find the earliest job on a given day for each name? – Tarik Feb 06 '15 at 21:13
  • Your name and your spelling of "example" seems to indicate you are French. – Tarik Feb 06 '15 at 21:15
  • @Tarik Yeah sorry for the spelling errors... My english is a bit rusty since I don't live in the english speaking part of Canada anymore – Sebastien Feb 06 '15 at 21:28
  • @Sebastien Have been there :-) – Tarik Feb 06 '15 at 21:55

1 Answers1

3

The following query will provide you with the date/time of the earliest job for a given name on a given day. In the following example I assumed you want the earliest jobs of each user on 2015-02-06.

SELECT Name, min(StartDate)
FROM MyTable
WHERE StartDate >= '2015-02-06'
  AND StartDate < '2015-02-07'
GROUP BY Name

Using the above query, you can trivially get the final solution:

SELECT t1.project, t2.name, t2.StartDate
FROM MyTable t1 INNER JOIN
   (SELECT Name, min(StartDate)
FROM MyTable
WHERE StartDate >= '2015-02-06'
  AND StartDate < '2015-02-07'
GROUP BY Name) t2 ON t1.Name = t2.Name AND t1.StartDate = t2.StartDate
Tarik
  • 10,810
  • 2
  • 26
  • 40
  • You need to return the project name too – Sasanka Panguluri Feb 06 '15 at 20:57
  • 1
    @Tarik Well actually your first query worked just fine I changed the MIN for MAX and everything seams to work properly – Sebastien Feb 06 '15 at 21:07
  • @Tarik Here is the final query that works: 'SELECT tablenum, name, project, MAX(StartDate) AS date FROM MyTable WHERE DATEDIFF(startdate, NOW()) = 0 GROUP BY name order by tablenum desc; – Sebastien Feb 06 '15 at 21:10
  • Clarifying your question will help me revise my answer to meet your needs. – Tarik Feb 06 '15 at 21:20
  • @Sebastien: Using DATEDIFF will prevent an index from being used on the WHERE clause, forcing a full table scan every time this query is run. See http://stackoverflow.com/questions/2547316/is-sql-datediffyear-an-expensive-computation You should either add logic to generate less-than-greater-than logic like Tarik used above or add a column to your table that is only the date part of the timestamp and search for an exact match. Otherwise, you will see scalability problems going forward. – Conspicuous Compiler Feb 06 '15 at 21:52