我有一个要求,要从数据库中检索用户的手机号码,且不能重复,因此我使用与众不同关键字来做到这一点。
也并非所有手机号码的格式都正确,因此我使用Case来格式化号码。
最后我想获取最新的500个手机号码,所以我想按ID by desc的顺序进行订购,但显示
ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
以下是我的查询
select distinct top 500 case
when len(PhoneNumber) = 13 And phoneNumber like '+311%' then replace(PhoneNumber, '+311', '0')
when len(PhoneNumber) = 13 And phoneNumber like '311-%' then replace(PhoneNumber, '311-', '0')
when len(PhoneNumber) = 13 And phoneNumber like '0311%' then stuff(PhoneNumber, 1, 4 , 0 )
when len(PhoneNumber) = 14 And phoneNumber like '00311%'then stuff(PhoneNumber, 1, 5 ,'0')
when len(PhoneNumber) = 14 And phoneNumber like '00311%' then stuff(PhoneNumber, 1, 4 ,'0')
when len(PhoneNumber) = 12 And phoneNumber like '311%' then stuff(PhoneNumber, 1 , 3 , '0')
when len(PhoneNumber) = 12 then REPLACE (PhoneNumber, '-', '')
when len(PhoneNumber) = 11 And phoneNumber like '00%' then Stuff(PhoneNumber, 1, 2 , '0')
else PhoneNumber
end
from users where cityid = 1 and statusid = 1 and (len(PhoneNumber) >= 10 And len(PhoneNumber) <= 14)
and PhoneNumber not like '"%' and PhoneNumber not like '+311-%' and PhoneNumber not like '01%'
and PhoneNumber like '0%'
Order by id
更新
正如Metal所建议的那样,我正在使用row_number()并进行分区,这给了我一个手机号码,这是不正确的结果
select top 500000 Mobile
from
(select
row_number() over (partition by PhoneNumber order by id desc) rn,
case
when len(PhoneNumber) = 13 And phoneNumber like '+966%' then replace(PhoneNumber, '+966', '0')
when len(PhoneNumber) = 13 And phoneNumber like '966-%' then replace(PhoneNumber, '966-', '0')
when len(PhoneNumber) = 13 And phoneNumber like '0966%' then stuff(PhoneNumber, 1, 4 , 0 )
when len(PhoneNumber) = 14 And phoneNumber like '00966%'then stuff(PhoneNumber, 1, 5 ,'0')
when len(PhoneNumber) = 14 And phoneNumber like '0096%' then stuff(PhoneNumber, 1, 4 ,'0')
when len(PhoneNumber) = 12 And phoneNumber like '966%' then stuff(PhoneNumber, 1 , 3 , '0')
when len(PhoneNumber) = 12 then REPLACE (PhoneNumber, '-', '')
when len(PhoneNumber) = 11 And phoneNumber like '00%' then Stuff(PhoneNumber, 1, 2 , '0')
else PhoneNumber
end as Mobile
from consigneeDetail
where cityid = 1 and statusid = 1 and (len(PhoneNumber) >= 10 And len(PhoneNumber) <= 14)
and PhoneNumber not like '"%' and PhoneNumber not like '+966-%' and PhoneNumber not like '01%'
and PhoneNumber like '0%') t1
where rn = 1
答案 0 :(得分:1)
改为使用Cars Date Area AreaDay0 AreaDay+1 AreaDay+2 AreaDay+3 AreaDay+4 AreaDay+5 AreaDay+6 AreaDay+7 AreaDay+8 AreaDay9 AreaDay+10 AreaChange Comment
127345 12/21/15 TRUE 1 0 0 0 1 1 0 0 NA 1 0 1 yes,_as_includes_switch_from_0_to_1
127346 12/21/15 TRUE 1 1 1 0 0 0 0 0 0 0 0 0 no,_as_the_switch_is_from_1_to_0
127347 12/22/15 FALSE 0 0 0 0 0 0 0 0 0 0 0 0 no,_as_no_switch
127348 12/22/15 FALSE 0 0 0 0 0 0 0 NA 1 0 0 1 yes,_as_includes_switch_from_0_to_1
127349 12/23/15 TRUE 1 1 1 1 1 1 NA 1 1 1 1 0 no,_as_no_switch
127350 12/21/15 FALSE 0 NA NA NA NA NA NA NA NA NA 1 1 yes,_as_includes_switch_from_0_to_1
,然后使用subquery
获取不同的值。
row_number()
答案 1 :(得分:0)
SELECT DISTINCT phonenumber
FROM (SELECT id,
CASE
WHEN Len(phonenumber) = 13
AND phonenumber LIKE '+311%' THEN
REPLACE(phonenumber, '+311', '0')
WHEN Len(phonenumber) = 13
AND phonenumber LIKE '311-%' THEN
REPLACE(phonenumber, '311-', '0')
WHEN Len(phonenumber) = 13
AND phonenumber LIKE '0311%' THEN
Stuff(phonenumber, 1, 4, 0)
WHEN Len(phonenumber) = 14
AND phonenumber LIKE '00311%'THEN
Stuff(phonenumber, 1, 5, '0')
WHEN Len(phonenumber) = 14
AND phonenumber LIKE '00311%' THEN
Stuff(phonenumber, 1, 4, '0')
WHEN Len(phonenumber) = 12
AND phonenumber LIKE '311%' THEN
Stuff(phonenumber, 1, 3, '0')
WHEN Len(phonenumber) = 12 THEN REPLACE (phonenumber, '-', '')
WHEN Len(phonenumber) = 11
AND phonenumber LIKE '00%' THEN
Stuff(phonenumber, 1, 2, '0')
ELSE phonenumber
end AS phonenumber
FROM users
WHERE cityid = 1
AND statusid = 1
AND ( Len(phonenumber) >= 10
AND Len(phonenumber) <= 14 )
AND phonenumber NOT LIKE '"%'
AND phonenumber NOT LIKE '+311-%'
AND phonenumber NOT LIKE '01%'
AND phonenumber LIKE '0%'
ORDER BY id)
LIMIT 500