【问题标题】:Count changes in table columns in same table row计算同一表行中表列的更改
【发布时间】:2020-11-19 13:04:27
【问题描述】:

我正在努力弄清楚如何做这样的事情。我有一个看起来像这样的 SQL 表。我想添加一列,该列总计每个产品在一年中拥有的服务商 ID 的数量。在名为“服务年数”的表中:

+-------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+----------------------+
| Product ID  | Jan Servicer ID | Feb Servicer ID | Mar Servicer ID | Apr Servicer ID | May Servicer ID | Jun Servicer ID | Jul Servicer ID | Aug Servicer ID | Sep Servicer ID | Oct Servicer ID | Nov Servicer ID | Dec Servicer ID | Years Servicer Count |
+-------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+----------------------+
| ID-001-2015 | SRVR-ID-01      | SRVR-ID-01      | SRVR-ID-01      | SRVR-ID-01      | SRVR-ID-01      | SRVR-ID-01      | SRVR-ID-01      | SRVR-ID-01      | SRVR-ID-01      | SRVR-ID-01      | SRVR-ID-02      | SRVR-ID-02      | 2                    |
+-------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+----------------------+
| ID-002-2015 | SRVR-ID-01      | SRVR-ID-01      | SRVR-ID-01      | SRVR-ID-01      | SRVR-ID-01      | SRVR-ID-01      | SRVR-ID-01      | SRVR-ID-01      | SRVR-ID-01      | SRVR-ID-01      | SRVR-ID-01      | SRVR-ID-01      | 1                    |
+-------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+----------------------+
| ID-002-2016 | SRVR-ID-01      | SRVR-ID-01      | SRVR-ID-01      | SRVR-ID-01      | SRVR-ID-02      | SRVR-ID-02      | SRVR-ID-02      | SRVR-ID-02      | SRVR-ID-02      | SRVR-ID-02      | SRVR-ID-03      | SRVR-ID-03      | 3                    |
+-------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+----------------------+

我正试图集中精力计算一列中不同值的数量。朝着正确方向的任何推动都会令人惊叹。

【问题讨论】:

  • 用您正在使用的数据库标记您的问题。如果列在行中而不是单独的列中,这将更容易回答。
  • 添加了 Netezza @Gordon Linoff
  • 记住:数据库表不是电子表格

标签: sql database count netezza unpivot


【解决方案1】:

一个选项在子查询中使用union all 将列取消透视到行,然后count(distinct):

select
    t.*,
    (
        select count(distinct servicer_id)
        from (
            select t.jan_servicer_id as servicer_id
            union all select t.feb_servicer_id
            union all select t.mar_servicer_id
            ...
            union all select t.dec_servicer_id
        ) t
    ) as years_servicer_count
from mytable t

如果对于 netezza 来说嵌套太多,那么另一种方法是连接:

select t.*, s.years_servicer_count
    
from mytable t
inner join (
    select product_id, count(distinct servicer_id) as years_servicer_count
    from (
        select product_id, jan_servicer_id as servicer_id from mytable
        union all select product_id, feb_servicer_id from mytable
        union all select product_id, mar_servicer_id from mytable
        ...
        union all select product_id, dec_servicer_id from mytable
    ) t
    group by product_id
) s on s.product_id = t.product_id

【讨论】:

  • 我曾考虑过这样做,但我有 100,000 多行。把它放回去会很糟糕。
  • @JoeWolf:我看不出有别的办法。主要问题在于表的设计(这些列应该是行),上面的查询试图解决这个问题。试一试。性能可能没那么差。
  • 100000 行在电子表格中仍然很多,但在 Netezza 上它被认为“非常小”:) union all 方法对我来说似乎完全没问题?
猜你喜欢
  • 1970-01-01
  • 2014-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多