表和sum函数之间的连接==>返回错误的值

时间:2012-04-04 07:42:07

标签: sql-server search dynamic join relation

我有一个带有连接多个表的动态搜索查询,用于显示某些列的总和和计数。但sum函数返回错误的值。它导致加入。但我不知道该怎么做,只是我知道如果删除传递表的结果将是真的。但我的交货表需要搜索参数。

以下是一些代码行:

SELECT     
    COUNT(DISTINCT Cargo.ID) AS 'TotalCargo',
    SUM(CargoService.Weight) AS 'TotalWeight',
    SUM(CargoService.PackageNo) AS 'TotalPack',
    COUNT(DISTINCT CargoService.FlightNo) AS 'TotalFlight',
    COUNT(DISTINCT Cargo.Origin) AS 'TotalOrigin'
FROM         
    Cargo 
INNER JOIN
    CargoService ON Cargo.ID = CargoService.CargoID 
INNER JOIN
    Contents ON Cargo.Contents = Contents.ID 
FULL JOIN
    Delivery ON Cargo.ID = Delivery.CargoID 
FULL JOIN
    Transit ON Cargo.ID = Transit.CargoID
WHERE       
    Cargo.InputDate BETWEEN CAST(CONVERT(char(10), '2012/04/03', 126) AS datetime) 
                        AND CAST(CONVERT(char(10), '2012/04/03', 126) AS datetime)
    AND CargoService.FlightNo = 1356

,总搜索查询如下:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER procedure [dbo].[Cargo_SearchTotal]
    @EntryFrom nvarchar(20),
    @EntryTo nvarchar(20),
    @DeliveredFrom nvarchar(20),
    @DeliveredTo nvarchar(20),
    @Origin nvarchar(10),
    @Transit nvarchar(10),
    @CargoCode char(16),
    @Name nvarchar(150),
    @Family nvarchar(150),
    @RealName nvarchar(150),
    @RealFamily nvarchar(150),
    @FlightNo nvarchar(10),
    @Daily nvarchar(10),
    @UndeliveredTransit nvarchar(10),
    @Undelivered nvarchar(10),
    @UndeliveredPerishable nvarchar(10),
    @Delivered nvarchar(10),
    @sort nvarchar(10)
as
    declare @query as nvarchar(max);
    declare @params as nvarchar(max);

    set @query = 'SELECT Count(DISTINCT Cargo.ID) AS ''TotalCargo'',
           sum(CargoService.Weight) AS ''TotalWeight'',
           SUM(CargoService.PackageNo) AS ''TotalPack'',
           COUNT(DISTINCT CargoService.FlightNo) AS ''TotalFlight'',
           COUNT(DISTINCT Cargo.Origin) AS ''TotalOrigin''
FROM        
                  Contents INNER JOIN
                  Cargo ON Contents.ID = Cargo.Contents FULL Join
                  Delivery ON Cargo.ID = Delivery.CargoID full JOIN
                  Transit ON Cargo.ID = Transit.CargoID JOIN
                  CargoService ON Cargo.ID = CargoService.CargoID '
---------------------------------------------  
if(@EntryFrom='select') set @EntryFrom='';
if(@EntryTo='select') set @EntryTo='';
if(@DeliveredFrom='select') set @DeliveredFrom='';
if(@DeliveredTo='select') set @DeliveredTo='';
if(@Origin='select') set @Origin='';
if(@Transit='select') set @Transit='';
if(@CargoCode='select') set @CargoCode='';
if(@Name='select') set @Name='';
if(@Family='select') set @Family='';
if(@RealName='select') set @RealName='';
if(@RealFamily='select') set @RealFamily='';
if(@FlightNo='select') set @FlightNo='';
if(@Daily <> 'True') set @Daily='';
if(@UndeliveredTransit <> 'True') set @UndeliveredTransit='';
if(@Undelivered <> 'True') set @Undelivered='';
if(@UndeliveredPerishable <> 'True') set @UndeliveredPerishable='';
if(@Delivered <> 'True') set @Delivered='';
if(@sort <> 'True') set @sort='';
---------------------------------------------  
if(@EntryFrom<>'' or @EntryTo<>'' or @DeliveredFrom<>'' or @DeliveredTo<>'' or @Origin<>'' 
    or @Transit<>'' or @CargoCode<>'' or @Name<>''
    or @Family<>'' or @RealName<>'' or @RealFamily<>'' or @FlightNo<>'' or @Daily <>''
    or @UndeliveredTransit <>'' or @Undelivered <>'' or @UndeliveredPerishable<>'' or @Delivered<>'')
begin
  set @query=@query+' where ';
end
---------------------------------------------  
if(@EntryFrom<>'' and @EntryTo<>'' and (@EntryFrom=@EntryTo))
begin 
 set @query=@query+' Cargo.InputDate=cast(CONVERT(char(10), '''+@EntryFrom+''',126) as datetime) and ';
end
else if(@EntryFrom<>'' and @EntryTo<>'')
begin
  set @query=@query+' Cargo.InputDate between cast(CONVERT(char(10), '''+@EntryFrom+''',126) as datetime) and cast(CONVERT(char(10), '''+@EntryTo+''',126) as datetime) and ';
end     
else if(@EntryFrom<>'' and @EntryTo='')
begin
 set @query=@query+' Cargo.InputDate  >=cast(CONVERT(char(10), '''+@EntryFrom+''',126) as datetime) and ';
end
else if(@EntryFrom='' and @EntryTo<>'')
begin
 set @query=@query+' Cargo.InputDate <=cast(CONVERT(char(10), '''+@EntryTo+''',126) as datetime) and ';
end
---------------------------------------------  
if(@DeliveredFrom<>'' and @DeliveredTo<>'' and (@DeliveredFrom=@DeliveredTo))
begin 
 set @query=@query+' Delivery.DeliveryDate=cast(CONVERT(char(10), '''+@DeliveredFrom+''',126) as datetime) and ';
end
else if(@DeliveredFrom<>'' and @DeliveredTo<>'')
begin
  set @query=@query+' Delivery.DeliveryDate between cast(CONVERT(char(10), '''+@DeliveredFrom+''',126) as datetime) and cast(CONVERT(char(10), '''+@DeliveredTo+''',126) as datetime) and ';
end     
else if(@DeliveredFrom<>'' and @DeliveredTo='')
begin
 set @query=@query+' Delivery.DeliveryDate >= cast(CONVERT(char(10), '''+@DeliveredFrom+''',126) as datetime) and ';
end
else if(@DeliveredFrom='' and @DeliveredTo<>'')
begin
 set @query=@query+' Delivery.DeliveryDate <= cast(CONVERT(char(10), '''+@DeliveredTo+''',126) as datetime) and ';
end
---------------------------------------------  
if(@Origin<>'')
begin
 set @query=@query+' Cargo.Origin = cast('''+@Origin+''' as int) and ';
end
---------------------------------------------  
if(@Transit<>'')
begin
 set @query=@query+' Transit.TransitTo = cast('''+@Transit+''' as int) and ';
end
---------------------------------------------  
if(@CargoCode<>'')
begin
  set @query=@query+' CargoNumber=cast('''+@CargoCode+''' as char(16)) and ';
end
---------------------------------------------  
if(@FlightNo<>'')
begin
  set @query=@query+' CargoService.FlightNo=cast('''+@FlightNo+''' as nvarchar(255)) and ';
end
---------------------------------------------  
if(@Name<>'')
begin
 set @query=@query+' Cargo.Rname like N'''+@Name+''' and ';
end
---------------------------------------------  
if(@Family<>'')
begin
 set @query=@query+' Cargo.RFamily like N'''+@Family+''' and ';
end
---------------------------------------------  
if(@RealName<>'')
begin
 set @query=@query+' Delivery.RName like N'''+@RealName+''' and ';
end
---------------------------------------------  
if(@RealFamily<>'')
begin
 set @query=@query+' Delivery.RFamily like N'''+@RealFamily+''' and ';
end
---------------------------------------------  
if(@Daily<>'')
 begin
    set @query=@query+' convert(varchar, Cargo.SaveDate, 111) = convert(varchar, getdate(), 111) and ';
end
---------------------------------------------  
if(@UndeliveredTransit<>'')
begin
    set @query=@query+' Cargo.Delivered = 0 and Cargo.Transit = 1 and ';
end
---------------------------------------------  
if(@Undelivered<>'')
begin
    set @query=@query+' Cargo.Delivered = 0 and ';
end
---------------------------------------------  
if(@UndeliveredPerishable<>'')
begin
    set @query=@query+' Contents.Perishable = 1 AND Cargo.Delivered = 0  and ';
end
---------------------------------------------  
if(@Delivered <> '')
begin
    set @query=@query+' Cargo.Delivered = 1 and ';
end
--------------------------------------------- 
if(@EntryFrom<>'' or @EntryTo<>'' or @DeliveredFrom<>'' or @DeliveredTo<>'' or @Origin<>'' 
    or @Transit<>'' or @CargoCode<>'' or @Name<>''
    or @Family<>'' or @RealName<>'' or @RealFamily<>'' or @FlightNo<>'' or @Daily <>'' 
    or @UndeliveredTransit <>'' or @Undelivered <>'' or @UndeliveredPerishable <>'' or @Delivered <>'')
begin
 set @query=substring(@query,0,len(@query)-3);
end 
---------------------------------------------  
set @params =  '@EntryFrom nvarchar(20),
                @EntryTo nvarchar(20),
                @DeliveredFrom nvarchar(20),
                @DeliveredTo nvarchar(20),
                @Origin nvarchar(10),
                @Transit nvarchar(10),
                @CargoCode char(16),
                @Name nvarchar(150),
                @Family nvarchar(150),
                @RealName nvarchar(150),
                @RealFamily nvarchar(150),
                @FlightNo nvarchar(10),
                @Daily nvarchar(10),
                @UndeliveredTransit nvarchar(10),
                @Undelivered nvarchar(10),
                @UndeliveredPerishable nvarchar(10),
                @Delivered  nvarchar(10),
                @sort   nvarchar(10)';
---------------------------------------------  
exec sp_executesql @query,@params,@EntryFrom,@EntryTo,@DeliveredFrom,@DeliveredTo,@Origin, 
                   @Transit,@CargoCode,@Name,
                   @Family,@RealName,@RealFamily,@FlightNo,@Daily, 
                   @UndeliveredTransit,@Undelivered,@UndeliveredPerishable,@Delivered,@sort;

1 个答案:

答案 0 :(得分:1)

您可以删除加入并使用存在:

declare @deliveryQuery as varchar(max)
declare @deliveryLen int
set @deliveryQuery = 'and exists (select null 
                                    from Delivery 
                                   where Cargo.ID = Delivery.CargoID'
-- It will change if we add conditions to @deliveryQuery
set @deliveryLen = len(@deliveryQuery)
-- ...
if(@RealName<>'')
begin
 set @deliveryQuery = @deliveryQuery 
                      + ' and Delivery.RName like N'''+@RealName+'''';
end
-- Aditional conditions here following the pattern above

--And later, test if @deliveryQuery has changed
if len(@deliveryQuery) <> @deliveryLen
begin
   set @query = @query + @deliveryQuery + ')'
end