英文:
I want to retain unique values in one column and remove several rows based on the count of those unique values in oracle sql
问题
抱歉,我只会翻译文本内容,不会执行代码。以下是您要翻译的内容:
I sincerely apologize that if the title of the question is not properly addressing the issue I have, hence I will try to explain here.
真诚地道歉,如果问题的标题没有正确地涉及到我所遇到的问题,因此我将在这里尝试解释。
The question is related to the last question I asked and I very grateful for the answer I got which got me the result I was looking for.
这个问题与我上次提出的问题有关,我非常感激得到的答案,它让我得到了我所期望的结果。
However, I had just been asked to make some alterations in the output which I had been trying in the weekend using the solution I got, but all in vain.
然而,我刚刚被要求对输出进行一些更改,我在周末使用得到的解决方案尝试了一下,但都没有成功。
My data is the same I posted before and that was:
我的数据与我之前发布的一样,如下所示:
Structure_no | Name | Structure_type | Spans NO. | Main Span Flag | Name_1 | Proximity |
---|---|---|---|---|---|---|
0087X | MainStreet | Bridge | 3 | |||
00010 | Steele | Bridge | 2 | |||
11010 | Simon | Bridge | 1 | Y | Clifton | ON |
11010 | Simon | Bridge | 2 | N | Clifton | ON |
11010 | Simon | Bridge | 3 | N | Clifton | ON |
11010 | Simon | Bridge | 1 | Y | Fraser | UNDER |
11010 | Simon | Bridge | 2 | N | Fraser | UNDER |
11010 | Simon | Bridge | 3 | N | Fraser | UNDER |
20666 | Canal Lane | Bridge | 1 | Y | Hawks Bay | ON |
20666 | Canal Lane | Bridge | 2 | N | Hawks Bay | ON |
20666 | Canal Lane | Bridge | 1 | Y | Hamilton | UNDER |
20666 | Canal Lane | Bridge | 2 | N | Hamilton | UNDER |
89500 | Spill Sade | Bridge | 1 | N | Creek | ON |
89500 | Spill Sade | Bridge | 2 | N | Creek | ON |
89500 | Spill Sade | Bridge | 3 | Y | Creek | ON |
89500 | Spill Sade | Bridge | 4 | N | Creek | ON |
89500 | Spill Sade | Bridge | 1 | N | Egerton R | UNDER |
89500 | Spill Sade | Bridge | 2 | N | Egerton R | UNDER |
89500 | Spill Sade | Bridge | 3 | Y | Egerton R | UNDER |
89500 | Spill Sade | Bridge | 4 | N | Egerton R | UNDER |
01764 | Fort Orban | Bridge | 2 | Y | Dockyard | ON |
01764 | Fort Orban | Bridge | 2 | N | Wycliffe | UNDER |
Now what happens is that the first 5 columns come from two tables that are LEFT JOINED but as soon as another table is joined (i tried inner join as well) the whole bridge (first 5 columns) repeats itself entirely based on the number of unique values the last two columns (Name_1 and Proximity) have. I guess its some sort of a cross-join??
现在的情况是,前5列来自于两个进行了LEFT JOIN 的表,但是一旦另一个表加入(我也尝试了INNER JOIN),整个桥梁(前5列)会根据最后两列(Name_1 和 Proximity)的唯一值的数量完全重复自己。我猜这是一种交叉连接??
What the I am asked to achieve is that my Data should look like:
我被要求实现的目标是,我的数据应该如下所示:
Structure_no | Name | Structure_type | Spans NO. | Main Span Flag | Name_1 | Proximity |
---|---|---|---|---|---|---|
0087X | MainStreet | Bridge | 3 | |||
00010 | Steele | Bridge | 2 | |||
11010 | Simon | Bridge | 1 | Y | Clifton | ON |
11010 | Simon | Bridge | 2 | N | Fraser | UNDER |
11010 | Simon | Bridge | 3 | N | ||
20666 | Canal Lane | Bridge | 1 | Y | Hawks Bay | ON |
20666 | Canal Lane | Bridge | 2 | N | Hamilton | UNDER |
89500 | Spill Sade | Bridge | 1 | N | Creek | ON |
89500 | Spill Sade | Bridge | 2 | N | Egerton R | UNDER |
89500 | Spill Sade | Bridge | 1 | Y | ||
89500 | Spill Sade | Bridge | 2 | N | ||
01764 | Fort Orban | Bridge | 2 |
英文:
I sincerely apologize that if the title of the question is not properly addressing the issue I have, hence I will try to explain here.
The question is related to the last question I asked and I very grateful for the answer I got which got me the result I was looking for.
However, I had just been asked to make some alterations in the output which I had been trying in the weekend using the solution I got, but all in vain.
My data is the same I posted before and that was:
Structure_no | Name | Structure_type | Spans NO. | Main Span Flag | Name_1 | Proximity |
---|---|---|---|---|---|---|
0087X | MainStreet | Bridge | 3 | |||
00010 | Steele | Bridge | 2 | |||
11010 | Simon | Bridge | 1 | Y | Clifton | ON |
11010 | Simon | Bridge | 2 | N | Clifton | ON |
11010 | Simon | Bridge | 3 | N | Clifton | ON |
11010 | Simon | Bridge | 1 | Y | Fraser | UNDER |
11010 | Simon | Bridge | 2 | N | Fraser | UNDER |
11010 | Simon | Bridge | 3 | N | Fraser | UNDER |
20666 | Canal Lane | Bridge | 1 | Y | Hawks Bay | ON |
20666 | Canal Lane | Bridge | 2 | N | Hawks Bay | ON |
20666 | Canal Lane | Bridge | 1 | Y | Hamilton | UNDER |
20666 | Canal Lane | Bridge | 2 | N | Hamilton | UNDER |
89500 | Spill Sade | Bridge | 1 | N | Creek | ON |
89500 | Spill Sade | Bridge | 2 | N | Creek | ON |
89500 | Spill Sade | Bridge | 3 | Y | Creek | ON |
89500 | Spill Sade | Bridge | 4 | N | Creek | ON |
89500 | Spill Sade | Bridge | 1 | N | Egerton R | UNDER |
89500 | Spill Sade | Bridge | 2 | N | Egerton R | UNDER |
89500 | Spill Sade | Bridge | 3 | Y | Egerton R | UNDER |
89500 | Spill Sade | Bridge | 4 | N | Egerton R | UNDER |
01764 | Fort Orban | Bridge | 2 | Y | Dockyard | ON |
01764 | Fort Orban | Bridge | 2 | N | Wycliffe | UNDER |
Now what happens is that the first 5 columns come from two tables that are LEFT JOINED but as soon as another table is joined (i tried inner join as well) the whole bridge (first 5 columns) repeats itself entirely based on the number of unique values the last two columns (Name_1 and Proximity) have. I guess its some sort of a cross-join??
What the I am asked to achieve is that my Data should look like:
Structure_no | Name | Structure_type | Spans NO. | Main Span Flag | Name_1 | Proximity |
---|---|---|---|---|---|---|
0087X | MainStreet | Bridge | 3 | |||
00010 | Steele | Bridge | 2 | |||
11010 | Simon | Bridge | 1 | Y | Clifton | ON |
11010 | Simon | Bridge | 2 | N | Fraser | UNDER |
11010 | Simon | Bridge | 3 | N | ||
20666 | Canal Lane | Bridge | 1 | Y | Hawks Bay | ON |
20666 | Canal Lane | Bridge | 2 | N | Hamilton | UNDER |
89500 | Spill Sade | Bridge | 1 | N | Creek | ON |
89500 | Spill Sade | Bridge | 2 | N | Egerton R | UNDER |
89500 | Spill Sade | Bridge | 1 | Y | ||
89500 | Spill Sade | Bridge | 2 | N | ||
01764 | Fort Orban | Bridge | 2 | Y | Dockyard | ON |
01764 | Fort Orban | Bridge | 2 | N | Wycliffe | UNDER |
Now based on the SQL query that I have which is shown below:
SELECT
STRUCTURE_NO,
NAME,
STRUCTURE_TYPE,
number_of_spans,
main_span_flag,
CASE rn WHEN 1 THEN name1 END AS name1,
CASE rn WHEN 1 THEN PROXIMITY_CODE END AS proximity_code
FROM (
SELECT a.STRUCTURE_NO,
a.NAME,
a.STRUCTURE_TYPE,
a.number_of_spans,
d.main_span_flag,
d.span_no,
e.NAME AS name1,
e.PROXIMITY_CODE,
ROW_NUMBER() OVER (
PARTITION BY a.structure_no,e.name,e.proximity_code
ORDER BY d.span_no ASC
) AS rn,
ROW_NUMBER() OVER (
PARTITION BY d.span_no,e.name
ORDER BY a.structure_no ASC
) AS rn1
FROM STRUCTURE a
INNER JOIN STRUCTURE_STATUS_TY b
ON a.structure_status_type_code=b.structure_status_type_code
LEFT OUTER JOIN span d
ON a.structure_id=d.structure_id
LEFT OUTER JOIN STRUCTURE_FEAT_PROXMTY e
ON a.STRUCTURE_ID = e.STRUCTURE_ID
ORDER BY a.STRUCTURE_NO ASC
)
I tried to create more partitions so that I can develop some sort of a sequence of the numbers and then remove those rows that dont have that sequence. But it has worked.
I apologize as I am not that experienced in SQL hence I would be highly obliged if this cross join or the whole bridge repetition gets removed and I get the required data, if its possible.
Thanks.
答案1
得分: 0
现在发生的情况是,前5列来自两个左连接的表,但一旦另一个表连接(我也尝试了内连接),整个桥梁(前5列)会根据最后两列(Name_1和Proximity)的唯一值的数量完全重复自身。我猜这是一种交叉连接??
这表明您需要在两个表之间的JOIN
条件中添加更多的筛选条件,以便不会为驱动表中的每一行获得多个匹配项。
在您的示例数据中没有明显的要加入的列,但您似乎是按span_no
和proximity_code
列的顺序匹配行的,因此您可以为连接的每一侧生成一个ROW_NUMBER
,然后在那个基础上进行相关操作(如果适用):
SELECT a.STRUCTURE_NO,
a.NAME,
a.STRUCTURE_TYPE,
a.number_of_spans,
d.main_span_flag,
d.span_no,
e.name AS name1,
e.proximity_code
FROM (
SELECT a.structure_id,
a.STRUCTURE_NO,
a.NAME,
a.STRUCTURE_TYPE,
a.number_of_spans,
d.main_span_flag,
d.span_no,
ROW_NUMBER() OVER (
PARTITION BY a.structure_id
ORDER BY d.span_no. d.main_span_flag DESC
) AS rn
FROM STRUCTURE a
LEFT OUTER JOIN span d
ON a.structure_id=d.structure_id
WHERE EXISTS(
-- This table is never used, you only check that a match exists.
SELECT 1
FROM STRUCTURE_STATUS_TY b
WHERE ad.structure_status_type_code = b.structure_status_type_code
)
) ad
LEFT OUTER JOIN (
SELECT structure_id,
name,
proximity_code,
ROW_NUMBER() OVER (
PARTITION BY structure_id ORDER BY proximity_code
) AS rn
FROM STRUCTURE_FEAT_PROXMTY
) e
ON ad.STRUCTURE_ID = e.STRUCTURE_ID
AND ad.rn = e.rn
请注意,这是SQL查询的一部分,用于解决您描述的问题。
英文:
> Now what happens is that the first 5 columns come from two tables that are LEFT JOINED but as soon as another table is joined (i tried inner join as well) the whole bridge (first 5 columns) repeats itself entirely based on the number of unique values the last two columns (Name_1 and Proximity) have. I guess its some sort of a cross-join??
This suggests that you need to add more filters to the JOIN
condition between the two tables so that you do not get multiple matches for each row in the driving table.
There is not an obvious column to join on in your sample data but you do appear to be matching rows in order of the span_no
and proximity_code
column so you can generate a ROW_NUMBER
for each side of the join and then correlate on that (if appropriate):
SELECT a.STRUCTURE_NO,
a.NAME,
a.STRUCTURE_TYPE,
a.number_of_spans,
d.main_span_flag,
d.span_no,
e.name AS name1,
e.proximity_code
FROM (
SELECT a.structure_id,
a.STRUCTURE_NO,
a.NAME,
a.STRUCTURE_TYPE,
a.number_of_spans,
d.main_span_flag,
d.span_no,
ROW_NUMBER() OVER (
PARTITION BY a.structure_id
ORDER BY d.span_no. d.main_span_flag DESC
) AS rn
FROM STRUCTURE a
LEFT OUTER JOIN span d
ON a.structure_id=d.structure_id
WHERE EXISTS(
-- This table is never used, you only check that a match exists.
SELECT 1
FROM STRUCTURE_STATUS_TY b
WHERE ad.structure_status_type_code = b.structure_status_type_code
)
) ad
LEFT OUTER JOIN (
SELECT structure_id,
name,
proximity_code,
ROW_NUMBER() OVER (
PARTITION BY structure_id ORDER BY proximity_code
) AS rn
FROM STRUCTURE_FEAT_PROXMTY
) e
ON ad.STRUCTURE_ID = e.STRUCTURE_ID
AND ad.rn = e.rn
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论