sql如何利用查询出来的数据再次查询原表中的项

yuki_cabbage 2015-04-30 05:53:48
菜鸟一个,求各位大牛解答。
问题如下:
有一张表,表名为status;假设status表(里面有重复项)内容如下:
列名: a lon lat
1 11 12
2 10 11
3 9 10
1 11 12

我想先统计a字段下各项的出现次数,并把lon,lat项也显示出来。
想得到结果如下:
a 出现次数 lon lat
1 2 11 12
2 1 10 11
3 1 9 10
...全文
267 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
-小蕾- 2015-05-06
  • 打赏
  • 举报
回复
select distinct * from status ,(select a,count(a) as 出现次数 from status group by a) as b where status.a=b.a
shoppo0505 2015-04-30
  • 打赏
  • 举报
回复
Temp是cte表格名,就是后面括号中选取的数据,放入这个temp表格 意义上类似于一个subselect,但是调用更方便,灵活
yuki_cabbage 2015-04-30
  • 打赏
  • 举报
回复
引用 1 楼 shoppo0505 的回复:
with temp as ( select a, count(*) 出现次数 from status group by a ) select temp.*, t. lon, t.lat from temp inner join (select distinct * from status) t on t.a = temp.a
不好意思 ···temp是什么···
shoppo0505 2015-04-30
  • 打赏
  • 举报
回复
with temp as ( select a, count(*) 出现次数 from status group by a ) select temp.*, t. lon, t.lat from temp inner join (select distinct * from status) t on t.a = temp.a

34,590

社区成员

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

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