答案 0 :(得分:1)
使用以下SQL查询:
WITH CTE
AS
(
SELECT CodeQ, ROW_NUMBER() OVER(ORDER BY NEWID()) AS RN
FROM QRCodes
), Parsed
AS
(
SELECT
RN5,
MAX(Image1) AS Image1, MAX(Image2) AS Image2, MAX(Image3) AS Image3,
MAX(Image4) AS Image4, MAX(Image5) AS Image5
FROM
(
SELECT
ceiling(RN/5.0) AS RN5,
CASE WHEN RN%5 = 1 THEN CodeQ END AS Image1,
CASE WHEN RN%5 = 2 THEN CodeQ END AS Image2,
CASE WHEN RN%5 = 3 THEN CodeQ END AS Image3,
CASE WHEN RN%5 = 4 THEN CodeQ END AS Image4,
CASE WHEN RN%5 = 0 THEN CodeQ END AS Image5
FROM CTE
) AS t
GROUP BY RN5
)
SELECT Image1, Image2, Image3, Image4, Image5
FROM Parsed;
这将为您提供5列图像。然后构建报告以显示这5列。
<强>更新强>
要将其设为4列而不是5列,请尝试以下操作:
WITH CTE
AS
(
SELECT CodeQ, ROW_NUMBER() OVER(ORDER BY NEWID()) AS RN
FROM QRCodes
), Parsed
AS
(
SELECT
RN4,
MAX(Image1) AS Image1, MAX(Image2) AS Image2, MAX(Image3) AS Image3,
MAX(Image4) AS Image4
FROM
(
SELECT
ceiling(RN/4.0) AS RN4,
CASE WHEN RN%4 = 1 THEN CodeQ END AS Image1,
CASE WHEN RN%4 = 2 THEN CodeQ END AS Image2,
CASE WHEN RN%4 = 3 THEN CodeQ END AS Image3,
CASE WHEN RN%4 = 0 THEN CodeQ END AS Image4
FROM CTE
) AS t
GROUP BY RN4
)
SELECT Image1, Image2, Image3, Image4
FROM Parsed;