Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
154 views
in Technique[技术] by (71.8m points)

sql - how to use Analytic functions instead of join with itself

i want to find the credit/debit side of a transaction in a table like this.

with t1 as
(select 1 v, 11 t, 100 amnt from dual
 union
 select 1 v, 12 t, 200 amnt from dual
 union
 select 1 v, 13 t, -100 amnt from dual
 union
 select 1 v, 14 t, -200 amnt from dual
)
select * from t1

in this table, there is a transaction with 4 articles. 11 receives 100$ from 13 and 12 receives 200$ from 14. one solution is join t1 with itself. this is heavy when the records are very much, like 100 million records per day. the problem in joining this table with itself is heavy process time. i am looking for a solution with Analytic functions to find the sender or receiver of a article. Can you give a solution for reaching this goal.

Thanks.

question from:https://stackoverflow.com/questions/65879689/how-to-use-analytic-functions-instead-of-join-with-itself

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If this sample T1 table represents data you really have, it is less than obvious that 11 receives 100$ from 13. Nothing but your words suggests that. Therefore, if you want to make it "obvious" and "faster", add another column which explicitly says that, makes a referential integrity constraint (possible within the same table; why not?), create additional table with pairs of "who received what from whom", or whatever else so that you wouldn't have to write queries which suffer from a wrong data model.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...