当前位置: 代码迷 >> Sql Server >> group by并且能保持原先排序的语句
  详细解决方案

group by并且能保持原先排序的语句

热度:33   发布时间:2016-04-24 10:14:54.0
求一个group by并且能保持原先排序的语句
数据如下

ID carID   peName     pePhone
224 上海大众     马C 188888888
223 上海通用雪佛兰 段AA 133333333
222 上海大众斯柯达 张先生 183333333
221 凯迪拉克     王AA 153333333
220 上海通用五菱   郝AA 132222222
219 一汽大众   张AA 131111111
218 一汽大众   张AA 131111111
217 一汽大众   张B 132111111
216 一汽大众   张B 132111111
215 一汽大众   张B 132111111
214 上海通用五菱 王A 1523333333
213 一汽大众   李先生 1323222222
212 一汽大众   李先生 1323222222
211 上海通用五菱 刘BB 1344444444
210 凯迪拉克 王女士 1355555555
209 宝骏汽车 黄AA 1566666666
208 上海通用五菱 王A 1822222222
207 一汽大众 张先生 1823322222
206 一汽大众 洪AA 15123123123
205 一汽大众 洪AA 15123123123
204 江淮汽车 李AA 1321111212
203 江淮汽车 李AA 1321111212
202 上海通用五菱 曹AA 15X1123213
201 上海通用五菱 曹AA 15X1123213
200 上海通用五菱 曹AA 15X1123213
199 江淮汽车 史A 13X22323232
198 江淮汽车 史A 13X22323232

同名的手机号是一样的,现在想去掉重复的并保持这个排序
谢谢!!
------解决方案--------------------
这样?
----------------------------------------------------------------
-- Author  :DBA_HuangZJ(發糞塗牆)
-- Date    :2014-07-31 17:51:51
-- Version:
--      Microsoft SQL Server 2012 - 11.0.5058.0 (X64) 
-- May 14 2014 18:34:29 
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition: Core-based Licensing (64-bit) on Windows NT 6.3 <X64> (Build 9600: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go 
create table [huang]([ID] int,[carID] varchar(14),[peName] varchar(6),[pePhone] varchar(11))
insert [huang]
select 224,'上海大众','马C','188888888' union all
select 223,'上海通用雪佛兰','段AA','133333333' union all
select 222,'上海大众斯柯达','张先生','183333333' union all
select 221,'凯迪拉克','王AA','153333333' union all
select 220,'上海通用五菱','郝AA','132222222' union all
select 219,'一汽大众','张AA','131111111' union all
select 218,'一汽大众','张AA','131111111' union all
select 217,'一汽大众','张B','132111111' union all
select 216,'一汽大众','张B','132111111' union all
select 215,'一汽大众','张B','132111111' union all
select 214,'上海通用五菱','王A','1523333333' union all
select 213,'一汽大众','李先生','1323222222' union all
select 212,'一汽大众','李先生','1323222222' union all
select 211,'上海通用五菱','刘BB','1344444444' union all
select 210,'凯迪拉克','王女士','1355555555' union all
select 209,'宝骏汽车','黄AA','1566666666' union all
select 208,'上海通用五菱','王A','1822222222' union all
select 207,'一汽大众','张先生','1823322222' union all
select 206,'一汽大众','洪AA','15123123123' union all
select 205,'一汽大众','洪AA','15123123123' union all
select 204,'江淮汽车','李AA','1321111212' union all
select 203,'江淮汽车','李AA','1321111212' union all
select 202,'上海通用五菱','曹AA','15X1123213' union all
select 201,'上海通用五菱','曹AA','15X1123213' union all
select 200,'上海通用五菱','曹AA','15X1123213' union all
select 199,'江淮汽车','史A','13X22323232' union all
select 198,'江淮汽车','史A','13X22323232'
--------------开始查询--------------------------

select MAX(id)id,[carID],[peName],[pePhone]
from [huang]
GROUP BY [carID],[peName],[pePhone]
ORDER BY MAX(id) DESC 
----------------结果----------------------------
/* 
id          carID          peName pePhone
----------- -------------- ------ -----------
224         上海大众           马C     188888888
223         上海通用雪佛兰        段AA    133333333
222         上海大众斯柯达        张先生    183333333
221         凯迪拉克           王AA    153333333
220         上海通用五菱         郝AA    132222222
219         一汽大众           张AA    131111111
217         一汽大众           张B     132111111
214         上海通用五菱         王A     1523333333
213         一汽大众           李先生    1323222222
211         上海通用五菱         刘BB    1344444444
  相关解决方案