首页 > 试题广场 >

写SQL:找出每个城市的最新一条记录。

[问答题]
写SQL:找出每个城市的最新一条记录。
select city, max(time) from table group by city
发表于 2018-12-23 22:48:12 回复(0)
select * from tb_city as a inner join (select  city, max(time) as time from tb_city group by city) as b on a.city = b.city and a.time = b.time
发表于 2019-04-06 23:41:17 回复(1)
dy_头像 dy_
select *,max(time) from table group by city
发表于 2019-01-18 12:19:49 回复(0)
select city,max(time) from db group by city
发表于 2019-05-27 19:12:06 回复(0)

select 城市,人口,max(创建时间) from table group by 城市;

发表于 2019-04-03 16:35:08 回复(0)

select id,城市,人口,信息,max(时间戳) from table group by 城市

发表于 2019-03-29 18:43:31 回复(0)
select *
from(
select *,row_number()over(partition by city order by time desc) t_rank
from table
) t 
where t_rank=1
发表于 2021-09-08 19:44:59 回复(0)
select * from table where (city,time) in (select city,max(time) from table group by city) 
发表于 2021-07-25 16:43:43 回复(0)
他要的是一条记录,那么记录应该是完整的。所以:
SELECT *,row_number() OVER (PARTITION BY city_id ORDER BY create_time desc)  num from table where num=1
发表于 2020-08-18 10:20:57 回复(0)
select city,max(time) from table group by city
发表于 2020-07-04 11:54:34 回复(0)
    int a[4]={-1,2,1,-4},i,j,k,target=1,b[100][4],n=0;
    for(i=0;i<4;i++)
    {
        for(j=i+1;j<4;j++)
        {
            for(k=j+1;k<4;k++)
            {
                b[n][0]=a[i];
                b[n][1]=a[j];
                b[n][2]=a[k];
                b[n][3]=fabs(a[i]+a[j]+a[k]-target);
                n++;
            }
        }
    }
    j=b[0][3];
    k=0;
    for(i=1;i<n;i++)
        if(b[i][3]<j)
        {
            j=b[i][3];
            k=i;
        }
    printf("%d",b[k][0]+b[k][1]+b[k][2]);
发表于 2020-04-27 11:25:52 回复(0)
select city,max(time) from table group by city
发表于 2020-03-23 21:16:40 回复(0)

思路:先用子查询根据城市分组得出每个城市最新的城市和创建时间,再用内联的方式显示完整的记录。

答:SELECT c1.* FROM city AS c1 INNER JOIN (SELECT city,MAX(createtime) AS createtime FROM city GROUP BY city) AS c2 ON c1.city=c2.city AND c1.createtime=c2.createtime;


编辑于 2019-10-07 12:53:04 回复(1)
Fea头像 Fea
SELECT * FROM table AS P1
WHERE time= (SELECT MAX(time) FROM table AS P2
WHERE P1.city = P2.city
GROUP BY city);
编辑于 2019-09-25 17:27:54 回复(0)
select city,time from city having time=max(time) group by city;
编辑于 2019-07-04 07:31:40 回复(0)
按城市分组查询,按时间倒叙
发表于 2019-06-22 00:33:17 回复(0)
1.查找出所有的城市记录按照城市分组 2.组内时间倒序排列 3.获取组内第一天记录
发表于 2019-06-16 10:23:03 回复(0)
发表于 2019-05-28 20:29:59 回复(0)
select*from
发表于 2019-05-25 11:30:07 回复(0)

max group by


发表于 2019-05-25 11:18:06 回复(0)