2012-09-26 13 views
6

Ho due tabelle, una è una lista di attività. L'altro contenente i valori storici per tali compiti.SQL Server: data massima e join interno

ho bisogno di generare un elenco delle ultime evento (e la sua descrizione) per ogni controllo, fino a quando fino a quando la sua Date_Executed è inferiore al datetime corrente meno le Timeframe (TimeFrame essendo ore all'interno del task deve essere fatto , formattato per l'uso in DATEADD). Ma solo se hanno un active = 1.

Tabella: controlli

Check_id description TimeFrame active 
1   Task One  -24  0 
2   Task Two  -24  0 
3   Task Forty -48  1 
4   Task Somehin -128  1 

Tabella: eventi

Event_id Check_id Comment  Date_Executed    User_Executed 
1   1   NULL  2012-09-18 16:10:44.917 admin 
2   1   NULL  2012-09-25 11:39:01.000 jeff 
3   4   Failed  2012-09-25 13:20:09.930 steve 
4   4   Half failed 2012-09-25 13:05:09.953 marsha 
5   2   NULL  2012-09-25 14:02:24.000 marsha 
6   3   NULL  2012-09-18 16:10:55.023 marsha 

le migliori soluzioni che ho finora è:

SELECT 
    a.[Date_Executed] 
    a.[Check_id], 
    a.[Comments], 
    b.[frequency], 
    b.[Check_id], 
    b.[description]  
FROM 
    [checksdb].[dbo].events as a, 
    [checksdb].[dbo].checks as b 
where 
    b.active = 1 
    and a.[Date_Executed] < = dateadd(HOUR,b.[frequency],GETDATE()) 
    and a.Check_id = b.Check_id 
order by Check_id, priority 

e

select MAX(date_Executed), Task_id from daily_check_events group by Task_id 

Nessuno dei due trova quello che mi serve, potrei davvero usare un po 'di aiuto.

+3

Oltre alle domande che hai provato che non vi danno i risultati desiderati, potrebbe mostrare i risultati desiderati? Inoltre, [smetti di usare 'FROM table_a, table_b' syntax] (http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/08/bad-habits-to-kick-using-old-style-joins aspx). –

risposta

8

Dato che si è SQL Server che supporta Common Table Expression e Window Function. Prova questo,

WITH latestEvents 
AS 
(
    SELECT Event_id, Check_id, [Comment], Date_Executed, User_Executed, 
      ROW_NUMBER() OVER(PARTITION BY Check_ID ORDER BY DATE_Executed DESC) 
       AS RowNum 
    FROM events 
) 
SELECT a.[Check_id], a.[description], 
     b.[Date_Executed], b.[Comment] 
FROM checks a 
     INNER JOIN latestEvents b 
      on a.check_ID = b.check_ID 
WHERE b.RowNum = 1 AND 
     a.active = 1 
     -- other conditions here 

SQLFiddle Demo

È possibile che questo interrogazione funziona solo su RDBMS che supporta Window Functions. In alternativa, utilizzare la query di seguito che funziona sulla maggior parte dei RDBMS

SELECT a.Check_id, a.description, 
     c.Date_Executed, c.Comment 
FROM checks a 
     INNER JOIN 
     (
      SELECT check_id, MAX(Date_Executed) maxExecuted 
      FROM events 
      GROUP BY check_ID 
     ) b ON a.check_ID = b.check_ID 
     INNER JOIN events c 
      ON c.check_ID = b.check_ID AND 
      c.date_executed = b.maxExecuted 
WHERE a.active = 1 

SQLFiddle Demo

+0

Grazie per quello. Ho finito per usare la versione 'Common Table Expression'. (Scusa per la risposta in ritardo, ho dovuto testare in modo esauriente). – flammable

Problemi correlati