【发布时间】: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