当PIVOT在SQL Server CE中不可用时等效的SQL

问题描述 投票:1回答:1

我有一个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;

非常感谢。

sql sql-server database sql-server-ce
1个回答
1
投票

疑难杂症。我相信你只需要删除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;
© www.soinside.com 2019 - 2024. All rights reserved.