簡體   English   中英

SQL僅在總和大於0時選擇所有記錄

[英]SQL select all records only if the sum is greater than 0

我有以下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安德魯

表結構

    [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,

嘗試將查詢的WHERE部分更改為以下內容:

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

也許:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM