英文:
"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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论