Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here i got an example for the table
TABLE
=====
id_user | equity | schedule
123 | 2000 | 2015-09-29
124 | 2000 | 2015-09-29
125 | 2000 | 2015-09-29
126 | 2000 | 2015-09-29
127 | 2000 | 2015-09-29
123 | 3000 | 2015-09-28
124 | 3000 | 2015-09-28
125 | 1000 | 2015-09-28
126 | 1000 | 2015-09-28
127 | 2000 | 2015-09-28

what i want is something like this after the query,

id_user | summary
123 | -1000
124 | -1000
125 | 1000
126 | 1000
127 | 0

so the formula is the equity from date 29 - the equity from date 28
how can i do that ?
thanks. please teach me the query.
Posted
Comments
CHill60 8-Oct-15 14:01pm    
Are you using SQL or PostgreSQL and which version?

If date are fixed or you are going to pass these someway to the query then you can ctry this-
SQL
SELECT T1.id_user, T1.equity-T2.equity AS summary
FROM YourTable T1
LEFT JOIN YourTable T2 ON T1.id_user=T2.id_user
WHERE T1.schedule='2015-09-29' AND T2.schedule='2015-09-28'


If you want it to make dynamic, please let me know.

Hope, it helps :)
 
Share this answer
 
Comments
Andrew Budiman 8-Oct-15 14:08pm    
Thanks very much, as if your query is very helpful to me.
Andrew Budiman 8-Oct-15 14:23pm    
Hi Suvendu,

what if the scenario become like this,

id_user | equity | schedule
123 | 2000 | 2015-09-29
124 | 2000 | 2015-09-29
125 | 2000 | 2015-09-29
126 | 2000 | 2015-09-29
127 | 2000 | 2015-09-28
123 | 3000 | 2015-09-28
124 | 3000 | 2015-09-28
125 | 1000 | 2015-09-28
126 | 1000 | 2015-09-28
127 | 2000 | 2015-09-27


id_user | summary | schedule
123 | -1000 | 2015-09-29
124 | -1000 | 2015-09-29
125 | 1000 | 2015-09-29
126 | 1000 | 2015-09-29
127 | 0 | 2015-09-28

how can i do that ?

thanks.
what if the scenario become like this,

id_user | equity | schedule
123 | 2000 | 2015-09-29
124 | 2000 | 2015-09-29
125 | 2000 | 2015-09-29
126 | 2000 | 2015-09-29
127 | 2000 | 2015-09-28
123 | 3000 | 2015-09-28
124 | 3000 | 2015-09-28
125 | 1000 | 2015-09-28
126 | 1000 | 2015-09-28
127 | 2000 | 2015-09-27


id_user | summary | schedule
123 | -1000 | 2015-09-29
124 | -1000 | 2015-09-29
125 | 1000 | 2015-09-29
126 | 1000 | 2015-09-29
127 | 0 | 2015-09-28

how can i do that ?

thanks.
 
Share this answer
 
Comments
CHill60 9-Oct-15 9:42am    
If you want to add information to your question use the Improve question link. Don't post further questions as solutions.
I asked you 19 hours ago whether you were using SQL or PostgreSQL and which version - you need to be looking at previous rows not hard-coding dates, but I need to know which version in order to determine a solution
Andrew Budiman 9-Oct-15 11:45am    
oops i'm sorry, its my bad, i'm using PostgreSQL version 9.4.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900