Domanda

I'm trying to make a summary making a indicator if any data in the SELECT past day from now... or just show a day (if > 0 : + , if < 0 : - ).

Like this: these are my tables.

We supose, today is 2013-12-25

tb_employee:

   ID_EMP                   EMPLOYEE               
     1                     Employee 1 
     2                     Employee 2 
     3                     Employee 3 

tb_requirement:

ID_REQ                   REQUIREMENT
   1                    Requirement 1
   2                    Requirement 2
   3                    Requirement 3
   4                    Requirement 4

tb_detail:

 ID_DET                   ID_EMP                ID_REQ          EXPIRATION
   1                         1                    1             2013-12-29
   2                         1                    2             2013-12-28
   3                         1                    3             2013-12-31

   4                         2                    2             2014-01-05
   5                         2                    3             2013-12-20
   6                         2                    4             2013-12-15

Now, the SELECT QUERY should show like this:

 ID_EMP            EMPLOYEE            REQUIREMENTS_GOT           ANY_REQ_EXPIRE
    1             Employee 1                 3                        YES
    2             Employee 2                 3                         NO

I hope i explained well. Maybe it could be with DATEDIFF ?

Thank you for answers... and of course, Merry Christmas !

È stato utile?

Soluzione

Since you're trying to determine if any of the requirements expired, you should compare the minimal expiry date to today's date. There's no need to use datediff - a simple > operator packed in a case statement would do:

SELECT   id_emp, 
         employee, 
         COUNT(*) AS requirements_got,
         CASE WHEN CURDATE() > MIN(expiration) THEN 'yes' ELSE 'no' END AS any_req_expire
FROM     tb_detail
JOIN     tb_employee ON tb_detail.id_emp = tb_employee.id_emp
GROUP BY id_emp, employee
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top