如何编写查询以过滤SQL

时间:2018-06-07 08:48:52

标签: sql oracle

我有一个关于过滤SQL表的问题。

我有一个用户可以注册的网站,每次注册都会生成一个唯一的客户ID。

我正在进行测试,用户将被分为不同类型的细分(新访问者,回访者,现有客户)。每个用户细分的体验都将是个性化的。

现在我想分析结果。我可以获取每个用户及其各自客户群的细分信息。但事情是用户可以动态地切换到用户细分,例如,您可以成为新访客并注册,然后再成为现有客户。

问题是我想知道从现有客户那里获得了多少增量注册。 我可以创建一个新用户细分公司ID表并返回用户细分公司ID。现在,我想创建一个现有客户表,其中公司ID 不是来自新用户和回访者。在这种情况下我应该使用什么类型的连接条件?

以下是表格示例:

新访客表

Segment name                 | Company ID
-----------------------------+------------
New Visitor- Control         | abcde     
New Visitor- Variation       | ffgg12

返回访客表

Segment name                       | Company ID
-----------------------------------+------------
Returning Visitor- Control         | Gg121     
Returning Visitor- Variation       | cc4234

我想创建一个表,其中公司ID不在上面的两个表中。

非常感谢提前。

- 编辑 - 这是我要创建的查询:

WITH segment_info AS(SELECT date,segment_id,visitor_id FROM main_table WHERE(segment_id LIKE' new_visitor'或segment_id LIKE' returning_visitor' OR segment_id LIKE' existing_customer') AND date> =' 2018-06-01'),                                                                                                                 newvisitor AS(SELECT DISTINCT visitor_id,segment_id,date)FROM main_table WHERE segment_id =' new_visitor'),

returnvisitor AS(SELECT DISTINCT visitor_id,segment_id,date)FROM main_table WHERE segment_id =' returning_visitor'),

existing_customer AS(SELECT DISTINCT visitor_id,segment_id,date)FROM main_table WHERE segment_id =' existing_customer' AND visitor_id NOT IN(SELECT visitor_id FROM newvisitor UNION ALL SELECT visitor_id FROM returnvisitor))

- 问题是新访客+返回访客+现有客户的数量与所有访客的总数不匹配。

2 个答案:

答案 0 :(得分:1)

  

我想创建一个公司ID不在上面的表格   表。

如果要筛选出位于这些表之一(或两个表中)的公司,请使用UNION ALL运算符:

SELECT * FROM some_table
WHERE ID Not In (
  SELECT Company_ID FROM New_Visitor_Table
  UNION ALL
  SELECT Company_ID FROM Returning_Visitor_Table
)

如果要同时过滤掉两个表中的公司,请使用JOIN:

SELECT * FROM some_table
WHERE ID Not In (
  SELECT Company_ID 
  FROM New_Visitor_Table
  JOIN Returning_Visitor_Table
  USING ( Company_ID  )
)

答案 1 :(得分:0)

很难得到你想要实现的东西。这就是为什么有两种不同的解决方案。

基于您最初编写的内容和示例表

如果您需要为两个表中的客户创建表格,则提供两个不同的表格,其中包含由公司ID标识的客户,您的查询将需要如下内容:< / p>

select
    date,
    segment_id,
    visitor_id
from
    main_table t
where
    lower(t.segment_id) like ('%existing_customer%')
    and t.date >= '2018-06-01'
    and not exists (
        select null 
        from new_visitor_table
        where visitor_id = t.visitor_id
        union all
        select null 
        from returning_visitor_table
        where visitor_id = t.visitor_id
    );

或者,如果您只使用主表而不分开访问:

select
    date,
    segment_id,
    visitor_id
from
    main_table t
where
    lower(t.segment_id) like ('%existing_customer%')
    and t.date >= '2018-06-01'
    and not exists (
        select null 
        from main_table
        where lower(segment_id) not like '%new_visitor%'
            and lower(segment_id) not like '%returning_visitor%'
            and visitor_id = t.visitor_id
    );

基于我尝试了解您的查询

我认为正确的看起来像这样:

select
    date,
    segment_id,
    visitor_id
from
    main_table t
where
    t.segment_id = 'existing_customer'
    and t.date >= '2018-06-01'
    and not exists (
        select null 
        from main_table
        where segment_id in ('new_visitor', 'returning_visitor')
            and visitor_id = t.visitor_id
    );

所以,我不确定哪个适合你,但希望他们中的一个会引导你达到你原来的目标。