当前位置: 代码迷 >> Sql Server >> 怎么用sql语句实现上面的查询
  详细解决方案

怎么用sql语句实现上面的查询

热度:93   发布时间:2016-04-27 12:10:07.0
如何用sql语句实现下面的查询
有一张表order。
name address mobile time
张三 北京 13888888888 2008/02/02
李四 武汉 13688878787  
王五 北京 13888888888  


我想查询列出 这样的效果,手机号的重复,在后面列出重复的次数。
比如

张三 北京 13888888888(2)
李四 武汉 13688878787 (1)
王五 北京 13888888888 (2)

------解决方案--------------------
修正一下:
SQL code
--> 测试数据: @orderdeclare @order table (name varchar(4),address varchar(4),mobile bigint,time datetime)insert into @orderselect '张三','北京',13888888888,'2008/02/02' union allselect '李四','武汉',13688878787,null union allselect '王五','北京',13888888888,nullselect name,address,mobile=ltrim(mobile)+'('+ltrim((select count(*) from @order where mobile=t.mobile))+')'from @order t/*name address mobile---- ------- --------------------------------------张三   北京      13888888888(2)李四   武汉      13688878787(1)王五   北京      13888888888(2)*/
------解决方案--------------------
SQL code
--> 测试数据:[order]if object_id('[order]') is not null drop table [order]create table [order]([name] varchar(4),[address] varchar(4),[mobile] bigint)goinsert [order]select '张三','北京',13888888888 union allselect '李四','武汉',13688878787 union allselect '王五','北京',13888888888goselect *,COUNT(1)over(partition by [mobile]) as times from [order] /*name    address    mobile    times------------------------------------------李四    武汉    13688878787    1王五    北京    13888888888    2张三    北京    13888888888    2*/--不知道你的版本支不支持count()over()这个系统函数
  相关解决方案