开发者

how to get newest/latest record from SQL Server database c#?

开发者 https://www.devze.com 2023-02-17 08:01 出处:网络
in my statement: SELECT MonitoringJob.ID, MonitoringJob.CreationDate, MonitoringJob.LastCheck, MonitoringJob.Category,

in my statement:

SELECT
    MonitoringJob.ID,
    MonitoringJob.CreationDate,
    MonitoringJob.LastCheck, 
    MonitoringJob.Category,
    MonitoringJob.URL,
    MonitoringJob.Description,
    MonitoringJob.IsJobActive,
    History.ChangeDateTime
FROM  MonitoringJob
INNER JOIN History
ON MonitoringJob.ID=History.JobID
ORDER BY History.ChangeDateTime DESC

i am gett开发者_开发百科ing from database:

7244D096-D388-4216-AE9D-8556F342F0F1    2011-03-17 22:47:15.007 2011-03-17 22:52:28.040 test2   www.spiegel.de  test2   1   2011-03-17 22:52:28.040
6152F7E6-5C4B-42B5-B05B-D3EC43E4B56F    2011-03-17 22:46:59.137 2011-03-17 22:52:27.507 test1   www.ragims.de   test1   1   2011-03-17 22:52:27.507
7244D096-D388-4216-AE9D-8556F342F0F1    2011-03-17 22:47:15.007 2011-03-17 22:52:28.040 test2   www.spiegel.de  test2   1   2011-03-17 22:51:28.323
6152F7E6-5C4B-42B5-B05B-D3EC43E4B56F    2011-03-17 22:46:59.137 2011-03-17 22:52:27.507 test1   www.ragims.de   test1   1   2011-03-17 22:49:27.413

i want get just the latest values of CheckDateTime, it is last column in database. i need just:

7244D096-D388-4216-AE9D-8556F342F0F1    2011-03-17 22:47:15.007 2011-03-17 22:52:28.040 test2   www.spiegel.de  test2   1   2011-03-17 22:52:28.040
    6152F7E6-5C4B-42B5-B05B-D3EC43E4B56F    2011-03-17 22:46:59.137 2011-03-17 22:52:27.507 test1   www.ragims.de   test1   1   2011-03-17 22:52:27.507

ORDER BY History.ChangeDateTime DESC gives me just sorted values by datetime, i need some different code piece instead. please help!


This should work

SELECT    MAX(History.ChangeDateTime)
FROM  MonitoringJob
INNER JOIN History
ON MonitoringJob.ID=History.JobID
GROUP BY MonitoringJob.ID


Is it that you want the rows with the latest value?

SELECT    * -- 
FROM  MonitoringJob
INNER JOIN History
ON MonitoringJob.ID=History.JobID
WHERE CAST(ChangeDateTime as DATE) = (SELECT CAST(MAX(ChangeDateTime) as DATE) FROM History)

This will give you all the records whose date is same as the highest ChangeDateTime's date.


I think this code does what you need. You should group by URL first.

SELECT MonitoringJob.ID
,      MonitoringJob.CreationDate
,      MonitoringJob.LastCheck
,      MonitoringJob.Category
,      MonitoringJob.URL
,      MonitoringJob.Description
,      MonitoringJob.IsJobActive
,      MAX(History.ChangeDateTime) AS ChangeDateTime
FROM  MonitoringJob
INNER JOIN History
ON MonitoringJob.ID=History.JobID
GROUP BY MonitoringJob.URL
      ,  MonitoringJob.ID
      ,  MonitoringJob.CreationDate
      ,  MonitoringJob.LastCheck
      ,  MonitoringJob.Category    
      ,  MonitoringJob.Description
      ,  MonitoringJob.IsJobActive


You will need to do a groupBy statement (whatever test2,test1 column was), then call an orderBy CheckDatTime desc and finish it off with a select TOP(1) command. With the groupBy command you can only return columns specified in the clause, so if you require further information you will likely need to perform a self-join operation on that table.

0

精彩评论

暂无评论...
验证码 换一张
取 消