我知道之前已经有人问过这个问题,但是我遇到了麻烦。 我有2个查询。一个返回评级为10%的报价之和,另一个返回评级为20%的报价之和。但是,我试图返回两个值的总和。 到目前为止,我的查询是:
SELECT SUM(total) FROM (
SUM(dbo.QuoteLines.UQMLQUOTEVALUE*0.10) AS "10%"
FROM ((dbo.Quotes LEFT OUTER JOIN dbo.QuoteLines ON dbo.Quotes.qmpQuoteID = dbo.QuoteLines.qmlQuoteID) LEFT OUTER JOIN dbo.Followups ON dbo.QuoteLines.qmlQuoteID = dbo.Followups.cmfQuoteID) WHERE dbo.Quotes.qmpClosed = 0 AND dbo.Quotes.UQMPFORWARDEDTOCUSTOMER = -1 AND dbo.QuoteLines.UQMLSALESSTAGE = 1 AND dbo.Followups.cmfStatus < 3 AND dbo.QuoteLines.qmlResolutionReasonID = ''
UNION ALL
SUM(dbo.QuoteLines.UQMLQUOTEVALUE*0.20) AS "20%"
FROM ((dbo.Quotes LEFT OUTER JOIN dbo.QuoteLines ON dbo.Quotes.qmpQuoteID = dbo.QuoteLines.qmlQuoteID) LEFT OUTER JOIN dbo.Followups ON dbo.QuoteLines.qmlQuoteID = dbo.Followups.cmfQuoteID) WHERE dbo.Quotes.qmpClosed = 0 AND dbo.Quotes.UQMPFORWARDEDTOCUSTOMER = -1 AND dbo.QuoteLines.UQMLSALESSTAGE = 2 AND dbo.Followups.cmfStatus < 3 AND dbo.QuoteLines.qmlResolutionReasonID = ''
)a
但是显然我在FROM附近有不正确的语法。
有什么想法吗?
答案 0 :(得分:0)
使用UNION ALL
的正确语法是
SELECT *
FROM
(
SELECT col1, col2 FROM TABLE1
UNION ALL
SELECT col1, col2 FROM TABLE2
) AS alias
您发布的查询对我来说没有意义,因此不确定如何更正
您的2条查询看起来非常相似,只是UQMLSALESSTAGE
有所不同。可能这就是您想要的。而且您不需要使用union
SELECT SUM(dbo.QuoteLines.UQMLQUOTEVALUE *
CASE WHEN dbo.QuoteLines.UQMLSALESSTAGE = 1 THEN 0.10 ELSE 0.20 END) AS Total
FROM dbo.Quotes
LEFT OUTER JOIN dbo.QuoteLines ON dbo.Quotes.qmpQuoteID = dbo.QuoteLines.qmlQuoteID
LEFT OUTER JOIN dbo.Followups ON dbo.QuoteLines.qmlQuoteID = dbo.Followups.cmfQuoteID
WHERE dbo.Quotes.qmpClosed = 0
AND dbo.Quotes.UQMPFORWARDEDTOCUSTOMER = -1
AND dbo.QuoteLines.UQMLSALESSTAGE IN ( 1 , 2 )
AND dbo.Followups.cmfStatus < 3
AND dbo.QuoteLines.qmlResolutionReasonID = ''
答案 1 :(得分:0)
您的脚本有问题。请尝试以下脚本-
SELECT SUM(total) FROM (
SELECT
SUM(dbo.QuoteLines.UQMLQUOTEVALUE*0.10) AS total
FROM dbo.Quotes
LEFT OUTER JOIN dbo.QuoteLines
ON dbo.Quotes.qmpQuoteID = dbo.QuoteLines.qmlQuoteID
LEFT OUTER JOIN dbo.Followups
ON dbo.QuoteLines.qmlQuoteID = dbo.Followups.cmfQuoteID
WHERE dbo.Quotes.qmpClosed = 0
AND dbo.Quotes.UQMPFORWARDEDTOCUSTOMER = -1
AND dbo.QuoteLines.UQMLSALESSTAGE = 1
AND dbo.Followups.cmfStatus < 3
AND dbo.QuoteLines.qmlResolutionReasonID = ''
UNION ALL
SELECT
SUM(dbo.QuoteLines.UQMLQUOTEVALUE*0.20) AS total
FROM dbo.Quotes
LEFT OUTER JOIN dbo.QuoteLines
ON dbo.Quotes.qmpQuoteID = dbo.QuoteLines.qmlQuoteID
LEFT OUTER JOIN dbo.Followups
ON dbo.QuoteLines.qmlQuoteID = dbo.Followups.cmfQuoteID
WHERE dbo.Quotes.qmpClosed = 0
AND dbo.Quotes.UQMPFORWARDEDTOCUSTOMER = -1
AND dbo.QuoteLines.UQMLSALESSTAGE = 2
AND dbo.Followups.cmfStatus < 3
AND dbo.QuoteLines.qmlResolutionReasonID = ''
)a
答案 2 :(得分:0)
找不到总计字段,联接表错误
也许您可以尝试这个
SELECT SUM(total) FROM (
SUM(dbo.QuoteLines.UQMLQUOTEVALUE*0.10) total
FROM ((select dbo.QuoteLines.UQMLQUOTEVALUE from dbo.Quotes LEFT OUTER JOIN dbo.QuoteLines ON dbo.Quotes.qmpQuoteID = dbo.QuoteLines.qmlQuoteID) LEFT OUTER JOIN dbo.Followups ON dbo.QuoteLines.qmlQuoteID = dbo.Followups.cmfQuoteID) WHERE dbo.Quotes.qmpClosed = 0 AND dbo.Quotes.UQMPFORWARDEDTOCUSTOMER = -1 AND dbo.QuoteLines.UQMLSALESSTAGE = 1 AND dbo.Followups.cmfStatus < 3 AND dbo.QuoteLines.qmlResolutionReasonID = ''
UNION ALL
SUM(dbo.QuoteLines.UQMLQUOTEVALUE*0.20) total
FROM ((select dbo.QuoteLines.UQMLQUOTEVALUE from dbo.Quotes LEFT OUTER JOIN dbo.QuoteLines ON dbo.Quotes.qmpQuoteID = dbo.QuoteLines.qmlQuoteID) LEFT OUTER JOIN dbo.Followups ON dbo.QuoteLines.qmlQuoteID = dbo.Followups.cmfQuoteID) WHERE dbo.Quotes.qmpClosed = 0 AND dbo.Quotes.UQMPFORWARDEDTOCUSTOMER = -1 AND dbo.QuoteLines.UQMLSALESSTAGE = 2 AND dbo.Followups.cmfStatus < 3 AND dbo.QuoteLines.qmlResolutionReasonID = ''
)a
答案 3 :(得分:0)
尝试一下:
SELECT SUM(total) FROM (
select
SUM(dbo.QuoteLines.UQMLQUOTEVALUE*0.10) AS "total"
From dbo.Quotes LEFT OUTER JOIN dbo.QuoteLines ON dbo.Quotes.qmpQuoteID = dbo.QuoteLines.qmlQuoteID
LEFT OUTER JOIN dbo.Followups ON dbo.QuoteLines.qmlQuoteID = dbo.Followups.cmfQuoteID WHERE dbo.Quotes.qmpClosed = 0
AND dbo.Quotes.UQMPFORWARDEDTOCUSTOMER = -1 AND dbo.QuoteLines.UQMLSALESSTAGE = 1 AND dbo.Followups.cmfStatus < 3 AND dbo.QuoteLines.qmlResolutionReasonID = ''
UNION ALL
select
SUM(dbo.QuoteLines.UQMLQUOTEVALUE*0.20) AS "total"
FROM dbo.Quotes LEFT OUTER JOIN dbo.QuoteLines ON dbo.Quotes.qmpQuoteID = dbo.QuoteLines.qmlQuoteID
LEFT OUTER JOIN dbo.Followups ON dbo.QuoteLines.qmlQuoteID = dbo.Followups.cmfQuoteID
WHERE dbo.Quotes.qmpClosed = 0 AND dbo.Quotes.UQMPFORWARDEDTOCUSTOMER = -1 AND dbo.QuoteLines.UQMLSALESSTAGE = 2 AND dbo.Followups.cmfStatus < 3 AND dbo.QuoteLines.qmlResolutionReasonID = ''
)a
答案 4 :(得分:0)
您在FROM ()a
中的代码不完整。您可以尝试提取该代码并单独运行。例如:
SUM(dbo.QuoteLines.UQMLQUOTEVALUE*0.10) AS "10%"
FROM ((dbo.Quotes LEFT OUTER JOIN dbo.QuoteLines ON dbo.Quotes.qmpQuoteID = dbo.QuoteLines.qmlQuoteID) LEFT OUTER JOIN dbo.Followups ON dbo.QuoteLines.qmlQuoteID = dbo.Followups.cmfQuoteID) WHERE dbo.Quotes.qmpClosed = 0 AND dbo.Quotes.UQMPFORWARDEDTOCUSTOMER = -1 AND dbo.QuoteLines.UQMLSALESSTAGE = 1 AND dbo.Followups.cmfStatus < 3 AND dbo.QuoteLines.qmlResolutionReasonID = ''
UNION ALL
SUM(dbo.QuoteLines.UQMLQUOTEVALUE*0.20) AS "20%"
FROM ((dbo.Quotes LEFT OUTER JOIN dbo.QuoteLines ON dbo.Quotes.qmpQuoteID = dbo.QuoteLines.qmlQuoteID) LEFT OUTER JOIN dbo.Followups ON dbo.QuoteLines.qmlQuoteID = dbo.Followups.cmfQuoteID) WHERE dbo.Quotes.qmpClosed = 0 AND dbo.Quotes.UQMPFORWARDEDTOCUSTOMER = -1 AND dbo.QuoteLines.UQMLSALESSTAGE = 2 AND dbo.Followups.cmfStatus < 3 AND dbo.QuoteLines.qmlResolutionReasonID = ''
该代码无法单独运行,因为缺少SELECT
。如果您解决了该问题,然后将代码重新插入FROM ()a
,那么我认为您的状态会很好。例如:
SELECT SUM(total) FROM (
SELECT
SUM(dbo.QuoteLines.UQMLQUOTEVALUE*0.10) AS "10%"
FROM ((dbo.Quotes LEFT OUTER JOIN dbo.QuoteLines ON dbo.Quotes.qmpQuoteID = dbo.QuoteLines.qmlQuoteID) LEFT OUTER JOIN dbo.Followups ON dbo.QuoteLines.qmlQuoteID = dbo.Followups.cmfQuoteID) WHERE dbo.Quotes.qmpClosed = 0 AND dbo.Quotes.UQMPFORWARDEDTOCUSTOMER = -1 AND dbo.QuoteLines.UQMLSALESSTAGE = 1 AND dbo.Followups.cmfStatus < 3 AND dbo.QuoteLines.qmlResolutionReasonID = ''
UNION ALL
SELECT
SUM(dbo.QuoteLines.UQMLQUOTEVALUE*0.20) AS "20%"
FROM ((dbo.Quotes LEFT OUTER JOIN dbo.QuoteLines ON dbo.Quotes.qmpQuoteID = dbo.QuoteLines.qmlQuoteID) LEFT OUTER JOIN dbo.Followups ON dbo.QuoteLines.qmlQuoteID = dbo.Followups.cmfQuoteID) WHERE dbo.Quotes.qmpClosed = 0 AND dbo.Quotes.UQMPFORWARDEDTOCUSTOMER = -1 AND dbo.QuoteLines.UQMLSALESSTAGE = 2 AND dbo.Followups.cmfStatus < 3 AND dbo.QuoteLines.qmlResolutionReasonID = ''
)a