SQL CASE单个答案是或否

时间:2018-12-05 11:02:31

标签: sql-server

我希望您能为我提供帮助,我正在尝试编写一个SQL查询,以从预订中提取客户记录以形成预订清单,所有相对简单的信息,例如ID,名称,预订开始时间等。

然后,我想添加到该列表中,如果他们在“是/否”中购买了其他特定产品。

我尝试使用CASE语句完成此操作

CASE WHEN ProductID = '1234' THEN 'Yes' ELSE 'No' END AS 'Authorised'

但是现在它会为每个客户显示多个记录,其中包含该成员过去购买过的eash产品的状态

有没有一种方法可以将其限制为我要寻找的产品?并忽略所有其他产品?

以下是我的查询和数据示例:

 SELECT

ID
, StartDateTime
, ActivityID
, ActivityName
, MaxBookees
, MemberID
, FirstNames
, CASE WHEN ProductID = '1234' THEN 'YES' ELSE 'Ignore' END AS 'Authorised'

FROM

Booking INNER JOIN
                         BookingCustomers ON Booking.ID = BookingCustomers.BookingID INNER JOIN
                         Members ON BookingCustomers.CustomerID = Customers.ID INNER JOIN
                         Activities ON Bookings.ActivityID = Activities.ID  RIGHT JOIN
                         Products ON Members.ID = Products.MemberID

WHERE        

(Booking.StartDateTime >= DATEADD(d, 0, GETDATE())) AND (Bookings.StartDateTime <= DATEADD(d, 7, GETDATE())) AND (BookingCustomers.MemberID > '0')

目前的样子

ID  Start Date Time Activity ID Activity Name   Maximum Bookees Member ID   First Names Authorised
12345   07/12/2018 18:00    12345   Activity 1  8   1   John    NO
12345   07/12/2018 18:00    12345   Activity 1  8   1   John    NO
12345   07/12/2018 18:00    12345   Activity 1  8   2   James   YES
12345   07/12/2018 18:00    12345   Activity 1  8   2   James   NO
12345   07/12/2018 18:00    12345   Activity 1  8   3   Rose    NO
12346   07/12/2018 18:40    12346   Activity 1  8   4   Matthew NO
12347   07/12/2018 19:20    12347   Activity 2  8   5   Jonathan    YES
12348   07/12/2018 20:00    12348   Activity 3  8   6   Poppy   NO
12348   07/12/2018 20:00    12348   Activity 3  8   7   Katy    NO
12348   07/12/2018 20:00    12348   Activity 3  8   7   Katy    YES
12349   07/12/2018 20:40    12349   Activity 3  8   8   Nick    NO

我希望它看起来如何

ID  Start Date Time Activity ID Activity Name   Maximum Bookees Member ID   First Names Authorised
12345   07/12/2018 18:00    12345   Activity 1  8   1   John    NO
12345   07/12/2018 18:00    12345   Activity 1  8   2   James   YES
12345   07/12/2018 18:00    12345   Activity 1  8   3   Rose    NO
12346   07/12/2018 18:40    12346   Activity 1  8   4   Matt    NO
12347   07/12/2018 19:20    12347   Activity 2  8   5   Jon YES
12348   07/12/2018 20:00    12348   Activity 3  8   6   Poppy   NO
12348   07/12/2018 20:00    12348   Activity 3  8   7   Katy    YES
12349   07/12/2018 20:40    12349   Activity 3  8   8   Nick    NO

在此先感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

select子句中的列减去CASE表达式似乎定义了一个逻辑组。也就是说,出现的“重复”是/否记录对在这些列中具有所有相同的值。我怀疑您要在此处进行的操作是报告YES,如果ProductID = '1234'出现在每个组的任何记录中。如果是这样,我们可以GROUP BY上面提到的列,并使用条件聚合来尝试检测匹配的ProductID

SELECT
    ID,
    StartDateTime,
    ActivityID,
    ActivityName,
    MaxBookees,
    MemberID,
    FirstNames,
    CASE WHEN COUNT(CASE WHEN ProductID = '1234' THEN 1 END) > 0
         THEN 'YES' ELSE 'NO' END AS Authorised
FROM Booking b
INNER JOIN BookingCustomers bc
    ON b.ID = bc.BookingID
INNER JOIN Members m
    ON bc.CustomerID = bc.ID
INNER JOIN Activities a
    ON b.ActivityID = a.ID
LEFT JOIN Products p
    ON m.ID = p.MemberID
WHERE
    (Booking.StartDateTime >= DATEADD(d, 0, GETDATE())) AND
    (Bookings.StartDateTime <= DATEADD(d, 7, GETDATE())) AND
    (BookingCustomers.MemberID > '0')
GROUP BY
    ID,
    StartDateTime,
    ActivityID,
    ActivityName,
    MaxBookees,
    MemberID,
    FirstNames;

答案 1 :(得分:0)

 SELECT

ID
, StartDateTime
, ActivityID
, ActivityName
, MaxBookees
, MemberID
, FirstNames
, CASE WHEN ProductID = '1234' THEN 'YES' ELSE 'Ignore' END AS 'Authorised'

FROM

Booking INNER JOIN
                         BookingCustomers ON Booking.ID = BookingCustomers.BookingID INNER JOIN
                         Members ON BookingCustomers.CustomerID = Customers.ID INNER JOIN
                         Activities ON Bookings.ActivityID = Activities.ID  RIGHT JOIN
                         Products ON Members.ID = Products.MemberID

WHERE        

(Booking.StartDateTime >= DATEADD(d, 0, GETDATE())) AND (Bookings.StartDateTime <= DATEADD(d, 7, GETDATE())) AND (BookingCustomers.MemberID > '0') AND Products.ProductId == '1234'

在where子句中添加对产品的过滤。 where子句用于过滤搜索结果。结合添加distinct也可以有所帮助。您可能需要对其进行微调,但这将使您入门。