【问题标题】:count distinct and window functions计算不同和窗口函数
【发布时间】:2016-10-23 14:15:54
【问题描述】:

我有一份 ID、交易、这些交易的日期以及这些交易的类别的列表。我想在每个 ID 中创建每个不同类别的计数

我的起始表是这样的:

id  trxn_dt     trxn_amt trxn_category
1   10/31/2014  58       apple
1   11/9/2014   34       banana
1   12/10/2014  12       apple
2   7/8/2014    78       banana
2   11/20/2014  99       banana
3   1/5/2014    120      orange
4   2/17/2014   588      apple
4   2/18/2014   8        banana
4   3/9/2014    65       orange
4   4/25/2014   74       apple

我希望最终结果看起来像这样:

id  trxn_dt     trxn_amt trxn_category  number_category
1   10/31/2014  58       apple          2
1   11/9/2014   34       banana         2 
1   12/10/2014  12       apple          2
2   7/8/2014    78       banana         1
2   11/20/2014  99       banana         1
3   1/5/2014    120      orange         1
4   2/17/2014   588      apple          3
4   2/18/2014   8        banana         3
4   3/9/2014    65       orange         3
4   4/25/2014   74       apple          3

我已尝试使用 count(distinct(trxn_category)) over(partition by id,trxn_category order by id) as number_category,但在使用 'distinct' 时出现错误

【问题讨论】:

  • exact 错误信息是什么?您使用的是哪个 DBMS? (Postgres?Oracle?SQL Server?)。此外:distinct 不是函数。

标签: sql distinct window-functions


【解决方案1】:

大多数 DBMS 不支持窗口函数中的 DISTINCT,但您可以使用两个 DENSE_RANK 模拟 COUNT(DISTINCT):

DENSE_RANK() over (partition by id,trxn_category order by id ASC)-
DENSE_RANK() over (partition by id,trxn_category order by id DESC)

或嵌套的 MAX(DENSE_RANK):

select
   MAX(dr) over (partition by id,trxn_category)
from
 (
   select 
      DENSE_RANK() over (partition by id,trxn_category order by id DESC) as dr
 ) 

【讨论】:

    【解决方案2】:
    ;WITH cteCounts AS (
        SELECT
           id
           ,COUNT(DISTINCT trxn_category) as CategoryCount
        FROM
           table
        GROUP BY
           id
    )
    
    
    SELECT
        t.id
        ,trxn_dt
        ,trxn_amt
        ,trxn_category
        ,c.CategoryCount
    FROM
        table t
        INNER JOIN cteCounts c
        ON t.id = c.id
    

    不能在分区聚合中使用 DISTINCT。

    【讨论】:

      【解决方案3】:

      您可以为此使用相关子查询:

      SELECT id, trxn_dt, trxn_amt, trxn_category,
             (SELECT COUNT(DISTINCT trxn_category)
              FROM mytable AS t2
              WHERE t2.id = t1.id) AS cnt
      FROM mytable AS t1
      

      Demo here

      【讨论】:

        猜你喜欢
        • 2012-11-08
        • 2020-02-09
        • 2023-03-30
        • 1970-01-01
        • 2021-03-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-07
        • 2020-06-23
        相关资源
        最近更新 更多