2009-05-19 45 views
0

我有一個存儲過程,其中iam將InvoiceValue作爲一個字段。我有不同的發票值對應於1個fileid.I想要獲得與每個fileid對應的發票值的總和。在這裏使用組? 這裏是我的存儲過程在存儲過程中使用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

這裏最快的實施方法是,不使用GROUP BY。我會在你的SELECT中使用一個SubQuery來執行發票的SUM。

取出LEFT JOIN到發票,然後relace在選擇L.Amount符合子查詢如下

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

如果您想使用GROUP BY執行SUM,那麼你要麼需要GROUP在您選擇的所有其他列上,或者在VIEW中(可能嵌套)執行GROUP。