Details of customers needed are here
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
Tables look like this:
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
Query Start
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;
Query End
Error I came to was this: An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.
What should my query be?
Thanks!
Your Query has aggregate functions in the select, so you'll need a group by clause any way:
FYI: Untested
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;
Try something like this:
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.