SQL 当 PIVOT 在 SQL 服务器 CE 中不可用时等效
SQL equivalent when PIVOT is not available in SQL Server CE
我有一个 SQL 查询的问题,该查询在 Microsoft SQL Server CE(不支持 PIVOT)上正常运行。查询如下:
SELECT
*, [1] as IMGN1, [2] as IMGN2, [3] as IMGN3,
[4] as IMGN4, [5] as IMGN5, [6] as IMGN6,
[7] as IMGN7, [8] as IMGN8, [9] as IMGN9,
[10] as IMGN10
FROM
(SELECT
area.CoilId as CID, area.DEFECTID,
(SELECT SUM(s2.endposmd - s2.startposmd)
FROM sections s2
WHERE s2.OutCoilID = 999999
AND s2.InCoilId <= area.coilid) AS POSITIONMD,
d1.DNO as CAMERADEFECTNO, d1.IMAGE_NO as IMAGE_NO,
area.MERGEDTO as MERGEDTO
FROM
(OutCoils AS oc
INNER JOIN
sections AS s ON oc.OutCoilId = s.OutCoilId
INNER JOIN
defects AS area ON area.coilid = s.InCoilId
AND area.PositionMD >= s.StartPosMD
AND area.PositionMD <= s.EndPosMD
INNER JOIN
defects AS d1 ON d1.CoilId = area.CoilId
AND d1.MergedTo = area.DEFECTID)
WHERE
oc.OutCoilID = 999999 AND area.MergedTo = -2) AS SourceTable
PIVOT
(MIN([CAMERADEFECTNO]) FOR [IMAGE_NO]
IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10])
) AS PivotTable
ORDER BY
PositionMD;
如何将其转换为非 PIVOT 版本的有效 SQL 查询?
我尝试了一些使用 CASE 的方法,但是我在用于获取 POSITIONMD 的聚合函数中的子查询有问题,其余部分已经正常工作。知道如何获得 POSITIONMD 吗?
SELECT
area.DEFECTID as DEFECTID,
min(CASE when d1.MERGEDTO = area.DEFECTID then area.COILID end) CID,
min(CASE when d1.MERGEDTO = area.DEFECTID then area.MERGEDTO end) MERGEDTO,
min(CASE when d1.MERGEDTO = area.DEFECTID then (select sum(s2.endposmd - s2.startposmd) from sections s2 where s2.OutCoilID=999999 and s2.InCoilId<=area.coilid) end) POSITIONMD,
sum(CASE when d1.IMAGE_NO = 1 then (d1.DNO) end) IMGN1,
sum(CASE when d1.IMAGE_NO = 2 then (d1.DNO) end) IMGN2,
sum(CASE when d1.IMAGE_NO = 3 then (d1.DNO) end) IMGN3,
sum(CASE when d1.IMAGE_NO = 4 then (d1.DNO) end) IMGN4,
sum(CASE when d1.IMAGE_NO = 5 then (d1.DNO) end) IMGN5,
sum(CASE when d1.IMAGE_NO = 6 then (d1.DNO) end) IMGN6,
sum(CASE when d1.IMAGE_NO = 7 then (d1.DNO) end) IMGN7,
sum(CASE when d1.IMAGE_NO = 8 then (d1.DNO) end) IMGN8,
sum(CASE when d1.IMAGE_NO = 9 then (d1.DNO) end) IMGN9,
sum(CASE when d1.IMAGE_NO = 10 then (d1.DNO) end) IMGN10
FROM ( steinb.OutCoils AS oc
INNER JOIN steinb.sections AS s ON oc.OutCoilId=s.OutCoilId
INNER JOIN steinb.defects AS area ON area.coilid=s.InCoilId AND area.PositionMD>=s.StartPosMD AND area.PositionMD<=s.EndPosMD
INNER JOIN steinb.defects AS d1 ON d1.CoilId=area.CoilId AND d1.MergedTo=area.DEFECTID AND d1.IMAGE_NO!=0)
WHERE oc.OutCoilID=999999 GROUP BY area.DEFECTID ORDER BY PositionMD;
非常感谢。
明白了。我相信您只需要删除 MIN:
SELECT
DEFECTID = area.DEFECTID
, CID = MIN(CASE when d1.MERGEDTO = area.DEFECTID then area.COILID end)
, MERGEDTO = MIN(CASE when d1.MERGEDTO = area.DEFECTID then area.MERGEDTO end)
, POSITIONMD = CASE when d1.MERGEDTO = area.DEFECTID then (select sum(s2.endposmd - s2.startposmd) from sections s2 where s2.OutCoilID=999999 and s2.InCoilId<=area.coilid) END
, IMGN1 = sum(CASE when d1.IMAGE_NO = 1 then (d1.DNO) END)
, IMGN2 = sum(CASE when d1.IMAGE_NO = 2 then (d1.DNO) end)
, IMGN3 = sum(CASE when d1.IMAGE_NO = 3 then (d1.DNO) end)
, IMGN4 = sum(CASE when d1.IMAGE_NO = 4 then (d1.DNO) end)
, IMGN5 = sum(CASE when d1.IMAGE_NO = 5 then (d1.DNO) end)
, IMGN6 = sum(CASE when d1.IMAGE_NO = 6 then (d1.DNO) end)
, IMGN7 = sum(CASE when d1.IMAGE_NO = 7 then (d1.DNO) end)
, IMGN8 = sum(CASE when d1.IMAGE_NO = 8 then (d1.DNO) end)
, IMGN9 = sum(CASE when d1.IMAGE_NO = 9 then (d1.DNO) end)
, IMGN10 = sum(CASE when d1.IMAGE_NO = 10 then (d1.DNO) end)
FROM ( steinb.OutCoils AS oc
INNER JOIN steinb.sections AS s ON oc.OutCoilId=s.OutCoilId
INNER JOIN steinb.defects AS area ON area.coilid=s.InCoilId AND area.PositionMD>=s.StartPosMD AND area.PositionMD<=s.EndPosMD
INNER JOIN steinb.defects AS d1 ON d1.CoilId=area.CoilId AND d1.MergedTo=area.DEFECTID AND d1.IMAGE_NO!=0)
WHERE oc.OutCoilID=999999 GROUP BY area.DEFECTID ORDER BY PositionMD;
我有一个 SQL 查询的问题,该查询在 Microsoft SQL Server CE(不支持 PIVOT)上正常运行。查询如下:
SELECT
*, [1] as IMGN1, [2] as IMGN2, [3] as IMGN3,
[4] as IMGN4, [5] as IMGN5, [6] as IMGN6,
[7] as IMGN7, [8] as IMGN8, [9] as IMGN9,
[10] as IMGN10
FROM
(SELECT
area.CoilId as CID, area.DEFECTID,
(SELECT SUM(s2.endposmd - s2.startposmd)
FROM sections s2
WHERE s2.OutCoilID = 999999
AND s2.InCoilId <= area.coilid) AS POSITIONMD,
d1.DNO as CAMERADEFECTNO, d1.IMAGE_NO as IMAGE_NO,
area.MERGEDTO as MERGEDTO
FROM
(OutCoils AS oc
INNER JOIN
sections AS s ON oc.OutCoilId = s.OutCoilId
INNER JOIN
defects AS area ON area.coilid = s.InCoilId
AND area.PositionMD >= s.StartPosMD
AND area.PositionMD <= s.EndPosMD
INNER JOIN
defects AS d1 ON d1.CoilId = area.CoilId
AND d1.MergedTo = area.DEFECTID)
WHERE
oc.OutCoilID = 999999 AND area.MergedTo = -2) AS SourceTable
PIVOT
(MIN([CAMERADEFECTNO]) FOR [IMAGE_NO]
IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10])
) AS PivotTable
ORDER BY
PositionMD;
如何将其转换为非 PIVOT 版本的有效 SQL 查询?
我尝试了一些使用 CASE 的方法,但是我在用于获取 POSITIONMD 的聚合函数中的子查询有问题,其余部分已经正常工作。知道如何获得 POSITIONMD 吗?
SELECT
area.DEFECTID as DEFECTID,
min(CASE when d1.MERGEDTO = area.DEFECTID then area.COILID end) CID,
min(CASE when d1.MERGEDTO = area.DEFECTID then area.MERGEDTO end) MERGEDTO,
min(CASE when d1.MERGEDTO = area.DEFECTID then (select sum(s2.endposmd - s2.startposmd) from sections s2 where s2.OutCoilID=999999 and s2.InCoilId<=area.coilid) end) POSITIONMD,
sum(CASE when d1.IMAGE_NO = 1 then (d1.DNO) end) IMGN1,
sum(CASE when d1.IMAGE_NO = 2 then (d1.DNO) end) IMGN2,
sum(CASE when d1.IMAGE_NO = 3 then (d1.DNO) end) IMGN3,
sum(CASE when d1.IMAGE_NO = 4 then (d1.DNO) end) IMGN4,
sum(CASE when d1.IMAGE_NO = 5 then (d1.DNO) end) IMGN5,
sum(CASE when d1.IMAGE_NO = 6 then (d1.DNO) end) IMGN6,
sum(CASE when d1.IMAGE_NO = 7 then (d1.DNO) end) IMGN7,
sum(CASE when d1.IMAGE_NO = 8 then (d1.DNO) end) IMGN8,
sum(CASE when d1.IMAGE_NO = 9 then (d1.DNO) end) IMGN9,
sum(CASE when d1.IMAGE_NO = 10 then (d1.DNO) end) IMGN10
FROM ( steinb.OutCoils AS oc
INNER JOIN steinb.sections AS s ON oc.OutCoilId=s.OutCoilId
INNER JOIN steinb.defects AS area ON area.coilid=s.InCoilId AND area.PositionMD>=s.StartPosMD AND area.PositionMD<=s.EndPosMD
INNER JOIN steinb.defects AS d1 ON d1.CoilId=area.CoilId AND d1.MergedTo=area.DEFECTID AND d1.IMAGE_NO!=0)
WHERE oc.OutCoilID=999999 GROUP BY area.DEFECTID ORDER BY PositionMD;
非常感谢。
明白了。我相信您只需要删除 MIN:
SELECT
DEFECTID = area.DEFECTID
, CID = MIN(CASE when d1.MERGEDTO = area.DEFECTID then area.COILID end)
, MERGEDTO = MIN(CASE when d1.MERGEDTO = area.DEFECTID then area.MERGEDTO end)
, POSITIONMD = CASE when d1.MERGEDTO = area.DEFECTID then (select sum(s2.endposmd - s2.startposmd) from sections s2 where s2.OutCoilID=999999 and s2.InCoilId<=area.coilid) END
, IMGN1 = sum(CASE when d1.IMAGE_NO = 1 then (d1.DNO) END)
, IMGN2 = sum(CASE when d1.IMAGE_NO = 2 then (d1.DNO) end)
, IMGN3 = sum(CASE when d1.IMAGE_NO = 3 then (d1.DNO) end)
, IMGN4 = sum(CASE when d1.IMAGE_NO = 4 then (d1.DNO) end)
, IMGN5 = sum(CASE when d1.IMAGE_NO = 5 then (d1.DNO) end)
, IMGN6 = sum(CASE when d1.IMAGE_NO = 6 then (d1.DNO) end)
, IMGN7 = sum(CASE when d1.IMAGE_NO = 7 then (d1.DNO) end)
, IMGN8 = sum(CASE when d1.IMAGE_NO = 8 then (d1.DNO) end)
, IMGN9 = sum(CASE when d1.IMAGE_NO = 9 then (d1.DNO) end)
, IMGN10 = sum(CASE when d1.IMAGE_NO = 10 then (d1.DNO) end)
FROM ( steinb.OutCoils AS oc
INNER JOIN steinb.sections AS s ON oc.OutCoilId=s.OutCoilId
INNER JOIN steinb.defects AS area ON area.coilid=s.InCoilId AND area.PositionMD>=s.StartPosMD AND area.PositionMD<=s.EndPosMD
INNER JOIN steinb.defects AS d1 ON d1.CoilId=area.CoilId AND d1.MergedTo=area.DEFECTID AND d1.IMAGE_NO!=0)
WHERE oc.OutCoilID=999999 GROUP BY area.DEFECTID ORDER BY PositionMD;