2015-05-14 33 views
1

我想在gridview中显示最新插入的记录。如何显示最新插入的记录?

样品DB:

Id Alerts Alert_Date 
-- ------ ---------- 
1 Alert1 5/11/2015 12:12:22 PM 
2 Alert2 5/11/2015 12:12:22 PM 
3 Alert1 5/12/2015 12:12:22 PM 
4 Alert2 5/13/2015 12:12:22 PM 
5 Alert2 5/14/2015 12:12:22 PM 
6 Alert3 5/14/2015 12:12:22 PM 

预期输出:

Alerts Alert_Date 
------ ---- 
Alert1 5/12/2015 12:12:22 PM 
Alert2 5/14/2015 12:12:22 PM 
Alert3 5/14/2015 12:12:22 PM 

SQL:

SELECT DISTINCT Alerts, 
(SELECT TOP (1) tbl_Notifications.Alert_Date AS Expr1) AS Expr1 
FROM tbl_Notifications 

我试过的东西,是上面给出的,但它不起作用。帮助我找到一个合适的解决方案。谢谢。

+0

您正在使用哪个数据库管理系统试试? (TOP是产品特定的SQL。) – jarlh

回答

2

尝试使用GROUP BY子句MAX功能如下

SELECT Alerts, MAX(Alert_Date) 
FROM tbl_Notifications 
GROUP BY Alerts 
+0

有时简单的答案是最好的:-)) – dnoeth

+0

@dnoeth hehe那是真的 - 你知道它:)甚至比有时更:) – Parado

2

SELECT TOP (1) Alert,Alert_Date AS Expr1 
FROM tbl_Notifications order by alert_date desc 
+0

请告诉我们你的答案是哪个dbms! (TOP是特定于产品的SQL,并且未指定dbms。) – jarlh

+0

sql-server-2008 –