0
我只想要記錄必須存在於特定日期之間的記錄。對於例如如果日期在2013年7月1日至2013年7月17日之間,並且我在2013年7月5日的購買表中輸入了日期爲2013-6-5的條目,並在2013-7-17日的銷售表中輸入了特定產品的條目。它應該忽略這些行並僅選擇那些在所需日期範圍內發生銷售的行。我的下面的查詢無法正常工作。由於上述定義的情況除以零發生異常。請幫助我。選擇記錄,忽略某些範圍之間的日期的空值
SELECT o.Name AS Owner, c.Name AS Company, SUM(sq.PQuantity) - SUM(sq.SQuantity) AS Quantity, SUM(sq.PQuantity * sq.PRate)/SUM(sq.PQuantity) AS Rate,
(SUM(sq.PQuantity) - SUM(sq.SQuantity)) * (SUM(sq.PQuantity * sq.PRate)/SUM(sq.PQuantity)) AS Amount,sq.CompanyId AS CompanyId, o.OwnerId AS OwnerId
FROM (SELECT OwnerId, CompanyId, Quantity AS PQuantity, RatePerShare AS PRate, 0 AS SQuantity, 0 AS SRate, Date
FROM Purchase
UNION ALL
SELECT OwnerId, CompanyId, Quantity AS PQuantity, 0 AS PRate, 0 AS SQuantity, 0 AS SRate, Date
FROM Bonus
UNION ALL
SELECT OwnerId, CompanyId, Quantity AS PQuantity, CostOfShare AS PRate, 0 AS SQuantity, 0 AS SRate, Date
FROM RightShare
UNION ALL
SELECT OwnerId, CompanyId, 0 AS PQuantity, 0 AS PRate, Quantity AS SQuantity, RatePerShare AS SRate, Date
FROM Sales) AS sq INNER JOIN
Owner AS o ON sq.OwnerId = o.OwnerId INNER JOIN
Company AS c ON sq.CompanyId = c.CompanyId
WHERE (sq.OwnerId = @Param1) AND (CONVERT(DATE, sq.Date, 111) BETWEEN @Param2 AND @Param3)
GROUP BY sq.CompanyId, c.Name, o.Name, o.OwnerId
這對我彈出「/ SUM(nullif(sq.PQuantity,0)」 – dseibert
我編輯了這個問題,我用nullif來避免這種情況,但它沒有給出正確的結果。 – user2516394