如何运行此SQL命令?

时间:2013-08-01 12:24:48

标签: sql sql-server

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

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子句或选择列表中包含的子查询中,并且要聚合的列是外部引用。

我的查询应该是什么?

谢谢!

2 个答案:

答案 0 :(得分:0)

您的查询在select中有聚合函数,因此您需要以任何方式使用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;

答案 1 :(得分: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;