在 SQL 中选择 2 列的不同组合
当我在表上进行多次联接后运行选择时,我有 2 列的输出,并且我想为返回的行集选择 col1 和 col2 的不同组合。
我运行的查询将是这样的:
select a.Col1,b.Col2 from a inner join b on b.Col4=a.Col3
现在输出将有点像这样
Col1 Col2
1 z
2 z
2 x
2 y
3 x
3 x
3 y
4 a
4 b
5 b
5 b
6 c
6 c
6 d
现在我希望输出应该是这样的
1 z
2 y
3 x
4 a
5 b
6 d
如果我随机选择第二列就可以了,因为我的查询输出就像一百万行,我真的我不认为会有一种情况,即使在这种情况下我可以编辑该值,我也会得到相同的 Col1 和 Col2 输出..
你能帮我做同样的事情吗..我认为基本上 col3 需要是一个我猜行号,然后我需要根据随机行号选择两个列。我不知道如何将其转换为 SQL
考虑这种情况 1a 1b 1c 1d 1e 2a 2b 2c 2d 2e 现在 group by 会给我所有这些结果是我想要的 1a 和 2d 或 1a 和 2b。任何这样的组合。
好吧,让我解释一下我的期望:
with rs as(
select a.Col1,b.Col2,rownumber() as rowNumber from a inner join b on b.Col4=a.Col3)
select rs.Col1,rs.Col2 from rs where rs.rowNumber=Round( Rand() *100)
现在我不确定如何使行号或随机数正常工作!
提前致谢。
When i run a select after a number of joins on my table I have an output of 2 columns and I want to select a distinct combination of col1 and col2 for the rowset returned.
the query that i run will be smthing like this:
select a.Col1,b.Col2 from a inner join b on b.Col4=a.Col3
now the output will be somewhat like this
Col1 Col2
1 z
2 z
2 x
2 y
3 x
3 x
3 y
4 a
4 b
5 b
5 b
6 c
6 c
6 d
now I want the output should be something like follows
1 z
2 y
3 x
4 a
5 b
6 d
its ok if I pick the second column randomly as my query output is like a million rows and I really dnt think there will be a case where I will get Col1 and Col2 output to be same even if that is the case I can edit the value..
Can you please help me with the same.. I think basically the col3 needs to be a row number i guess and then i need to selct two cols bases on a random row number.. I dont know how do i transalte this to SQL
consider the case 1a 1b 1c 1d 1e 2a 2b 2c 2d 2e now group by will give me all these results where as i want 1a and 2d or 1a and 2b. any such combination.
OK let me explain what im expecting:
with rs as(
select a.Col1,b.Col2,rownumber() as rowNumber from a inner join b on b.Col4=a.Col3)
select rs.Col1,rs.Col2 from rs where rs.rowNumber=Round( Rand() *100)
now I am not sure how do i get the rownumber or the random working correctly!!
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您根本不关心返回什么
col2
值,如果您确实想要一个随机值,则可以使用以下方法。
或者使用 CLR 聚合函数。这种方法的优点是不需要按分区、newid() 进行排序,下面是一个示例实现。
If you simply don't care what
col2
value is returnedIf you do want a random value you could use the approach below.
Or alternatively use a CLR Aggregate function. This approach has the advantage that it eliminates the requirement to sort by
partition, newid()
an example implementation is below.您需要按
a.Col1
进行分组才能仅通过a.Col1
进行区分,然后由于b.Col2
不包含在您的组中需要找到一个合适的聚合函数将组中的所有值减少到只有一个,<如果您只想要其中一个值,则 code>MIN 就足够了。You need to group by
a.Col1
to get distinct by onlya.Col1
, then sinceb.Col2
is not included in the group you need to find a suitable aggregate function to reduce all values in the group to just one,MIN
is good enough if you just want one of the values.如果我理解正确的话,您希望第 1 列和第 2 列中的每个组合各占一行。这可以通过使用 GROUP BY 或 DISTINCT 轻松完成
例如:
SELECT col1, col2
FROM Your Join
GROUP BY col1, col2
If I understand you correctly, you want to have one line for each combination in column 1 and 2. That can easily be done by using GROUP BY or DISTINCT
for instance:
SELECT col1, col2
FROM Your Join
GROUP BY col1, col2
您必须使用
group by
子句:You must use a
group by
clause :