繁体   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