如何在 SQL Developer 中获取子字符串的计数?
How to get the count of a substing in SQL Developer?
我有一个 table 列中有 phone 个数字。我想 return 使用 COUNT(*)
.
某个区号出现的次数
PHONE:
555-555-5565
323-595-8686
666-565-5232
323-599-0000
查询后:
ZIP: FREQUENCY:
555 1
323 2
666 1
非工作查询:
select distinct (substr(phone,0,3)) as "AREA_CODE", count(distinct substr(phone,0,3)) as "COUNT",
from address
我觉得我应该得到 phone 数字的子串作为一个结果列,然后用另一个计算频率,但我似乎无法得到它上班。有帮助吗?
使用 select distinct
进行此类查询的想法从何而来?
正确的结构是group by
。学习 SQL!
select substr(phone, 1, 3)) as "AREA_CODE", count(*) as "COUNT"
from address
group by substr(phone, 1, 3)) as "AREA_CODE";
备注:
substr()
从“1”而不是“0”开始计数。
count(distinct)
不合适。当引用 group by
键时,它只会 return 1。
- 其实
distinct
在SQL中是非常特殊的用途。你通常想避免它。
您可以创建一个临时文件 table 来存储您的 zip :
create table #TempTable
(
Zip Varchar(3)
)
INSERT INTO #TempTable (Zip )
SELECT substr(phone,0,3) from Address
那你可以查询统计这个结果:
Select Zip , count(distinct Zip ) as FREQUENCY
from #TempTable
group by Zip
我有一个 table 列中有 phone 个数字。我想 return 使用 COUNT(*)
.
PHONE:
555-555-5565
323-595-8686
666-565-5232
323-599-0000
查询后:
ZIP: FREQUENCY:
555 1
323 2
666 1
非工作查询:
select distinct (substr(phone,0,3)) as "AREA_CODE", count(distinct substr(phone,0,3)) as "COUNT",
from address
我觉得我应该得到 phone 数字的子串作为一个结果列,然后用另一个计算频率,但我似乎无法得到它上班。有帮助吗?
使用 select distinct
进行此类查询的想法从何而来?
正确的结构是group by
。学习 SQL!
select substr(phone, 1, 3)) as "AREA_CODE", count(*) as "COUNT"
from address
group by substr(phone, 1, 3)) as "AREA_CODE";
备注:
substr()
从“1”而不是“0”开始计数。count(distinct)
不合适。当引用group by
键时,它只会 return 1。- 其实
distinct
在SQL中是非常特殊的用途。你通常想避免它。
您可以创建一个临时文件 table 来存储您的 zip :
create table #TempTable
(
Zip Varchar(3)
)
INSERT INTO #TempTable (Zip )
SELECT substr(phone,0,3) from Address
那你可以查询统计这个结果:
Select Zip , count(distinct Zip ) as FREQUENCY
from #TempTable
group by Zip