通过加入其他表并重置它,在每个组上选择@ROW_NUMBER
问题描述:
我的问题标题可能并不完全符合我的意思,但我不知道要写什么tbh。通过加入其他表并重置它,在每个组上选择@ROW_NUMBER
那么让我们来看看这个问题。我想有像下面
表1
+---------+---------+
| Column1 | Column2 |
+---------+---------+
| Code1 | LCode1 |
| Code2 | LCode2 |
| Code3 | LCode3 |
+---------+---------+
表2
+---------+---------+
| Column3 | Column4 |
+---------+---------+
| LCode1 | string1 |
| LCode1 | string2 |
| LCode1 | string3 |
| LCode2 | String1 |
| LCode2 | String2 |
| LCode2 | String3 |
| LCode2 | String4 |
| LCode3 | String1 |
| LCode3 | String2 |
+---------+---------+
选择的表应该是这样的......
+-------+---------+-----------+---------+
| Index | Column1 | Column2-3 | Column4 |
+-------+---------+-----------+---------+
| 0 | Code1 | LCode1 | string1 |
| 1 | Code1 | LCode1 | string2 |
| 2 | Code1 | LCode1 | string3 |
| 0 | Code2 | LCode2 | string1 |
| 1 | Code2 | LCode2 | string2 |
| 2 | Code2 | LCode2 | string3 |
| 3 | Code2 | LCode2 | string4 |
| 0 | Code3 | LCode3 | string1 |
| 1 | Code3 | LCode3 | string1 |
+-------+---------+-----------+---------+
我曾尝试使用ROW_NUMBER与两个表之间的连接语句,但很遗憾,我没有得到任何好运。只有当我对指定的Column2-3值使用WHERE语句而不是全部时才有可能。 举例
SELECT ROW_NUMBER() AS Index, * FROM Table1 T1 JOIN Table2 T2 ON T1.Column2 = T2.Column3 Order By Index
在此先感谢。
答
你似乎想:
SELECT ROW_NUMBER() OVER (PARTITION BY T1.column1 ORDER BY T2.column4) - 1 AS seqnum,
T1.*, T2.*
FROM Table1 T1 JOIN
Table2 T2
ON T1.Column2 = T2.Column3
ORDER BY seqnum;
index
是一列一个不好的名字,因为它是一个SQL关键字。
答
尝试使用PARTITION BY
在您查询
我希望能帮助你。
我曾试过,但似乎我有一个ROW_NUMBER函数的错误。 '这个函数'ROW_NUMBER'只需要0个参数'' – GAMER
@Gamer。 。 。糟糕,查询中存在拼写错误。 –
正确的语法是'row_number()结束(由Col1分区按col3排序)' – MWillemse