我怎样才能将表't2'的'col'字段与作为记录的a,b,c,d均等地分为两个相等的字段:Col1(a,b)和Col2(c,d)?
表:t2
Col
A
B
C
D
输出:
Col1 Col2
A C
B D
这是我尝试过的:
SELECT a.col1, "" as col2
FROM (SELECT Top 50 Percent Col as Col1 From t2 order by Col ASc) as a
Union all
SELECT "", b.col1
FROM (SELECT top 50 Percent Col as Col1 From t2 order by Col Desc) as b
上面的输出
col1 col2
a
b
d
c
我目前只能到达(女士访问权限)。非常感谢您的帮助。
创建表“ t2”
Create Table t2(Col Char)
插入值
INSERT INTO t2 ([Col]) VALUES ("a")
INSERT INTO t2 ([Col]) VALUES ("b")
INSERT INTO t2 ([Col]) VALUES ("c")
INSERT INTO t2 ([Col]) VALUES ("d")
答案 0 :(得分:2)
是的,如果您有一个顺序ID,则可以使用交叉表:
TRANSFORM
First(T.Col) AS Col
SELECT
([ID]+1)\2 AS DualID
FROM
(SELECT ID, [ID] Mod 2 AS ColID, [Col]
FROM YourTable) AS T
GROUP BY
([ID]+1)\2
PIVOT
T.ColID;
答案 1 :(得分:0)
SELECT A1.COL1 & "-" & B1.COL1 AS PAIRS
FROM
(SELECT TOP 50 PERCENT A.Col AS COL1 FROM t2 AS A ORDER BY A.Col ASC) AS A1
INNER JOIN
(SELECT TOP 50 PERCENT B.Col AS COL1 FROM t2 AS B ORDER BY B.Col DESC) AS B1
ON A1.COL1 <> B1.COL1
WHERE (SELECT COUNT(*) FROM t2 AS C WHERE C.Col < A1.COL1) = (SELECT
COUNT(*) FROM t2 AS D WHERE D.Col > B1.COL1)