【问题标题】:List of unique values instead of count teradata sql唯一值列表而不是计数 teradata sql
【发布时间】:2022-02-23 23:27:11
【问题描述】:

我有 2 列; access_method-id, app_name 每个用户不时使用不同的应用程序,我需要编写一个查询来获取用户在一列中使用过的所有应用程序的列表。 像这样

acess_method_id  |  App_Name
12345            | [bima,gaming,tube]
34579            | [candy,bubbles,gaming,tube]

我运行查询的表中的数据如下所示

acess_method_id  |  App_Name
    12345        | bima
    12345        | gaming
    12345        | tube
    34579        | candy
    34579        | bubbles
    34579        | gaming
    34579        | tube

我在 Dbeaver 的 Teradata 上使用这个查询

Select COUNT(DISTINCT App_Name),ACCESS_METHOD_ID 
from DP_VEW.mytable as a 
GROUP BY ACCESS_METHOD_ID

此查询为我提供了应用程序的数量,我需要获取列表。使用 Teradata SQL 有没有办法编写查询以获得所需的结果?

【问题讨论】:

  • 请输入您的 dbms 名称
  • 由于您指定了 Teradata,我删除了 SQLServer TAG
  • @ZaynulAbadinTuhin 使用 dBeaver
  • 我见过人们在这里使用递归 cte。
  • 您的 Teradata 版本是什么?

标签: sql teradata dbeaver teradata-sql-assistant teradatasql


【解决方案1】:

使用 XMLAGG()

SELECT acess_method_id,
XMLAGG(App_Name || ',' order by App_Name)
FROM your_table
GROUP BY acess_method_id;

【讨论】:

    【解决方案2】:

    您想要的类似于标准 SQL 的 ListAgg 函数,该函数未在 Teradata 中实现。

    但是 nPath 也可以返回这个结果。您必须习惯语法,但它比 XMLAGG 更有效。

    SELECT * 
    FROM 
       NPath
        (
          ON (
               SELECT DISTINCT acess_method_id, App_Name
               FROM mytable
             ) AS dt                           -- input data
          PARTITION BY acess_method_id         -- group by column(s)
          ORDER BY App_Name                    -- order within list
          USING
            MODE (NonOverlapping)              -- required syntax 
            Symbols (True AS T)                -- every row
            Pattern ('T*')                     -- is returned
            RESULT(First (acess_method_id OF T) AS acess_method_id, -- group by column
                   Count (App_Name OF T) AS Cnt,
                   Accumulate(App_Name OF ANY (T) Delimiter ',') AS ListAgg
                  )
        );
    

    您还可以将 distinct 移动到 RESULT 函数中,如下所示:

    SELECT * 
    FROM 
       NPath
        (
          ON (
               SELECT acess_method_id,  App_Name
               FROM mytable
             ) AS dt                           -- input data
          PARTITION BY acess_method_id         -- group by column(s)
          ORDER BY App_Name                    -- order within list
          USING
            MODE (NonOverlapping)              -- required syntax 
            Symbols (True AS T)                -- every row
            Pattern ('T*')                     -- is returned
            RESULT(First (acess_method_id OF T) AS acess_method_id, -- group by column
                   Count (DISTINCT App_Name OF T) AS Cnt,
                   Accumulate(DISTINCT App_Name OF ANY (T) Delimiter ',') AS ListAgg
                  )
        );
        
    

    检查哪个版本更有效。

    【讨论】:

      猜你喜欢
      • 2019-12-12
      • 2018-02-12
      • 1970-01-01
      • 2022-11-11
      • 1970-01-01
      • 1970-01-01
      • 2016-02-11
      • 1970-01-01
      • 2021-04-25
      相关资源
      最近更新 更多