我想找到与一个或多个其他州共享最长边框的州的名称?我有两个表,州和边框。提供的表经过简化,只有5个州
“state”代码是我们的关键字,对每个状态都是唯一的
| 姓名|代码|
| - -|- -|
| 密歇根州|故障指示器|
| 印第安纳州|输入|
| 伊利诺州|离子液体|
| 俄亥俄州|羟基|
| 威斯康星州|工作区|
“边界”两州代码指两州之间的边界
| 代码1|代码2|长度|
| - -|- -|- -|
| 故障指示器|输入|二十个|
| 离子液体|输入|五十个|
| 米|羟基|五个|
| 输入|羟基|四十|
| 工作区|故障指示器|三十|
预期结果:因为从我们的数据看IN有最长的边界,合并长度为20 + 50 + 40
| 状态|
| - -|
| 印第安纳州|
编辑:我到目前为止所做的查询。
SELECT s.name
FROM state s
INNER JOIN boarder b
ON s.code = b.code1
WHERE SUM(b.length);
我的想法是我想选择名字,加入它与边界表。然后得到总和。我看到的两个问题是,这是它列出了一堆名字,我只想一个,我没有考虑如果寄宿者在另一边。例子是寄宿者表的第一行。没有两个单独的行MI,IN,然后IN,那么我应该在两边各做两个subquire吗,但是我怎么把长度加起来呢?
3条答案
按热度按时间vdgimpew1#
Assuming that the data isn't duplicated (so you don't have a row for both MI/IN and IN/MI) you can unpivot that table directly to get all the individual border lengths for each state:
and join that to the states table to get the matching names:
then use the
sum()
aggregate function to get the total length for each state:and then order the result by descending total length, and pick the first row (i.e. the one with the highest value):
fiddle showing the intermediate steps, including how to get the name and total length if you want both.
If you might have ties then you need to decide whether to pick one of them - indeterminately, which this query will do, or determinately by adding say the state code to the order-by clause; or show them all - changing
only
towith ties
.If you're on an older version of Oracle that doesn't support the
fetch
syntax you can put the query in an inline view and select the first row from that.The query i have made so far. ...
That would also work with a bit of tweaking, particularly changing your invalid
where
clause to anorder by
; but you don't need to use two subqueries as you suggested, you can modify the join condition to look at both columns in the borders table:and then as above, order by the total length and fetch the first value:
fiddle
wwtsj6pe2#
您可以对
borders
表执行UNPIVOT
操作,然后计算每个状态的总长度并找到最大长度,然后连接到state
表以查找名称:其中,对于示例数据:
输出:
| 名称|
| - -|
| 印第安纳州|
fiddle
w8rqjzmb3#
也许一个更简单的方法是在CTE中进行联合。我不知道SQL的Oracle方言,但这应该是标准的,我们很容易做到:
未测试。
注意不要在数据库中使用保留字作为名称,例如名称和州。