这似乎比我找到它更容易。我有一个表,包含名字和姓氏(由类型ID指定)和名称的常见频率。例如:
NameType | Name | FrequencyPercent
1 John 3.267
1 Thomas 1.987
1 Wilson 0.945
2 Smith 4.528
2 Wilson 2.221
2 Thomas 0.437
我希望能够查询名字列表(NameType = 1),如果名字更可能是名字而不是姓氏(基于FrequencyPercentage),那么它只会包含名称。使用此数据集,我的FirstNames查询将包括John和Thomas,我的LastNames查询将返回Smith和Wilson。
希望我能解释清楚。
先谢谢你的帮助,
Russell Schutte
答案 0 :(得分:2)
如果我理解正确,当频率高于与姓氏同名的频率时,你正在寻找名字
这适用于名字。你只需要将其反转为姓氏
CREATE Table YourTable
(
NameType int,
name varchar(20),
FrequencyPercent decimal(12,4)
)
INSERT INTO YourTable
VALUES (1 ,'John', 3.267),
(1 , 'Thomas', 1.987),
(1 , 'Wilson', 0.945),
(2 , 'Smith', 4.528),
(2 , 'Wilson', 2.221),
(2 , 'Thomas', 0.437)
SELECT firstNames.name
FROM
YourTable firstNames
LEFT JOIN YourTable lastNames
ON firstnames.Name = lastNames.Name
AND lastNames.NameType =2
and firstnames.FrequencyPercent < lastNames.FrequencyPercent
WHERE firstNames.NameType =1
AND
lastNames.name is null
结果
name
--------------------
John
Thomas
(2 row(s) affected)
答案 1 :(得分:0)
如果我明白你想要什么......
这将为您提供名字:
SELECT *
FROM [NameTable]
WHERE FrequencyPercent > 1.0
AND NameType = 1
ORDER BY FrequencyPercent
答案 2 :(得分:0)
前5名最常见的名字:
select Name
from Names
where NameType = 1
order by FrequencyPercent desc
limit 5;
答案 3 :(得分:0)
如果你想要名字的前2名,你可以做UNION ALL。
select name FROM table where Nametype = 1 order by FrequencyPercent desc limit 2 UNION ALL select name from table where nametype = 2 order by FrequencyPercent desc limit2