0

所需客户的详细信息在这里

Core -- Division_ID = 1
Valid Email -- email LIKE '%@%'
Opt-In: Yes -- Consent_Status_ID = 4
Consent Type Description: Consent to send marketing email -- Consent_Type_ID = 3
Has made a booking -- I assume this will be done anyways becasue i will only display bookings of customers with greater to or equal to 4 and revenue generated from them greater to or equal to 4
Confirmed booking -- Booking_Status_ID = 1
Total Revenue >= 20k
Total Direct Bookings >= 4
Maximum Revenue >= 20k
Details of customers end here

表如下所示:

Customer
Customer_ID
Title
Initial
Firstname
email

Customer Consent
Customer_ID
consent_status_Id
consent_type_id
Household_ID

Booking
Customer_ID
Booking_ID
Total_Revenue
Customer_ID
Booking_Status_ID
Division_ID

Household
Household_ID
Surname

查询开始

Select distinct
        c.Customer_ID,
        c.Title, 
        c.Initial, 
        c.Firstname, 
        h.Surname,
        c.email,
        SUM(b.Total_Revenue) as 'Total Total Revenue',
        COUNT(b.Customer_ID) as 'Number of Bookings'

FROM Customer c INNER JOIN Household h ON c.Household_ID=h.Household_ID
                INNER JOIN Booking b ON c.Customer_ID=b.Customer_ID
                INNER JOIN Customer_consent cc ON cc.Customer_ID=c.Customer_ID

where b.Division_ID = 1 
and b.Booking_Status_ID = 1
and c.email LIKE '%@%'
and cc.consent_status_Id = 4
and cc.consent_type_id = 3
and (SELECT SUM(b.Total_Revenue) FROM Booking) >= 20000
and count(b.Customer_ID) >= 4
and MAX(b.Total_Revenue) >= 20000;

查询结束

我遇到的错误是:聚合可能不会出现在 WHERE 子句中,除非它位于 HAVING 子句或选择列表中包含的子查询中,并且被聚合的列是外部引用。

我的查询应该是什么?

谢谢!

4

2 回答 2

0

您的查询在选择中有聚合函数,因此您需要一个 group by 子句以任何方式:

仅供参考:未经测试

Select distinct
c.Customer_ID,
c.Title, 
c.Initial, 
c.Firstname, 
h.Surname,
c.email,
SUM(b.Total_Revenue) as 'Total Total Revenue',
COUNT(b.Customer_ID) as 'Number of Bookings'
FROM Customer c INNER JOIN Household h ON c.Household_ID=h.Household_ID
            INNER JOIN Booking b ON c.Customer_ID=b.Customer_ID
            INNER JOIN Customer_consent cc ON cc.Customer_ID=c.Customer_ID

where b.Division_ID = 1 
and b.Booking_Status_ID = 1
and c.email LIKE '%@%'
and cc.consent_status_Id = 4
and cc.consent_type_id = 3
GROUP BY c.Customer_ID,
c.Title, 
c.Initial, 
c.Firstname, 
h.Surname,
c.email,
HAVING 
(SELECT SUM(b.Total_Revenue) FROM Booking) >= 20000
and count(b.Customer_ID) >= 4
and MAX(b.Total_Revenue) >= 20000;
于 2013-08-01T12:28:16.470 回答
0

尝试这样的事情:

SELECT * 
FROM   (SELECT DISTINCT c.CUSTOMER_ID, 
                        c.TITLE, 
                        c.INITIAL, 
                        c.FIRSTNAME, 
                        h.SURNAME, 
                        c.EMAIL, 
                        Sum(b.TOTAL_REVENUE) AS [Total Total Revenue], 
                        Count(b.CUSTOMER_ID) AS [Number of Bookings], 
                        Max(b.TOTAL_REVENUE) AS MaxRevenue 
        FROM   CUSTOMER c 
               INNER JOIN HOUSEHOLD h 
                       ON c.HOUSEHOLD_ID = h.HOUSEHOLD_ID 
               INNER JOIN BOOKING b 
                       ON c.CUSTOMER_ID = b.CUSTOMER_ID 
               INNER JOIN CUSTOMER_CONSENT cc 
                       ON cc.CUSTOMER_ID = c.CUSTOMER_ID 
        WHERE  b.DIVISION_ID = 1 
               AND b.BOOKING_STATUS_ID = 1 
               AND c.EMAIL LIKE '%@%' 
               AND cc.CONSENT_STATUS_ID = 4 
               AND cc.CONSENT_TYPE_ID = 3) T 
WHERE  [TOTAL TOTAL REVENUE] >= 20000 
       AND [NUMBER OF BOOKINGS] >= 4 
       AND MAXREVENUE >= 20000; 
于 2013-08-01T12:30:53.953 回答