[英]SQL Server query : selecting a total figure, based on a sub-query
I am trying to select total figures from my database table, using aggregate functions. 我正在尝试使用聚合函数从数据库表中选择总数。
The trouble is: one of the columns I need requires that I run a sub-query within the aggregate. 问题是:我需要的列之一要求我在聚合中运行子查询。 Which SQL does not allow. 哪个SQL不允许。
Here is the error I am getting : 这是我得到的错误:
Cannot perform an aggregate function on an expression containing an aggregate or a subquery. 无法对包含聚合或子查询的表达式执行聚合功能。
Here is the initial query : 这是初始查询:
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 method GROUP BY方法
(I used : " etc, etc, etc " to replace a large part of the query; I assume there is no need to write the entire code, as it is very long. But, the gist is clear) (我用:“ etc,etc,etc ”来替换查询的很大一部分;我认为不需要编写整个代码,因为它很长。但是要点很明显)
This query worked just fine. 该查询工作正常。
However, I need an extra field -- a field for those customers whose data are " clean " --- meaning : trimmed, purged of garbage characters (like : */?"#%), etc. 但是,我需要一个额外的字段-该字段用于那些数据“ 干净 ”的客户---表示:已修剪,清除了垃圾字符(例如:* /?“#%)等。
I have a query that does that. 我有一个查询可以做到这一点。 But, the problem is: how to insert this query into my already existing query, so I can create that extra column? 但是,问题是:如何将此查询插入到我现有的查询中,以便我可以创建该额外的列?
This is the query I am using to "clean" customer data : 这是我用来“清理”客户数据的查询:
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
This query works fine on its own. 此查询本身可以正常工作。
But, how to INSERT it into the initial query, to create a separate field, where I can get the TOTAL of those customers who meet this "clean" criteria? 但是,如何将其插入到初始查询,创建一个单独的领域,在那里我可以得到那些谁符合这个“干净”的标准客户总数是多少?
I tried it like this : 我这样尝试过:
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
and it gave me that "aggregate" error. 它给了我“汇总”错误。
SELECT COUNT(*)
instead of SUM()
, also, the WHERE
Clause to clean the data is awful. SELECT COUNT(*)
代替SUM()
,清理数据的WHERE
子句也很糟糕。 There has to be a better way. 一定有更好的方法。 Maybe mark the rows as clean when they're updated or as a batch job? 也许在更新或批处理作业时将其标记为干净的行?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.