这是示例想法,基本上我正在做的是从 2 个选择语句中获得不同的结果并加入它,试一试,欢呼 =)
--样本表
DECLARE @SAMPLE TABLE
(
ProductID INT,
ProductName NVARCHAR(255)
)
- 样本数据
INSERT INTO @SAMPLE
VALUES
('1','ProductA'),
('2','ProductB'),
('3','ProductC'),
('4','ProductD')
SELECT FirstProductID,FirstProductName,SecondProductID,SecondProductName FROM
(
--查询以获取第一个产品项
SELECT ROW_NUMBER() OVER (ORDER BY FirstProductID) firstrn,FirstProductID,FirstProductName
FROM(
SELECT ProductID 'FirstProductID',ProductName 'FirstProductName' FROM
(
SELECT ROW_NUMBER() OVER (Order by productid) firstrn1,ProductID,ProductName
FROM @SAMPLE
) FIRSTPRODUCTTABLE
WHERE firstrn1%2 = 1
) FIRSTPRODUCTTABLE1
)t1
- 执行连接
LEFT OUTER JOIN (
--查询获取第二个产品项
SELECT * FROM
(
SELECT ROW_NUMBER() OVER (ORDER BY SecondProductID) rownumber,SecondProductID,SecondProductName
FROM(
SELECT ProductID 'SecondProductID',ProductName 'SecondProductName' FROM
(
SELECT ROW_NUMBER() OVER (Order by productid) rn,ProductID,ProductName
FROM @SAMPLE
) SECONDPRODUCTTABLE
WHERE rn%2 = 0
)SECONDPRODUCTTABLE1
) t2
) t3 ON t1.firstrn=t3.rownumber