选择另一列必须与两个特定选项之一匹配的地方

时间:2018-10-10 13:57:26

标签: sql postgresql

我正在尝试从单个表中的一列获取值,其中另一列可以匹配两种情况之一。

我的桌子如下:

CREATE TABLE user_event (
  id SERIAL Primary Key,
  session_id uuid NOT NULL,
  event_name text NOT NULL,
  event_value text NOT NULL
);

-- Test Values
INSERT INTO user_event(session_id, event_name, event_value)
VALUES ('0971ad0b-0b98-4f09-bc93-9ae15c45941b', 'Heading.Click', 'test'),
('0971ad0b-0b98-4f09-bc93-9ae15c45941b', 'Subheading.Click', 'test'),
('0971ad0b-0b98-4f09-bc93-9ae15c45941b', 'Paragraph.Click', 'test'),
('6a627852-5f2a-4884-9949-cf11edf73628', 'Heading.Click', 'test'),
('1e5ec268-34ea-464a-82d1-2cf1e1425df3', 'Heading.Click', 'test'),
('1e5ec268-34ea-464a-82d1-2cf1e1425df3', 'Subheading.Click', 'test'), 
('1e5ec268-34ea-464a-82d1-2cf1e1425df3', 'Page.Impression', 'test');

-- Result
('0971ad0b-0b98-4f09-bc93-9ae15c45941b', '6a627852-5f2a-4884-9949-cf11edf73628')

我要(伪sql):

SELECT session_id
FROM user_event
WHERE event_name IS Heading.Click
AND other event_name's for the same session_id don't Contain Subheading.Click
OR 
event_name IS Heading.Click
AND other event_name's for the same session_id must contain both (Subheading.Click,
Paragraph.Click)

到目前为止,我已经提出了一种效率极低的方法

SELECT session_id
FROM user_event
WHERE event_name = 'Heading.Click'
AND 
(session_id not in (select session_id from user_event where event_name != 'Heading.Click')
OR
(session_id in (select session_id from user_event where event_name = 'Subheading.Click')
AND session_id in (select session_id from user_event where event_name = 'Paragraph.Click')
));

这是我一直用来测试的小提琴设置。 http://sqlfiddle.com/#!17/a55a3/1

1 个答案:

答案 0 :(得分:2)

您可以将查询重写为

SELECT session_id
FROM user_event
GROUP BY session_id
HAVING 
    SUM(CASE WHEN event_name = 'Heading.Click' THEN 1 ELSE 0 END) >0
    AND (
        SUM(CASE WHEN event_name = 'Subheading.Click' THEN 1 ELSE 0 END) =0
        OR (
            SUM(CASE WHEN event_name = 'Subheading.Click' THEN 1 ELSE 0 END) > 0
            AND 
            SUM(CASE WHEN event_name = 'Paragraph.Click' THEN 1 ELSE 0 END) > 0
        ) 
    )

不需要子查询,您可以使用条件聚合来匹配每个会话ID的条件。

Demo