0

いくつかの列の合計と数を表示するために、複数のテーブルを結合する動的検索クエリがあります。しかし、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;
4

1 に答える 1

1

join を削除して、exists を使用することもできます。

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
于 2012-04-04T07:52:02.503 に答える