我有一个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
尝试,但我得到一个错误有关使用“独立的”
是什么***确切的***错误信息?你正在使用哪些DBMS? (Postgres?Oracle?SQL Server?)。另外:'distinct'是**不是**的函数。 –