唯一值列表而不是计数 teradata sql

发布于 2025-01-09 08:15:35 字数 716 浏览 0 评论 0原文

我有 2 列;访问方法 ID、应用程序名称 每个用户不时使用不同的应用程序,我需要编写一个查询来获取用户在一列中使用过的所有应用程序的列表。 像这样

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 有没有办法编写查询来获得所需的结果?

I have 2 columns; access_method-id, app_name
Each user uses different apps from time to time and I need to write a query to get a list of all the apps that the user have used in one column.
like this

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

Data in the table on which I am running the query looks like this

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

I am using this query on Teradata from Dbeaver

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

this query gives me a count of the apps, I need to get the list. Using Teradata SQL is there a way to write a query to get the desired results?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

握住我的手 2025-01-16 08:15:35

使用 XMLAGG()

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

use XMLAGG()

SELECT acess_method_id,
XMLAGG(App_Name || ',' order by App_Name)
FROM your_table
GROUP BY acess_method_id;
此生挚爱伱 2025-01-16 08:15:35

您想要的是类似于标准 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
              )
    );

您还可以将不同的值移至 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
              )
    );
    

检查哪个版本更有效。

What you want is similar to Standard SQL's ListAgg function, which is not implemented in Teradata.

But nPath can return this result, too. You have to get used to the syntax, but it's way more efficient than 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
              )
    );

You can also move the distinct into the RESULT functions like this:

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
              )
    );
    

Check which version is more efficient.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文