请问您能帮我解决这个SQL问题吗?
SELECT *
FROM
(SELECT
SERIAL,ID,NAME,
ROW_NUMBER() OVER (ORDER BY Date_Time) AS NUM1
FROM TABLE_1) AS X
FULL OUTER JOIN
(SELECT
PROGRAM, COLOR, PART,
ROW_NUMBER() OVER (ORDER BY Date_Time) AS NUM2
FROM TABLE_2) AS Y ON X.NUM1 = Y.NUM2
,输出
Serial ID# Name Num1 Program Color part Num2
---------------------------------------------------------
105057 8 A1 1 Basketball Blue 12 1
104552 9 B2 2 Soccer Yellow 15 2
104552 9 B2 3 Soccer Yellow 15 3
104956 9 B2 4 Soccer Yellow 15 4
104956 8 A1 5 Basketball Blue 12 5
如何根据完整联接计算和分组它们。我的输出应该是;
Serial ID# Name Program Color part Total
------------------------------------------------------
105057 8 A1 Basketball Blue 12 2
104552 9 B2 Soccer Yellow 15 3
答案 0 :(得分:0)
您需要select
列,然后group by
所有列。
SELECT
min(x.serial), x.id, x.name,
y.program, y.color, y.part, count(*) as total
FROM
(SELECT SERIAL,ID,NAME,
ROW_NUMBER() OVER (ORDER BY Date_Time) AS NUM1 FROM TABLE_1) AS X
FULL OUTER JOIN
(SELECT PROGRAM,COLOR,PART,
ROW_NUMBER() OVER (ORDER BY Date_Time) AS NUM2 FROM TABLE_2) AS Y
ON X.NUM1 = Y.NUM2
group by x.id, x.name, x.num1,
y.program, y.color, y.part, y.num2
答案 1 :(得分:0)
您需要对重复的列进行分组,并使用函数选择Serial并获取Total。
SELECT MAX(Serial) as Serial, ID, Name, Program, Color, Part, COUNT(*) as Total
FROM
(SELECT SERIAL, ID, NAME,
ROW_NUMBER() OVER (ORDER BY Date_Time) AS NUM1 FROM TABLE_1) AS X
FULL OUTER JOIN
(SELECT PROGRAM,COLOR,PART,
ROW_NUMBER() OVER (ORDER BY Date_Time) AS NUM2 FROM TABLE_2) AS Y
ON X.NUM1 = Y.NUM2
GROUP BY ID, Name, Program, Color, Part