计数独特的+带有条件SQL Oracle的窗口功能
我有一个数据表如下:
customerId | contruct_id | value_date |
---|---|---|
a | 1234 | 01-JUL-20 |
A | 7896 | 20-DEC-20-DEC-20 |
C | 6578 | 01-JUN-20 |
C | 8990 C 8990 | 20-OCT-20-OCT-20 |
C | 4789 | 20-DEC-21 |
B | 3457 09--3457 | 09--3457 09--3457 09--3457 09- 9月21日, |
我尝试在每个唯一 customeriD
的每个 value_date
之前尝试计算 contract_id
的不同数量。
所需的结果是:
customerId | value_date | count_contract |
---|---|---|
a | 01-Jul-20 | 0 |
A | 20-DEC-20 | 1 |
C | 01-JUN-20 | 0 |
C | 20-OCT-20 | 1 |
C | 20-DEC 20-DEC-21 21 | 2 |
B | 09-SEP-21 | 0 |
Could anyone suggest how I could count distinct in this case?
I tried the window functions:
select distinct CustomerID,
Value_date,
count(distinct Contract_id) over (partition by CustomerID order by Value_date rows unbounded preceding) count_contract
from tbl_cus_contract;
但是它没有错误的错误:
错误报告 - SQL错误:ORA -30487:在此处不允许订购30487。00000-“不允许在此处订购” *原因:不同的功能和比率_to_report不能有订单 *行动:
谢谢大家的答案!!! 我发现一种更好的方式来计算不同的方式,因为同一customerID的几个不同的合同_id具有相同的value_date。
我正在使用等级()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以查看给定客户的第一次出现
Contract_id
。然后仅计算第一次出现。
db<> fiddle:
You can check the first occurrence of the
contract_id
for a given customer.Then Count only the first occurrence.
db<>fiddle: Try here
您的预期结果似乎不需要计数不同的合同_id,如果这是正确的,那么您可以省略在计数中使用不同的使用,而查询将起作用。
Your expected result does not appear to require counting distinct contract_id's and if that is true then you could just omit the use of distinct within the count and your query would work.
这可以使用
Model
子句来完成:db&lt;&gt;&gt;
:如果多个
Contract_id
每个日期,您可以添加唯一的单个参考
,因此模型
将不符合重复的维度值。请参阅更新的代码和结果:db&lt;&gt;&gt;
This may be done with a
model
clause:db<>fiddle here
UPD: In case of multiple
contract_id
per date you may addunique single reference
, somodel
will not comply on duplicated dimension values. See updated code and a result:db<>fiddle here