使用Group by with table

时间:2015-01-23 03:33:02

标签: sql sql-server oracle oracle10g

     cities         people   lastupdate
    mumbai@c.com    2314     12/2/2014
    mumbai@c.com    1356     13/7/2014
    mumbai@c.com    1124     13/9/2014
    delhi@c.com     1778      1/1/2015
    delhi@c.com     2259      1/1/2014

我有三列citiespeoplelastdate ..我希望数据为

mumbai@c.com|3|1/1/2015 

即。 1列应该是不同的,第2列应该是相关的第一列和第3列应该给出最后更新日期。所以我的主要问题是使用最后更新值进行排序..

2 个答案:

答案 0 :(得分:3)

SELECT cities, COUNT(*), MAX(lastupdate) lastupdate
FROM yourTable
GROUP BY cities
ORDER BY lastupdate

GROUP BY获得不同的citiesCOUNT(*)会计算该组中的行数,MAX(lastupdate)将为每个城市获取最新的lastupdate

答案 1 :(得分:0)

以下是查询:

SELECT cities, COUNT(cities), MAX(lastupdate)
FROM yourtable
GROUP BY cities
order by lastupdate

对于输出,请参阅:sqlfiddle

sqlfiddle