使用2个阵列汇总并保留贡献信息

发布于 2025-02-02 23:19:58 字数 1187 浏览 2 评论 0原文

问题

想要汇总行,但仍然保留有关排成最后一笔敬意的信息。

该信息分为两个阵列,但无法弄清楚如何汇总信息并保留信息。

模式&示例数据(简化)

CREATE TABLE A 
(
    Groupkey Int32,
    Sizename String,
    Givenqty Array(int),
    Category Array(int)
) Engine = Memory ;
                
INSERT INTO A VALUES (1, 'XS', [1], [154]);               
INSERT INTO A VALUES (1, 'M', [1], [154]);               
INSERT INTO A VALUES (1, 'L', [1], [154]);                
INSERT INTO A VALUES (1, 'XL', [1,1,2,1,1], [145,145,146,154,145]);

SELECT * FROM A:

“示例数据”

所需的结果

我想要1个汇总行,其中“ vercyqty pr类别”的总和。

结果应为:(145 = 3,146 = 2,154 = 4):

“

进行查询时,我知道“类别”列的可能结果,因此我们可以使用此信息来添加值由索引(按类别Number排序)。

类别的数量可以是数据中的随机数,但是在查询数据时已知全范围(如果解决方案需要的话,可以用作帮助数组)。

我已经尝试了不同种类的组隔间,元组等,但似乎无法解决:/

Issue

Want to aggregate rows, but still keep information of how rows tributes to the final sum.

The information is in two arrays, but can't figure out how to aggregate it and keep information.

Schema & sample data (simplified)

CREATE TABLE A 
(
    Groupkey Int32,
    Sizename String,
    Givenqty Array(int),
    Category Array(int)
) Engine = Memory ;
                
INSERT INTO A VALUES (1, 'XS', [1], [154]);               
INSERT INTO A VALUES (1, 'M', [1], [154]);               
INSERT INTO A VALUES (1, 'L', [1], [154]);                
INSERT INTO A VALUES (1, 'XL', [1,1,2,1,1], [145,145,146,154,145]);

SELECT * FROM A:

Sample data

Desired result

I would like 1 aggregated row with the sum of "givenqty pr category".

The result should be: (145 = 3, 146 = 2, 154= 4):

Result

When doing the query I know the possible outcomes of the "Category" column, so we can use this information to add the value by index (sorted by categorynumber).

The number of categories can be random in the data, but the full range is known when querying the data (can be used as a helping array if solution requires it).

I've tried different kinds of groupArray, tuples etc, but can't seem to get I solved :/

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

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

发布评论

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

评论(1

静待花开 2025-02-09 23:19:58

尝试使用聚合函数 summsap 这样:

SELECT
    groupKey,
    sumMap(category, givenqty) AS summap,
    summap.2 AS result
FROM 
( 
    /* Emulate the test dataset. */
    SELECT
        data.1 AS groupKey,
        data.2 AS givenqty,
        data.3 AS category
    FROM 
    (        
        SELECT arrayJoin([(1, [1], [154]), (1, [1], [154]), (1, [1], [154]), (1, [1, 1, 2, 1, 1], [145, 145, 146, 154, 145])]) AS data
    )
)
GROUP BY groupKey

/*
┌─groupKey─┬─summap──────────────────┬─result──┐
│        1 │ ([145,146,154],[3,2,4]) │ [3,2,4] │
└──────────┴─────────────────────────┴─────────┘
*/

Try to use the aggregate function sumMap this way:

SELECT
    groupKey,
    sumMap(category, givenqty) AS summap,
    summap.2 AS result
FROM 
( 
    /* Emulate the test dataset. */
    SELECT
        data.1 AS groupKey,
        data.2 AS givenqty,
        data.3 AS category
    FROM 
    (        
        SELECT arrayJoin([(1, [1], [154]), (1, [1], [154]), (1, [1], [154]), (1, [1, 1, 2, 1, 1], [145, 145, 146, 154, 145])]) AS data
    )
)
GROUP BY groupKey

/*
┌─groupKey─┬─summap──────────────────┬─result──┐
│        1 │ ([145,146,154],[3,2,4]) │ [3,2,4] │
└──────────┴─────────────────────────┴─────────┘
*/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文