Netezza 聚合问题(枢轴)

发布于 2024-12-11 04:37:26 字数 601 浏览 0 评论 0原文

我正在使用 Netezza 生成报告,并且有以下查询

select  b.group_name,a.group_id, PASS_FAIL, COUNT(*) from log a
join group b on a.group_id=b.group_id
group by b.group_name,a.group_id, PASS_FAIL

它会收集有关表中通过和失败次数的信息,如下所示:

    GROUP_NAME  GROUP_ID    PASS_FAIL   COUNT
    GROUP1        4            FAIL     382
    GROUP1        4            PASS     339

如何更改查询以便可以使用 PASS 和 FAIL 作为列。

GROUP_NAME  GROUP_ID      PASS      FAIL
GROUP1        4           339       382

Netezza 中不提供 Pivot,因此我想知道解决此问题的最佳方法是什么。最好的方法是什么?

I'm using Netezza to generate a report and I have the following query

select  b.group_name,a.group_id, PASS_FAIL, COUNT(*) from log a
join group b on a.group_id=b.group_id
group by b.group_name,a.group_id, PASS_FAIL

It collects information on the number of passes and failures in a table like this:

    GROUP_NAME  GROUP_ID    PASS_FAIL   COUNT
    GROUP1        4            FAIL     382
    GROUP1        4            PASS     339

How can I change the query so that I can use PASS and FAIL as the columns.

GROUP_NAME  GROUP_ID      PASS      FAIL
GROUP1        4           339       382

Pivot isn't available in Netezza so I was wondering what the best way to go about this would be. What is the best way to do this?

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

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

发布评论

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

评论(2

优雅的叶子 2024-12-18 04:37:26

我没有使用 netezza 的直接经验,但从我读到的内容来看,它支持 SUM/CASE 的标准 SQL 解决方案

SELECT 
    GROUP_NAME,
    GROUP_ID,
    SUM(CASE WHEN PASS_FAIL = 'PASS' THEN 1 ELSE 0 END) as PASS,
    SUM(CASE WHEN PASS_FAIL = 'FAIL' THEN 1 ELSE 0 END) as FAIL
FROM 
    log a
    join group b 
    on a.group_id=b.group_id
GROUP BY 
    b.group_name,
    a.group_id

I don't have direct experience with netezza but from what I've read it supports the standard SQL solution of SUM/CASE

SELECT 
    GROUP_NAME,
    GROUP_ID,
    SUM(CASE WHEN PASS_FAIL = 'PASS' THEN 1 ELSE 0 END) as PASS,
    SUM(CASE WHEN PASS_FAIL = 'FAIL' THEN 1 ELSE 0 END) as FAIL
FROM 
    log a
    join group b 
    on a.group_id=b.group_id
GROUP BY 
    b.group_name,
    a.group_id
緦唸λ蓇 2024-12-18 04:37:26

只需对上面的查询的修改版本进行自连接,每个查询都会生成您需要的 passfail 列之一(请注意,我还没有测试过这是因为我没有安装 Netezza 来使用):

select group_name,group_id,pass,fail
from(
    select group_name,group_id,pass
    from(
        select  b.group_name,a.group_id, COUNT(*) as pass
        from log a
        join group b on a.group_id=b.group_id
        where pass_fail='PASS'
        group by b.group_name,a.group_id
        )x
    )p
join(
    select group_name,group_id,fail
    from(
        select  b.group_name,a.group_id, COUNT(*) as fail
        from log a
        join group b on a.group_id=b.group_id
        where pass_fail='FAIL'
        group by b.group_name,a.group_id
        )y
    )f
on(p.group_name=f.group_name and p.group_id=f.group_id);

Just do a self-join of modified versions of your query above, each of which produces one of the pass and fail columns that you need (note that I haven't tested this since I don't have an installation of Netezza to work with):

select group_name,group_id,pass,fail
from(
    select group_name,group_id,pass
    from(
        select  b.group_name,a.group_id, COUNT(*) as pass
        from log a
        join group b on a.group_id=b.group_id
        where pass_fail='PASS'
        group by b.group_name,a.group_id
        )x
    )p
join(
    select group_name,group_id,fail
    from(
        select  b.group_name,a.group_id, COUNT(*) as fail
        from log a
        join group b on a.group_id=b.group_id
        where pass_fail='FAIL'
        group by b.group_name,a.group_id
        )y
    )f
on(p.group_name=f.group_name and p.group_id=f.group_id);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文