我有很多行的结果集,我想编写一个case表达式来比较结果中的每一行而不用硬编码rownumber。
我的结果中的每一行都相互连接。
示例:
1 Number1:2345 Number2:1111
2 Number1:3456 Number2:2222
3 Number1:4567 Number3:3333
4 Number1:5678 Number4:4444
想要比较第1行和第2/3以及第4行等等。
有什么建议吗?我正在使用SQL Developer 2008.
答案 0 :(得分:0)
将相同数据插入两个不同的#table并使用while循环进行比较,如下所示
DECLARE @min INT =1,
@max INT ,@Table1_number1 INT,@Table1_number2 INT,@Table2_number1 INT,@Table2_number2 INT
CREATE TABLE #Table1(ID INT IDENTITY(1,1),
number1 INT,number2 INT)
CREATE TABLE #Table2(ID INT IDENTITY(1,1),
number1 INT,number2 INT)
INSERT INTO #Table1(number1,
number2)
VALUES(1,
1), (1,
1), (2,
2)
INSERT INTO #Table2(number1,
number2)
VALUES(1,
1), (1,
1), (2,
2)
SELECT @max=
(SELECT count(1)
FROM #Table1) WHILE(@min<=@max) BEGIN
SELECT @Table1_number1=number1,
@Table1_number2=number2
FROM #Table1
WHERE ID=@min IF(@min=@max) BEGIN
SELECT @Table2_number1=number1,
@Table2_number2=number2
FROM #Table2
WHERE ID=1 END ELSE BEGIN
SELECT @Table2_number1=number1,
@Table2_number2=number2
FROM #Table2
WHERE ID=@min+1 END
IF(@Table1_number1=@Table2_number1
AND @Table1_number2=@Table2_number2) BEGIN PRINT 'do something' END ELSE BEGIN PRINT '-- do something else' END
SET @min=@min+1 END
DROP TABLE #Table1
DROP TABLE #Table2