比较来自2个不同列的总和

时间:2013-08-15 13:36:09

标签: sql inner-join declare

我正在尝试比较两个不同列的总和,我收到一个错误,说我必须声明@Base。然后我试着做像@Base AS B这样的事情,错误就会消失。但我没有检索任何数据。 如果我输错了或者我的INNER JOIN错了,有人可以帮我吗?

 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 而我得到的错误

3 个答案:

答案 0 :(得分:1)

当我在@Base

的引用上放置别名时,它对我有用
Declare @zeroKiloVal float          = 10
Declare @zeroVoluVal float          = 10
Declare @zeroVoluFac float          = 200
Declare @puC        varchar         = 'Sweden'
Declare @deC        varchar         = 'Sweden'
Declare @start      smalldatetime   = '2013-04-21'
Declare @end        smalldatetime   = '2013-05-01'

DECLARE @Base TABLE (SendingID INT, Barcode VARCHAR(50), PickupType CHAR, PickupDate SMALLDATETIME, DeliveryDate SMALLDATETIME, PickupAdrID INT, PickupCustID VARCHAR(10), DeliveryType CHAR, DeliveryAdrID INT, DeliveryCustID VARCHAR(10), DeliveryAlias VARCHAR (30), Volumen FLOAT, [Weight] FLOAT)

INSERT INTO @Base(SendingID, Barcode, PickupType, PickupDate,DeliveryDate, PickupAdrID, PickupCustID, DeliveryType, DeliveryAdrID, DeliveryCustID, DeliveryAlias, Volumen,[Weight]) 
SELECT SendingID = 1, Barcode= 1, PickupType= 1, PickupDate= 1,DeliveryDate= 1, PickupAdrID= 1, PickupCustID= 1, DeliveryType= 1, DeliveryAdrID= 1, DeliveryCustID= 1, DeliveryAlias= 1, Volumen= 1, [Weight] = 1

-- Replacing below code with stubbed data for testing.
-- SELECT SendingID, Barcode, PickupType, 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 'TMHSE' OR DeliveryCustID like 'TMHSE' ) ) 

SELECT totals.DeliveryAdrID
,   totals.PickupDate
,   InvoiceWeight   =   
    (
        CASE WHEN weightTOTAL <= volumenTOTAL THEN volumenTOTAL  
             WHEN weightTOTAL >= volumenTOTAL THEN weightTOTAL ELSE  weightTOTAL END
    ) 
FROM @Base AS B -- <<Added alias here>>
INNER JOIN 
(
    SELECT DeliveryAdrID
    ,   PickupDate      = CONVERT(CHAR(10),PickupDate,110) 
    ,   WeightTOTAL     = CEILING(SUM(CASE [Weight] WHEN 0 THEN @zeroKiloVal     ELSE [Weight] END))
    ,   volumenTOTAL    = CEILING(SUM(CASE Volumen  WHEN 0 THEN (@zeroVoluVal * @zeroVoluFac) ELSE Volumen END)) 
    ,   Packages        = COUNT(DeliveryAdrID)
    FROM @Base 
    GROUP BY CONVERT(CHAR(10),PickupDate,110), DeliveryAdrID 
) AS totals ON B.DeliveryAdrID = totals.DeliveryAdrID 
AND CONVERT(CHAR(10),B.PickupDate,110) = totals.PickupDate

答案 1 :(得分:0)

也许您需要在与Like运算符进行比较的常量中使用一些通配符,例如:

PickUpCustID Like '%TMHSE%' OR DeliveryCustID like '%TMHSE%'

否则,我认为你只是和

一样
PickUpCustID = 'TMHSE' OR DeliveryCustID = 'TMHSE'

'TMHSE' in (PickUpCustID, DeliveryCustID)

答案 2 :(得分:0)

我想出了我声明的变量似乎缺少的错误:

 Declare @puC varchar = 'Sweden'
 Declare @deC varchar = 'Sweden'

我把它改成了

 Declare @puC varchar (50) = 'Sweden'
 Declare @deC varchar (50) = 'Sweden'

感谢您的时间