【问题标题】:How can I return desired record when using pivot使用数据透视时如何返回所需的记录
【发布时间】:2020-02-25 01:18:41
【问题描述】:

我正在使用数据透视表来返回过去 7 天内更新的记录。如果帐户已更新,则仅返回 ID 的 contype 'AAA' 和 'BBB'。每个 ID 最多可以有 8 个“类型”。我只想返回 cont_desc= 01 但 contypes 不是 AAA 或 BBB 的记录,cont_desc 不必是 01。话虽如此,我希望数据透视表中的所有 cont_desc 都是 01。这有点难以解释,所以我举个例子:

表格可能如下所示:

ID    contype     date         contdesc
1     AAA         2020-01-30   01
1     AAA         2019-05-05   0002
1     BBB         2020-01-02   01
1     CCC         2020-02-23   38372
2     AAA         2020-01-22   93021
2     AAA         2020-01-30   01
2     BBB         2019-03-09   01
2     DDD         2020-02-20   92821

注意,ID 1 将被拉取,因为 CCC 在过去 7 天内最后一次更新,因此它会返回 ID 的 AAA 和 BBB 记录。 另请注意,ID 2 将被拉出,因为 contype DDD 在过去 7 天内已更新,但它会拉出 AAA,id_cont_desc = 93021。我希望记录始终为 01。我不能在 where 子句中包含这个,因为不是 AAA 或 BBB 的记录不必是 01。

SELECT *
FROM( SELECT
        id_nbr AS ID,
        contact_type AS contype,
        contact_id_desc AS contdesc
      FROM table
      WHERE ((contact_type = 'AAA' OR contact_type = 'BBB' AND cont_id_desc = '01') OR contact_type NOT = 'AAA' OR 
        contact_type NOT = 'BBB'
        AND (last_update >= CURRENT_DATE - INTERVAL '7' DAY)
        QUALIFY ROW_NUMBER() OVER (PARTITION BY id_nbr, contact_type
        ORDER BY last_update) = 1) as internal_select
      PIVOT (MAX(contype) contype,MAX(contdesc) contdesc FOR contype IN ('AAA','BBB')) derived_Pilot;

它可能会返回如下内容:

  ID  'AAA'_contype     'AAA'_contdesc   'BBB'_contype     'BBB'_contdesc
  1    AAA              01                BBB               01
  2    AAA              93021             BBB               01

记录 1 看起来很棒。所有 contdesc 都等于 01。注意记录 2,'AAA'_contdesc = 93021。 这是因为记录不等于 AAA 或 BBB(但在过去 7 天内已更新)但返回 contype AAA 且 contdesc 不等于 01。我提前道歉,这有点令人困惑,但我尽力了.

欢迎提出任何建议!提前致谢!

【问题讨论】:

    标签: sql teradata


    【解决方案1】:

    这是你想要的吗?

    select t.*
    from (select t.*,
                 max(date) over (partition by id) as max_date
          from t
         ) t
    where max_date >= CURRENT_DATE - INTERVAL '7' DAY) and
          contract_type in ('AAA', 'BBB') and
          contdesc = '01';
    

    那么您想要的结果似乎是:

    select id,
           max(case when contract_type = 'AAA' then contract_type end) as AAA_contract_type,
           max(case when contract_type = 'AAA' then contdesc end) as AAA_contdesc,
           max(case when contract_type = 'BBB' then contract_type end) as BBB_contract_type,
           max(case when contract_type = 'BBB' then contdesc end) as BBB_contdesc
    from (select t.*,
                 max(date) over (partition by id) as max_date
          from t
         ) t
    where max_date >= CURRENT_DATE - INTERVAL '7' DAY) and
          contract_type in ('AAA', 'BBB') and
          contdesc = '01'
    group by id;
    

    【讨论】:

    • 感谢您的回复。现在试试这个!很快就会回来找你!
    【解决方案2】:

    您会得到错误的结果,因为您将 AND 和 OR 混合在一起,而优先顺序是 NOT - AND - OR。您当前的查询结果:

    WHERE 
      ((contact_type = 'AAA'
         OR (contact_type = 'BBB' AND cont_id_desc = '01')
       )
      OR contact_type NOT = 'AAA'
      OR (contact_type NOT = 'BBB'
           AND (last_update >= Current_Date - INTERVAL '7' DAY))
    

    这可能不是你想要的。顺便说一句,您的选择、剪切和粘贴错误中缺少)

    在混合 AND/OR 时应始终添加括号,另外 contact_type = 'AAA' OR contact_type = 'BBB' 应简化为 contact_type IN ('AAA', 'BBB')

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-22
      相关资源
      最近更新 更多