我有這個查詢,當我向retailer_id參數提供一個值時,它完美地工作。SQL - 內聯選擇語句 - 當@retailer_id = null時發出
SELECT reda.RetailerId,
( SELECT SUM(reda.Purchases)
FROM RetailerDaily reda
WHERE (reda.RetailerId = @retailer_id
OR @retailer_id IS NULL)
AND (reda.DateStart >= @date_from
OR @date_from IS NULL)
AND (reda.DateStart <= @date_to
OR @date_to IS NULL)
GROUP BY reda.RetailerId) AS Purchases,
( SELECT SUM(reda.PurchaseTotal)
FROM RetailerDaily reda
WHERE (reda.RetailerId = @retailer_id
OR @retailer_id IS NULL)
AND (reda.DateStart >= @date_from
OR @date_from IS NULL)
AND (reda.DateStart <= @date_to
OR @date_to IS NULL)
GROUP BY reda.RetailerId) AS PaymentsTotal,
( SELECT SUM(reda.Refunds)
FROM RetailerDaily reda
WHERE (reda.RetailerId = @retailer_id
OR @retailer_id IS NULL)
AND (reda.DateStart >= @date_from
OR @date_from IS NULL)
AND (reda.DateStart <= @date_to
OR @date_to IS NULL)
GROUP BY reda.RetailerId) AS Refunds,
( SELECT SUM(reda.RefundTotal)
FROM RetailerDaily reda
WHERE (reda.RetailerId = @retailer_id
OR @retailer_id IS NULL)
AND (reda.DateStart >= @date_from
OR @date_from IS NULL)
AND (reda.DateStart <= @date_to
OR @date_to IS NULL)
GROUP BY reda.RetailerId) AS RefundsTotal,
( SELECT SUM(reda.OffersOptedInto)
FROM RetailerDaily reda
WHERE (reda.RetailerId = @retailer_id
OR @retailer_id IS NULL)
AND (reda.DateStart >= @date_from
OR @date_from IS NULL)
AND (reda.DateStart <= @date_to
OR @date_to IS NULL)
GROUP BY reda.RetailerId) AS OffersOptedInto,
( SELECT SUM(reda.RedeemedOffers)
FROM RetailerDaily reda
WHERE (reda.RetailerId = @retailer_id
OR @retailer_id IS NULL)
AND (reda.DateStart >= @date_from
OR @date_from IS NULL)
AND (reda.DateStart <= @date_to
OR @date_to IS NULL)
GROUP BY reda.RetailerId) AS RedeemedOffers,
( SELECT SUM(reda.RedeemedOfferTotal)
FROM RetailerDaily reda
WHERE (reda.RetailerId = @retailer_id
OR @retailer_id IS NULL)
AND (reda.DateStart >= @date_from
OR @date_from IS NULL)
AND (reda.DateStart <= @date_to
OR @date_to IS NULL)
GROUP BY reda.RetailerId) AS RedeemedOfferTotal
FROM RetailerDaily reda
WHERE (reda.RetailerId = @retailer_id
OR @retailer_id IS NULL)
AND (reda.DateStart >= @date_from
OR @date_from IS NULL)
AND (reda.DateStart <= @date_to
OR @date_to IS NULL)
GROUP BY reda.RetailerId
然而,當我在retailer_id通過爲空我收到以下錯誤消息
Msg 512, Level 16, State 1, Line 9
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
任何人都可以提供一些線索到如何解決這個問題,我似乎無法工作了。
在此先感謝
史蒂芬
這是預期的行爲。 – 2012-02-22 10:10:57