太多相同的子查询?是否可以在View中创建变量? SQL Server

时间:2015-10-19 18:06:24

标签: sql sql-server sql-server-2008

添加多个子查询后,此查询大约需要8秒钟才能运行。

SELECT 
    a.ImageID, 
    COALESCE (a.[14ky], 0) + COALESCE (b.Labor, 0) + COALESCE ((SELECT SUM (e.MelTotal) AS sm FROM v_Mel e WHERE e.ImageID = a.ImageID), 0) + COALESCE ((SELECT SUM (f.MelSetFee) AS sm FROM v_Mel f WHERE f.ImageID = a.ImageID), 0) + COALESCE (SUM(DISTINCT c.), 0) AS Final14ky, 
    COALESCE (a.[14kw], 0) + COALESCE (b.Labor, 0) + COALESCE ((SELECT SUM (g.MelTotal) AS sm FROM v_Mel g WHERE g.ImageID = a.ImageID), 0) + COALESCE ((SELECT SUM (h.MelSetFee) AS sm FROM v_Mel h WHERE h.ImageID = a.ImageID), 0) + COALESCE (SUM(DISTINCT c.), 0) AS Final14kw, 
    COALESCE (a.[18ky], 0) + COALESCE (b.Labor, 0) + COALESCE ((SELECT SUM (i.MelTotal) AS sm FROM v_Mel i WHERE i.ImageID = a.ImageID), 0) + COALESCE ((SELECT SUM (j.MelSetFee) AS sm FROM v_Mel j WHERE j.ImageID = a.ImageID), 0) + COALESCE (SUM(DISTINCT c.), 0) AS Final18ky, 
    COALESCE (a.[18kw], 0) + COALESCE (b.Labor, 0) + COALESCE ((SELECT SUM (k.MelTotal) AS sm FROM v_Mel k WHERE k.ImageID = a.ImageID), 0) + COALESCE ((SELECT SUM (l.MelSetFee) AS sm FROM v_Mel l WHERE l.ImageID = a.ImageID), 0) + COALESCE (SUM(DISTINCT c.), 0) AS Final18kw, 
    COALESCE (a.Plat, 0) + COALESCE (b.Labor, 0) + COALESCE ((SELECT SUM (m.MelTotal) AS sm FROM v_Mel m WHERE m.ImageID = a.ImageID), 0) + COALESCE ((SELECT SUM (n.MelSetFee) AS sm FROM v_Mel n WHERE n.ImageID = a.ImageID), 0) + COALESCE (SUM(DISTINCT c.), 0) AS FinalPlat, 
    COALESCE (a.Pall, 0) + COALESCE (b.Labor, 0) + COALESCE ((SELECT SUM (o.MelTotal) AS sm FROM v_Mel o WHERE o.ImageID = a.ImageID), 0) + COALESCE ((SELECT SUM (p.MelSetFee) AS sm FROM v_Mel p WHERE p.ImageID = a.ImageID), 0) + COALESCE (SUM(DISTINCT c.), 0) AS FinalPall, 
    a.[14ky], 
    a.[14kw], 
    b.Labor AS LaborTotal, 
    (SELECT SUM (v_Mel.MelTotal) AS sm 
     FROM v_Mel 
     WHERE v_Mel.ImageID = a.ImageID) AS MelTotal, 
    (SELECT SUM (v_Mel.MelSetFee) AS sm 
     FROM v_Mel 
     WHERE v_Mel.ImageID = a.ImageID) AS MelSettingTotal, 
    SUM(DISTINCT c.) AS SideStoneTotal, 
    a.[18ky], a.[18kw], a.Plat, a.Pall
FROM 
    v_itemM a 
LEFT OUTER JOIN
    v_LaborCalc b ON a.ImageID = b.ImageID 
LEFT OUTER JOIN
    v_SideStone c ON a.ImageID = c.ImageID, v_Mel
GROUP BY 
    a.[14ky], a.[14kw], a.ImageID, 
    a.[18ky], a.[18kw], a.Plat, a.Pall, 
    b.Labor

有一些冗余查询(MelTotalMelSetting)。我在某处读到了我无法使用变量,因为我在视图中使用了这个查询。 有没有更好的方法来写这个?我使用的是SQL Server 2008。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

假设您实际上从此查询中获得了所需的结果,并且您需要有关子查询的问题,只需创建一个子查询以从v_Mel和左连接中获取所需的总和它。

FROM 
    v_itemM a 
    LEFT OUTER JOIN v_LaborCalc b ON a.ImageID = b.ImageID 
    LEFT OUTER JOIN v_SideStone c ON a.ImageID = c.ImageID, 
    LEFT OUTER JOIN (Select ImageID, 
                            SUM(MelTotal) MelTotal, 
                            SUM(MelSetFee) MelSetFee 
                     FROM v_Mel 
                     GROUP BY ImageID) d ON a.ImageID = d.ImageID

然后为你的子查询使用

COALESCE (d.MelTotal, 0)
COALESCE (d.MelSetFee , 0)

然后通过

MelTotalMelSetFee添加到您的论坛
GROUP BY a.[14ky], 
    a.[14kw], 
    a.ImageID, 
    a.[18ky], 
    a.[18kw], 
    a.Plat, 
    a.Pall, 
    b.Labor,
    d.MelTotal,
    d.MelSetFee