“Duplicating” entries SQL

huangapple go评论48阅读模式
英文:

"Duplicating" entries SQL

问题

我有一个看起来像这样的列

a
b
c

我认为可以使用窗口函数来选择,以获得

a  1
a  2

b  1
b  2

c  1
c  2

但似乎找不到合适的方法。

我知道可以使用联合操作来实现这一点,但如果存在窗口函数的话,我更愿意使用窗口函数。

英文:

I have a column that looks like

a
b
c

and I think I can select using some sort of a window function to get

a  1
a  2

b  1
b  2

c  1
c  2

but can't seem to find something suitable.

I know you can do this using a union but would prefer using a window function if it exists.

答案1

得分: 0

select t.myColumn, x.N
from myTable t
CROSS JOIN
(SELECT TOP (2)
ROW_NUMBER() OVER (ORDER BY t1.Object_ID) AS N
FROM Master.sys.All_Columns t1
CROSS JOIN Master.sys.All_Columns t2) x

英文:
select t.myColumn, x.N
from myTable t
CROSS JOIN
(SELECT TOP (2)
			ROW_NUMBER() OVER (ORDER BY t1.Object_ID) AS N
			FROM Master.sys.All_Columns t1
			CROSS JOIN Master.sys.All_Columns t2) x

答案2

得分: 0

SELECT YT.YourColumn, V.I
FROM dbo.YourTable YT
CROSS JOIN (VALUES(1),(2))V(I);
英文:

Considering you just want 2 rows, I would just CROSS JOIN to a VALUES table construct with the values 1 and 2 in it:

SELECT YT.YourColumn,
       V.I
FROM dbo.YourTable YT
     CROSS JOIN (VALUES(1),(2))V(I);

huangapple
  • 本文由 发表于 2023年2月8日 21:50:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386763.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定