求写个sql,group by 用不好

兔子家族-二哥 2018-06-06 01:59:39
数据库中有年月字段,year ,month,现在要根据这俩个分组 查询出最大的一组,


select max(year + month),c.name,c.id from UserInfo c where c.age is not null order by c.regtime desc group by c.year ,c.month
...全文
608 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
chengangcsdn 2018-06-06
  • 打赏
  • 举报
回复
有多条就再套一层。 declare @TestTable table(tYear int,tMonth int) insert into @TestTable select 2018,6 union all select 2017,12 union all select 2018,4 union all select 2018,6 select * from @TestTable a where exists( select 1 from( select top 1 * from @TestTable order by tYear desc,tMonth desc) ls where a.tyear =ls.tyear and a.tmonth = ls.tmonth)
二月十六 版主 2018-06-06
  • 打赏
  • 举报
回复
--测试数据
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([year] VARCHAR(4),[month] int)
Insert #T
select '2018',6 union all
select '2017',12 union all
select '2018',4 union all
select '2018',6 union all
select '2018',6 union all
select '2018',3 union all
select '2018',3
Go
--测试数据结束
SELECT year,
t.month
FROM
(
SELECT *,
DENSE_RANK() OVER (ORDER BY year DESC, month DESC) rn
FROM #T
) t
WHERE rn = 1;


  • 打赏
  • 举报
回复
引用 12 楼 sinat_28984567 的回复:
[quote=引用 11 楼 God_Girl 的回复:] [quote=引用 7 楼 sinat_28984567 的回复:] 是不是先按照年,再按照月读取最大的?试试这样
SELECT TOP 1
    year,
    month,
    c.name,
    c.id
FROM UserInfo c
WHERE c.age IS NOT NULL
ORDER BY c.year desc,
         c.month desc,
         c.regtime DESC;
别啊 用 top1 我就凉了 我不知道 2018 6的有几条 好吧 他是这样 我说的有问题 year month 2018 6 2017 12 2018 4 2018 6 2018 6 2018 3 2018 3 [/quote] 结果是啥?[/quote] 查出所有 2018 6 的数据啊 所以我一来就说能不能 grope by
  • 打赏
  • 举报
回复
引用 7 楼 sinat_28984567 的回复:
是不是先按照年,再按照月读取最大的?试试这样
SELECT TOP 1
    year,
    month,
    c.name,
    c.id
FROM UserInfo c
WHERE c.age IS NOT NULL
ORDER BY c.year desc,
         c.month desc,
         c.regtime DESC;
别啊 用 top1 我就凉了 我不知道 2018 6的有几条 好吧 他是这样 我说的有问题 year month 2018 6 2017 12 2018 4 2018 6 2018 6 2018 3 2018 3
  • 打赏
  • 举报
回复
就是说我不知道 有几条是最大的
  • 打赏
  • 举报
回复
这只是其中一个小逻辑 不然
引用 5 楼 wmxcn2000 的回复:
select top 1 * from UserInfo c where c.age is not null order by c.regtime desc order by c.year desc,c.month desc
别啊 用 top1 我就凉了 我不知道 2018 6的有几条 好吧 他是这样 我说的有问题 year month 2018 6 2017 12 2018 4 2018 6 2018 6 2018 3 2018 3
chengangcsdn 2018-06-06
  • 打赏
  • 举报
回复
用排序就可以解决 declare @TestTable table(tYear int,tMonth int) insert into @TestTable select 2018,6 union all select 2017,12 union all select 2018,4 select top 1 * from @TestTable order by tYear desc,tMonth desc
二月十六 版主 2018-06-06
  • 打赏
  • 举报
回复
是不是先按照年,再按照月读取最大的?试试这样
SELECT TOP 1
    year,
    month,
    c.name,
    c.id
FROM UserInfo c
WHERE c.age IS NOT NULL
ORDER BY c.year desc,
         c.month desc,
         c.regtime DESC;
二月十六 版主 2018-06-06
  • 打赏
  • 举报
回复
引用 11 楼 God_Girl 的回复:
[quote=引用 7 楼 sinat_28984567 的回复:] 是不是先按照年,再按照月读取最大的?试试这样
SELECT TOP 1
    year,
    month,
    c.name,
    c.id
FROM UserInfo c
WHERE c.age IS NOT NULL
ORDER BY c.year desc,
         c.month desc,
         c.regtime DESC;
别啊 用 top1 我就凉了 我不知道 2018 6的有几条 好吧 他是这样 我说的有问题 year month 2018 6 2017 12 2018 4 2018 6 2018 6 2018 3 2018 3 [/quote] 结果是啥?
吉普赛的歌 版主 2018-06-06
  • 打赏
  • 举报
回复
USE tempdb
GO
IF OBJECT_ID('t') IS NOT NULL DROP TABLE t
GO
CREATE TABLE t(
[year] INT,
[month] INT	
)
INSERT INTO t VALUES (2018,6);
INSERT INTO t VALUES (2017,12);
INSERT INTO t VALUES (2018,4);

SELECT [year],[month]  FROM (
	SELECT ROW_NUMBER() OVER (ORDER BY [year] DESC,[month] desc) AS rid,* FROM t
) AS t1 
WHERE t1.rid=1;
/*
year	month
2018	6
*/
 
  • 打赏
  • 举报
回复
year month 2018 6 2017 12 2018 4 查出 2018 6 这一组
二月十六 版主 2018-06-06
  • 打赏
  • 举报
回复
建议楼主给出测试数据和测试数据对应想要的结果
二月十六 版主 2018-06-06
  • 打赏
  • 举报
回复
啥最大的一组?他俩和做大的一组?
卖水果的net 版主 2018-06-06
  • 打赏
  • 举报
回复
select top 1 * from UserInfo c where c.age is not null order by c.regtime desc order by c.year desc,c.month desc
chengangcsdn 2018-06-06
  • 打赏
  • 举报
回复
这个明显描述不清。 你写个你本来的数据,想得到的结果。

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧