2009-05-19 19 views
0

私はストアドプロシージャを持っており、iamは1つのフィールドとしてInvoiceValueを取得しています.1つのfileidに対応する請求書の値が異なります。各fileidに対応する請求書の値の合計を取得します。ここでグループを使用しますか?ここ は、ここで最も速い実装がGROUP BYを使用しないことであろう私のストアドプロシージャストアドプロシージャのGroup By節を使用する

set ANSI_NULLS ON 
set QUOTED_IDENTIFIER ON 
GO 
ALTER Procedure [dbo].[rptGGM] 
(
    @FromDate varchar(50), 
    @ToDate varchar(50), 
    @PartyID int, 
    @WID int 

) 

As 
DECLARE @WhereStr VarChar(800) 

SET @FromDate = LTRIM(RTRIM(@FromDate)) 
SET @ToDate = LTRIM(RTRIM(@ToDate)) 

SET @WhereStr = 
    CASE @WID 
     WHEN 23 THEN 'WHERE G.PartyID='+LTRIM(str(@PartyID)) +' AND ((D.ATASea >='''[email protected]+''' And D.ATASea<='''[email protected]+'''))' 
     WHEN 7 THEN 'WHERE G.PartyID='+LTRIM(str(@PartyID)) +' AND ((D.ATASea >='''[email protected]+''' And D.ATASea<='''[email protected]+''')) AND A.WorkID='+LTRIM(str(@WID)) 
     WHEN 6 THEN 'WHERE G.PartyID='+LTRIM(str(@PartyID)) +' AND ((D.ATASea >='''[email protected]+''' And D.ATASea<='''[email protected]+''')) AND A.WorkID='+LTRIM(str(@WID)) 
     WHEN 11 THEN 'WHERE G.PartyID='+LTRIM(str(@PartyID)) +' AND ((D.ATASea >='''[email protected]+''' And D.ATASea<='''[email protected]+''')) AND A.WorkID='+LTRIM(str(@WID)) 
     WHEN 12 THEN 'WHERE G.PartyID='+LTRIM(str(@PartyID)) +' AND ((D.ATASea >='''[email protected]+''' And D.ATASea<='''[email protected]+''')) AND A.WorkID='+LTRIM(str(@WID)) 

    END 


BEGIN 
exec 

('SELECT 
     A.FileNumber As [File_No], 
     H.IDFNumber as [IDF No], 
     H.IDFRegNo as [TZDAR], 
     F.POString As [PO_Numbers], 
     B.PartyName As [Exporter Name], 
     F.SIString As [Supplier Invoices], 
     C.CargoDesc As [Cargo_Description], 
     dbo.PackCntDetails(A.FileID) As [Pk/Cnt_Details], 
     D.VesselName As [Vessel Name], 
     D.VoyageNo As [Voyage No], 
     L.Amount As [Invoice Value], 
     convert(varchar, Dbo.ActualDate(A.ETA), 103) + '' '' + convert(varchar, Dbo.ActualDate(A.ETA), 108) As ETA, 
     convert(varchar,Dbo.ActualDate(D.ATASea),103) + '' '' + convert(varchar, Dbo.ActualDate(D.ATASea), 108) As ATA, 
     convert(varchar,Dbo.ActualDate(H.PCVRIssuedDate),103) + '' '' + convert(varchar, Dbo.ActualDate(H.PCVRIssuedDate), 108) as [PCVR Issued Date], 
     convert(varchar,Dbo.ActualDate(V.VATRelDate),103) + '' '' + convert(varchar, Dbo.ActualDate(V.VATRelDate), 108) as [VR Requested], 
     convert(varchar,Dbo.ActualDate(V.OriginalRecdDate),103) + '' '' + convert(varchar, Dbo.ActualDate(V.OriginalRecdDate), 108) as [VR Granted], 
     convert(varchar,Dbo.ActualDate(H.SBECVRAppReturnedDate),103) + '' '' + convert(varchar, Dbo.ActualDate(H.SBECVRAppReturnedDate), 108) as [SBE _VR App. ReturnedDate], 
     convert(varchar,Dbo.ActualDate(H.SBECVRIssuedDate),103) + '' '' + convert(varchar, Dbo.ActualDate(H.SBECVRIssuedDate), 108) as [SBE_CVR IssuedDate], 
     convert(varchar,Dbo.ActualDate(I.CDFLodgedDate),103) + '' '' + convert(varchar, Dbo.ActualDate(I.CDFLodgedDate), 108) as [CDF LodgedDate], 
     convert(varchar,dbo.ActualDate(I.AssessmentPaidDate), 103) + '' '' + convert(varchar,dbo.ActualDate(I.AssessmentPaidDate),108) As [DutyPaidDate], 
     convert(varchar,Dbo.ActualDate(I.EntryLodgedDate),103) + '' '' + convert(varchar, Dbo.ActualDate(I.EntryLodgedDate), 108) as [Entry LodgedDate], 
     convert(varchar,Dbo.ActualDate(I.EntryPassedDate),103) + '' '' + convert(varchar, Dbo.ActualDate(I.EntryPassedDate), 108) as [Custom Released], 
     [Actual Days from ATA to CR]=DATEDIFF(dd, Dbo.ActualDate(D.ATASea),Dbo.ActualDate(I.EntryPassedDate)), 
     convert(varchar,Dbo.ActualDate(K.ShippingLineReleaseDate),103) + '' '' + convert(varchar, Dbo.ActualDate(K.ShippingLineReleaseDate),108) as [ShippingLine ReleaseDate], 
     convert(varchar,Dbo.ActualDate(K.PortInvoicePaidDate),103) + '' '' + convert(varchar, Dbo.ActualDate(K.PortInvoicePaidDate),108) as [Port_Charges Paid_Date], 
     convert(varchar,Dbo.ActualDate(K.RemPortYardDateSea),103) + '' '' + convert(varchar, Dbo.ActualDate(K.RemPortYardDateSea),108) as [Rem_Sea_Port to Yard_Date], 
     [Actual Days From CR to RFD Dar]=DATEDIFF(dd,Dbo.ActualDate(I.EntryPassedDate), Dbo.ActualDate(K.RemPortYardDateSea)), 
     [Total No: of Days(ATA-RFD)]=DATEDIFF(dd,Dbo.ActualDate(D.ATASea), Dbo.ActualDate(K.RemPortYardDateSea)), 
     A.Remarks As [File Status] 
FROM 
    FileMain A 
INNER JOIN Party G ON G.PartyID = A.PartyID 
LEFT JOIN Party B ON B.PartyID = A.ExporterID 
LEFT JOIN Cargo C ON C.FileID = A.FileID 
LEFT JOIN FileSea D ON D.FileID = A.FileID 
LEFT JOIN SIPOString F ON F.FileID=A.FileID 
LEFT JOIN IDFMain H ON H.FileID=A.FileID 
LEFT JOIN Customs I ON I.FileID=A.FileID 
LEFT JOIN VATRelief V ON V.FileID=A.FileID 
LEFT JOIN PortAirPortOperation K ON K.FileID=A.FileID 
LEFT JOIN Invoice L ON L.FileID=A.FileID and L.Incoterm=''CIF''' [email protected]) 

END 

答えて

0

です。代わりにSELECT内のSubQueryを使用して、請求書のSUMを実行します。

あなたはSUMを実行するためにGROUP BYを使用したい場合は、あなたがする必要がいずれかの請求書と、その後

(SELECT SUM(Amount) FROM Invoice WHERE Invoice.FileID = A.FileID and Invoice.Incoterm = 'CIF') [Invoice Value] 

を次のようにサブクエリをSELECTでL.AmountラインをrelaceするJOINをLEFTを削除します選択した他のすべての列にGROUPを実行するか、VIEW内でGROUPを実行します(入れ子になっている可能性があります)。

関連する問題