SQL仅在总和大于0时才选择所有记录

时间:2014-02-04 08:58:25

标签: sql sql-server stored-procedures sum

我有以下SQL;

ALTER  PROCEDURE [dbo].[MyReport]
    @startdate  datetime,
    @enddate    datetime
AS
    /* Return the event plan (coming events) for a specific volunteer */
   declare @sd datetime
   declare @ed datetime

    /* Ensure that the start and end dates covert whole days */
    set @sd = convert(varchar(10),@startdate,120) + ' 00:00:00'
    set @ed = convert(varchar(10),@enddate,120) + ' 23:59:59'

    SELECT 
       E.EventID, E.EventDate, E.StartTime, E.StartLocation, E.EndTime, 
       E.EndLocation, E.Charged, E.Actual,E.ChargeRate, E.Cost, 
       E.Persons, E.Reason,
       C.ClientID, C.Address1, C.Address2,
       C.Town, C.County, C.Postcode, 
       C.InvoiceName, C.InvoiceAddress1, C.InvoiceAddress2,
       C.InvoiceTown, C.InvoiceCounty, C.InvoicePostCode,
       ISNULL(C.Surname, '') + ', ' + ISNULL(C.Forename, '') AS ClientSurnameForename
    FROM 
       vEvents E 
    INNER JOIN 
       vClients C ON E.ClientID = C.ClientID
    WHERE    
       (E.EventDate BETWEEN @sd AND @ed) 
       AND E.SchemeID = 4
    ORDER BY 
       c.Surname, c.Forename, E.EventDate, E.StartTime, E.EndTime

我需要对E.Charged列进行求和,以便在返回记录集之前检查客户端的数量是否大于0。我尝试过以下方法:

ALTER PROCEDURE [dbo].[MyReport]
    @startdate  datetime,
    @enddate    datetime
AS
    /* Return the event plan (coming events) for a specific volunteer */
    declare @sd datetime
    declare @ed datetime

    /* Ensure that the start and end dates covert whole days */
    set @sd = convert(varchar(10),@startdate,120) + ' 00:00:00'
    set @ed = convert(varchar(10),@enddate,120) + ' 23:59:59'

    SELECT 
        E.EventID, E.EventDate, E.StartTime, E.StartLocation, E.EndTime, 
        E.EndLocation, E.Charged, E.Actual,E.ChargeRate, E.Cost, 
        E.Persons, E.Reason,
        C.ClientID, C.Address1, C.Address2, C.Town, C.County, C.Postcode,
        C.InvoiceName, C.InvoiceAddress1, C.InvoiceAddress2, C.InvoiceTown,
        C.InvoiceCounty, C.InvoicePostCode,
        ISNULL(C.Surname, '') + ', ' + ISNULL(C.Forename, '') AS ClientSurnameForename
    FROM 
        vEvents E 
    INNER JOIN 
        vClients C ON E.ClientID = C.ClientID
    WHERE 
        vEvents.ClientID IN (SELECT vEvents.Charged 
                             FROM vEvents 
                             GROUP BY vEvents.ClientID, vEvents.charged 
                             HAVING SUM(vEvents.Charged) > 0) 
        AND (E.EventDate BETWEEN @sd AND @ed) 
        AND E.SchemeID = 4
    ORDER BY 
        c.Surname, c.Forename, E.EventDate, E.StartTime, E.EndTime

但我不断得到多部分标识符无法绑定'。 TIA Andrew

表格结构

    [vEvents](
[EventID] [int] IDENTITY(1,1) NOT NULL,
[ClientID] [int] NOT NULL,
[ChargeID] [int] NOT NULL,
[EventDate] [datetime] NULL,
[StartTime] [datetime] NULL,
[StartLocation] [nvarchar](50) NULL,
[EndTime] [datetime] NULL,
[EndLocation] [nvarchar](50) NULL,
[Reason] [nvarchar](50) NULL,
[Charged] [decimal](6, 2) NOT NULL,
[Actual] [decimal](6, 2) NOT NULL,
[Additional] [decimal](6, 2) NOT NULL,
[Done] [bit] NOT NULL,
[Verifier] [nvarchar](50) NULL,
[ChargeRate] [decimal](6, 4) NULL,
[TeamID] [int] NOT NULL,
[Combined] [bit] NOT NULL,

它是一个编辑列表,但包含最相关的

客户表

    [vClients](
[ClientID] [int] IDENTITY(1,1) NOT NULL,
[ManagerID] [int] NOT NULL,
[RegularID] [int] NOT NULL,
[Forename] [nvarchar](50) NULL,
[Surname] [nvarchar](50) NULL,
[Address1] [nvarchar](50) NULL,
[Address2] [nvarchar](50) NULL,
[Town] [nvarchar](50) NULL,
[County] [nvarchar](50) NULL,
[PostCode] [nvarchar](10) NULL,
[Telephone] [nvarchar](30) NULL,
[Comments] [ntext] NULL,
[ReviewDate] [datetime] NULL,
[Requirements] [int] NOT NULL,
[Status] [int] NOT NULL,
[EmergencyType] [nvarchar](50) NULL,
[EmergencyContact] [nvarchar](50) NULL,
[EmergencyNotes] [ntext] NULL,
[EmergencyTelephone] [nvarchar](50) NULL,
[Title] [nvarchar](50) NULL,
[VolunteerID] [int] NOT NULL,
[UserID] [int] NOT NULL,
[DateOfBirth] [datetime] NULL,
[HasPushPin] [bit] NULL,
[InvoiceAddress1] [nvarchar](50) NULL,
[InvoiceAddress2] [nvarchar](50) NULL,
[InvoiceTown] [nvarchar](50) NULL,
[InvoiceCounty] [nvarchar](50) NULL,
[InvoicePostcode] [nvarchar](10) NULL,
[InvoiceName] [nvarchar](50) NULL,
[Email] [nvarchar](50) NULL,

2 个答案:

答案 0 :(得分:0)

尝试将查询的WHERE部分更改为以下内容:

WHERE 
    E.ClientID IN (SELECT vEvents.ClientID 
                         FROM vEvents 
                         GROUP BY vEvents.ClientID 
                         HAVING SUM(vEvents.Charged) > 0) 
    AND ...

答案 1 :(得分:0)

也许:

ALTER PROCEDURE [dbo].[MyReport]
    @startdate  datetime,
    @enddate    datetime
AS
    /* Return the event plan (coming events) for a specific volunteer */
    declare @sd datetime
    declare @ed datetime

    /* Ensure that the start and end dates covert whole days */
    set @sd = convert(varchar(10),@startdate,120) + ' 00:00:00'
    set @ed = convert(varchar(10),@enddate,120) + ' 23:59:59'

    SELECT 
        E.EventID, E.EventDate, E.StartTime, E.StartLocation, E.EndTime, 
        E.EndLocation, E.Charged, E.Actual,E.ChargeRate, E.Cost, 
        E.Persons, E.Reason,
        C.ClientID, C.Address1, C.Address2, C.Town, C.County, C.Postcode,
        C.InvoiceName, C.InvoiceAddress1, C.InvoiceAddress2, C.InvoiceTown,
        C.InvoiceCounty, C.InvoicePostCode,
        ISNULL(C.Surname, '') + ', ' + ISNULL(C.Forename, '') AS ClientSurnameForename
    FROM 
        vEvents E 
    INNER JOIN 
        vClients C ON E.ClientID = C.ClientID
    INNER JOIN (SELECT ClientID, SUM(Charged) ch
                FROM vEvents 
                GROUP BY ClientID
                HAVING SUM(Charged) > 0) t
      ON t.ClientID = vEvents.ClientID
    WHERE (E.EventDate BETWEEN @sd AND @ed) 
        AND E.SchemeID = 4
    ORDER BY 
        c.Surname, c.Forename, E.EventDate, E.StartTime, E.EndTime