Я пытаюсь сравнить сумму двух разных столбцов, и я получаю сообщение об ошибке, указывающее, что я должен объявить @Base. Затем я попытался сделать что-то вроде @Base AS B, ошибка исчезнет. Но я не получаю никаких данных. Может ли кто-нибудь помочь мне, если я сделал опечатку, или мой INNER JOIN ошибается?Сравнение суммы из 2 разных столбцов
Declare @Base table(PickupDate smalldatetime, DeliveryDate smalldatetime, PickupAdrID int, PickupCustID varchar(10), DeliveryType char, DeliveryAdrID int, DeliveryCustID varchar(10), DeliveryAlias varchar (30), Volumen float, Weight float) Insert @Base(PickupDate,DeliveryDate, PickupAdrID, PickupCustID, DeliveryType, DeliveryAdrID, DeliveryCustID, DeliveryAlias, Volumen,Weight)
SELECT PickupDate,DeliveryDate, PickupAdrID, PickupCustID, DeliveryType, DeliveryAdrID, DeliveryCustID, DeliveryAlias, Volumen, Weight
FROM Sending
INNER JOIN Address_ViewI ON Sending.PickupAdrID = Address_ViewI.AdrID
INNER JOIN Address_ViewI AS Address_View_DE ON Sending.DeliveryAdrID = Address_View_DE.AdrID
WHERE (Address_ViewI.CountryUK = @puC AND Address_View_DE.CountryUK = @deC) AND (Sending.PickupDate >= @start) AND (Sending.PickupDate < @end) AND ((PickUpCustID Like 'TMHSE' OR DeliveryCustID like 'TMHSE'))
OR (Address_ViewI.CountryUK = @puC AND Address_View_DE.CountryUK = @deC) AND (Sending.PickupDate >= @start) AND (Sending.PickupDate < @end) AND ((PickUpCustID Like 'SomeName' OR DeliveryCustID like 'SomeName'))
SELECT totals.DeliveryAdrID, totals.PickupDate,
(CASE WHEN weightTOTAL <= volumenTOTAL THEN volumenTOTAL
WHEN weightTOTAL >= volumenTOTAL THEN weightTOTAL ELSE weightTOTAL END) AS InvoiceWeight
FROM @Base INNER JOIN
(SELECT DeliveryAdrID, CONVERT(CHAR(10),PickupDate,110) AS PickupDate,
CEILING(SUM(CASE Weight When 0 Then @zeroKiloVal ELSE Weight END)) AS WeightTOTAL,
CEILING(SUM(CASE Volumen WHEN 0 THEN (@zeroVoluVal * @zeroVoluFac) ELSE Volumen END)) AS volumenTOTAL,
COUNT(DeliveryAdrID)AS Packages
FROM @Base GROUP BY CONVERT(CHAR(10),PickupDate,110), DeliveryAdrID) AS totals
ON @Base.DeliveryAdrID = totals.DeliveryAdrID AND CONVERT(CHAR(10),@Base.PickupDate,110) = totals.PickupDate
Полный код указан здесь http://pastie.org/8238866 И ошибка я получаю