1

我试图以三种不同的方式从一列中获取数据。我正在处理供应商报价和客户接受的报价(然后成为项目)。

我想从他们所有的项目中获得总节省额,无论是否完成。其次,我们正在抓住当前的储蓄,即当前开放项目的储蓄。最后,我需要找出提供给他们但他们尚未采取行动的总金额。

我已经处理了这个查询的前两个:

SELECT SUM(osh.Savings) as YTD,
       SUM(case when p.current_status < 3 then Savings end) AS "Open Savings"
FROM OfferSuggestionHeader osh LEFT JOIN
     Projects p
     ON p.offer_id = osh.OfferID
WHERE p.uid = '1'

到现在为止还挺好。当我开始加入必要的表格以抓住最后一块时,问题就出现了。这是单数查询的样子(并且有效):

SELECT SUM(ofh.Savings)
from OfferSuggestionHeader ofh
LEFT JOIN OfferSuggestionDetail osd
on ofh.OfferID = osd.OfferID
LEFT JOIN Facilities f
on osd.FacilityID = f.id
LEFT JOIN UserFacility uf
on f.id = uf.fid
LEFT JOIN Users u
on uf.uid = u.uid
WHERE u.uid = 1
AND ofh.OfferID NOT IN(SELECT offer_id FROM Projects WHERE uid = 1)

我尝试了六种不同的混合方式,但都以失败告终。似乎它正在多次将数字相加或类似的东西。

感谢您的任何帮助或指点。

编辑: - - - - - - - -

因此,这是一个查询,它执行必要的连接以尝试在保持CASE语句存在的同时获得这么大的总数:

SELECT (osh.Savings) as "Potential", 
    (case when p.current_status < 3 then osh.Savings else null end) AS "Open Savings",
    (case when p.uid=1 then osh.Savings else null end) AS "YTD"
FROM OfferSuggestionHeader osh
LEFT JOIN OfferSuggestionDetail osd
    ON osd.OfferID = osh.OfferID
LEFT JOIN UserFacility uf
    ON uf.fid = osd.FacilityID
LEFT JOIN Projects p
    ON p.uid = uf.uid
WHERE p.uid = '1'

我已经删除了这一SUM部分,看看会发生什么,正如你们中的一些人所建议的那样。果然,我得到了每个值的三个记录。

在此处输入图像描述

这是否有助于更好地澄清问题?

编辑#2:------------ 根据要求,这里是单独的工作查询:

年初至今:

/*YTD Savings*/
SELECT SUM(osh.Savings) as YTD
FROM Projects p
LEFT JOIN OfferSuggestionHeader osh
    ON p.offer_id = osh.OfferID
WHERE p.uid = 1

当前活动/打开:

/*total from open projects*/
SELECT SUM(osh.Savings) as TotalOpen
FROM Projects p
LEFT JOIN OfferSuggestionHeader osh
    ON p.offer_id = osh.OfferID
WHERE p.uid = 1 AND p.current_status < 3

最后,潜在节省的总和:

/*POTENTIAL*/
SELECT SUM(ofh.Savings) as Potential
from OfferSuggestionHeader ofh
LEFT JOIN OfferSuggestionDetail osd
on ofh.OfferID = osd.OfferID
LEFT JOIN Facilities f
on osd.FacilityID = f.id
LEFT JOIN UserFacility uf
on f.id = uf.fid
LEFT JOIN Users u
on uf.uid = u.uid
WHERE u.uid = 1
AND ofh.OfferID NOT IN(SELECT offer_id FROM Projects WHERE uid = 1)
4

1 回答 1

1

Shibormot 可能是正确的,您的 OfferSuggestionDetail 在 OfferSuggestionHeader 中的每一行都有多行。当您加入它们时,加入的每一行的 ofh.savings 字段都是相同的。然后,当你总结时,它会乘以细节行的数量。

-- 编辑为使用您的三个单独的查询:如果您采用查询 2,则可以这样重写:当前活动/打开:

SELECT SUM(CASE WHEN p.current_status < 3 osh.Savings ELSE 0 END ) as TotalOpen
FROM Projects p
LEFT JOIN OfferSuggestionHeader osh
    ON p.offer_id = osh.OfferID
WHERE p.uid = 1 

确认它给了你你想要的。如果是这样,它现在具有与查询 2 相同的 FROM 子句和相同的 WHERE 子句,因此它们可以这样组合:

SELECT SUM(osh.Savings) as YTD
      ,SUM(CASE WHEN p.current_status < 3 osh.Savings ELSE 0 END ) as TotalOpen
FROM Projects p
LEFT JOIN OfferSuggestionHeader osh
    ON p.offer_id = osh.OfferID
WHERE p.uid = 1 

MySql 支持选择列表中的列,该列本身就是一个 SELECT。

如果我将您的第三个查询滑入上面的组合查询中,我会得到这个:(可能可以稍微清理一下内部查询,但它应该可以工作。)

SELECT SUM(osh.Savings) as YTD
      ,SUM(CASE WHEN p.current_status < 3 osh.Savings ELSE 0 END ) as TotalOpen

, (SELECT SUM(ofh2.Savings) as Potential
   from OfferSuggestionHeader ofh2
   LEFT JOIN OfferSuggestionDetail osd2 on ofh2.OfferID = osd2.OfferID
   LEFT JOIN Facilities f2 on osd2.FacilityID = f2.id
   LEFT JOIN UserFacility uf2 on f2.id = uf2.fid
   LEFT JOIN Users u on uf2.uid = u2.uid
   WHERE ofh.OfferID = ofh2.OfferID 
   AND u2.uid = 1
   AND ofh2.OfferID NOT IN(SELECT offer_id FROM Projects WHERE uid = 1
  )


FROM Projects p
LEFT JOIN OfferSuggestionHeader osh
    ON p.offer_id = osh.OfferID
WHERE p.uid = 1 

希望这能让你更近一步。

于 2013-02-27T03:52:49.730 回答