如何查询sqlserver中出现次数最多的组合?

qdaijchf 2013-12-24 01:00:23
请各位指点?

商品 规格
A 大
B 中
C 小
A 中
A 小
C 小

其中“C小”这种组合出现次数最多,如何select出现次数最多的那种组合?
...全文
348 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
feiyun0112 2013-12-24
  • 打赏
  • 举报
回复
select top 1 *
from (select 商品,规格,count(*) as num
from 你的表
group by 商品,规格
)t
order by num desc

*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/
qdaijchf 2013-12-24
  • 打赏
  • 举报
回复
谢谢指点! 商品很多,规格也很多,语句中无法一一列举,能否自动获取?
Andy__Huang 2013-12-24
  • 打赏
  • 举报
回复
;with cte(商品,规格) as
(
select 'A','大'
union all select 'B','中'
union all select 'C','小'
union all select 'A','中'
union all select 'A','小'
union all select 'C','小'
)
select top 1 *
from (select 商品,规格,count(*) as num
	from cte
	group by 商品,规格
	)t
order by num desc

/*
商品	规格	num
C	小	2
*/

16,553

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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