sql从列表中选择1项

时间:2018-09-21 13:29:37

标签: sql

我想从一个表中选择一列,该表可以多次引用另一个列。

select t1.name 
from ccp.ENTITIES t1

Non
Albania
Australia
China
Czech Republic
Egypt
Germany
Greece
Group
Hungary
India
Ireland
Italy
Luxembourg
Malaysia
Malta
Netherlands
Portugal
Romania
Spain
Turkey
UK
US

这将给我列出我想要从另一个表中移出一行的名称的列表

v_networks_by_lm此表保存具有列t1.namenetwork的记录。我只希望列表中每个项目的列网络一次。 v_networks_by_lm可以容纳许多t1.name

entity  name   
a   Spain
b   Spain
c   Spain
d   Spain
e   Spain
f   Spain
g   Spain
h   Germany
i   Germany
j   Germany
k   Germany
l   Germany
m   Germany
n   Germany
o   Germany
p   UK
q   Germany
r   Spain
s   Spain
t   Portugal
u   Portugal
v   Portugal
q   Portugal
v_networks_by_lm中的上述数据中

我只希望名称返回一次,且带有任何实体值。但我想从ENTITIES中选择名称,因为它可以是动态的

2 个答案:

答案 0 :(得分:3)

我认为聚合可以满足您的需求:

SELECT MAX(n.network) as network, e.name
FROM ccp.ENTITIES e JOIN
     ccp.v_networks_by_lm n
     ON n.name = e.name
GROUP BY e.name;

答案 1 :(得分:1)

听起来像您希望子查询从表中获取name的单个实例,然后对实体进行联接。

Select sub.one_of_entity_values, sub.name 
from ccp.entities e 
inner join (
    select max(entity) as one_of_entity_values, name
    from v_networks_by_lm
    group by name) sub on e.name  = sub.name