所需客户的详细信息在这里
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 子句或选择列表中包含的子查询中,并且被聚合的列是外部引用。
我的查询应该是什么?
谢谢!