簡體   English   中英

SQL Server查詢:根據子查詢選擇總數

[英]SQL Server query : selecting a total figure, based on a sub-query

我正在嘗試使用聚合函數從數據庫表中選擇總數。

問題是:我需要的列之一要求我在聚合中運行子查詢。 哪個SQL不允許。

這是我得到的錯誤:

無法對包含聚合或子查詢的表達式執行聚合功能。

這是初始查詢:

 select 
     method, 
     sum(payment_id) as payment_id, 
     sum(status) as status,     
     sum(allowEmailContact) as allowEmailContact, 
     sum(allowPhoneContact) as allowPhoneContact, 
     sum(totalReservations) as totalReservations 
from 
    (SELECT 
         RES.method, count(*) as payment_id, 
         '' as status, '' as complete_data, 
         '' as allowEmailContact, '' as allowPhoneContact,
         '' as totalReservations
     FROM 
         Customer CUS
     INNER JOIN 
         Reservation RES ON CUS.id = RES.customerId
     WHERE 
         (RES.created > '2015-05-31 23:59' and RES.created <= '2015-06-15   
 23:59')
         AND RES.payment_id IS NOT NULL
         AND scope_id = 1
     GROUP BY 
         RES.method

     UNION ALL

      etc
      etc
    ) AS results 

GROUP BY方法

(我用:“ etc,etc,etc ”來替換查詢的很大一部分;我認為不需要編寫整個代碼,因為它很長。但是要點很明顯)

該查詢工作正常。

但是,我需要一個額外的字段-該字段用於那些數據“ 干凈 ”的客戶---表示:已修剪,清除了垃圾字符(例如:* /?“#%)等。

我有一個查詢可以做到這一點。 但是,問題是:如何將此查詢插入到我現有的查詢中,以便我可以創建該額外的列?

這是我用來“清理”客戶數據的查詢:

select * 
from dbo.Customer 
where 
    Len(LTRIM(RTRIM(streetAddress))) > 5 and   
    Len(LTRIM(RTRIM(streetAddress))) <> '' and 
    (Len(LTRIM(RTRIM(streetAddress))) is not null and 
    Len(LTRIM(RTRIM(postalCode))) = 5 and postalCode <> '00000' and  
    postalCode <> '' and Len(LTRIM(RTRIM(postalCode))) is not null and 
    Len(LTRIM(RTRIM(postalOffice))) > 2 and 
    phone <> '' and  Len(LTRIM(RTRIM(email))) > 5 and 
    Len(LTRIM(RTRIM(email))) like '@' and 
    Len(LTRIM(RTRIM(firstName))) > 2 and Len(LTRIM(RTRIM(lastName))) > 2) and
    Len(LTRIM(RTRIM(firstName))) <> '-' and Len(LTRIM(RTRIM(lastName))) <>  '-' and
    Len(LTRIM(RTRIM(firstName))) is not null and 
    Len(LTRIM(RTRIM(lastName))) is not null
    etc, etc

此查詢本身可以正常工作。

但是,如何將其插入到初始查詢,創建一個單獨的領域,在那里我可以得到那些誰符合這個“干凈”的標准客戶總數是多少?

我這樣嘗試過:

select 
    method, 
    sum(payment_id) as payment_id, 
    sum(status) as status, 
    SUM((select * 
         from dbo.Customer 
         where 
            Len(LTRIM(RTRIM(streetAddress))) > 5 and   
            Len(LTRIM(RTRIM(streetAddress))) <> '' and 
            (Len(LTRIM(RTRIM(streetAddress))) is not null and 
            Len(LTRIM(RTRIM(postalCode))) = 5 and 
            postalCode <> '00000' and postalCode <> '' and 
            Len(LTRIM(RTRIM(postalCode))) is not null and 
            Len(LTRIM(RTRIM(postalOffice))) > 2 and phone <> '' and 
            Len(LTRIM(RTRIM(email))) > 5 and 
            Len(LTRIM(RTRIM(email))) like '@' and 
            Len(LTRIM(RTRIM(firstName))) > 2 and 
            Len(LTRIM(RTRIM(lastName))) > 2) and 
            Len(LTRIM(RTRIM(firstName))) <> '-' and 
            Len(LTRIM(RTRIM(lastName))) <> '-' and 
            Len(LTRIM(RTRIM(firstName))) is not null and  
            Len(LTRIM(RTRIM(lastName))) is not null)  ) as clean_data,
    sum(allowEmailContact) as allowEmailContact, sum(allowPhoneContact) as   allowPhoneContact, 
    sum(totalReservations) as totalReservations 
from 
    (SELECT 
        RES.method, count(*) as payment_id, '' as status, 
        '' as complete_data, '' as allowEmailContact, 
        '' as allowPhoneContact, '' as totalReservations
     FROM Customer CUS
     INNER JOIN Reservation RES ON CUS.id = RES.customerId
     WHERE (RES.created > '2015-05-31 23:59' and RES.created <= '2015-06-15   
  23:59')
       AND RES.payment_id is not null and scope_id = 1
     GROUP BY RES.method

     UNION ALL

     etc
     etc
     etc

它給了我“匯總”錯誤。

SELECT COUNT(*)代替SUM() ,清理數據的WHERE子句也很糟糕。 一定有更好的方法。 也許在更新或批處理作業時將其標記為干凈的行?

暫無
暫無

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

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