我试图查询具有多个非唯一外键的表。我想选择'商店'最小距离(英里)所以可能有多个不同距离的商店,我想要一个具有最低里程的独特行
download.file(url, destfile = 'C://whatever.html')
content <- read_html('C://whatever.html')
现在这给我带来了错误的数据......我被困了!
答案 0 :(得分:1)
只需使用row_number()
:
select t.*
from (select t.*,
row_number() over (partition by store1 order by miles asc) as seqnum
from mitable t
) t
where seqnum = 1;
编辑:
如果您想要行数最少的行,那么您可以这样做:
select top 1 t.*
from mitable t
order by miles asc;
这只返回一行。如果您想要关系中的多行,请使用top with ties
。
答案 1 :(得分:0)
region
中的group by
即可
SELECT
m.store1, m.miles, mitable.region, mitable.terminal, mitable.city, mitable.state
FROM
(SELECT
store1, MIN (miles) as miles
FROM
mitable
GROUP BY
store1
--region
) m
INNER JOIN
mitable ON m.store1 = mitable.store1
AND m.miles = mitable.miles