Topic: I ask to prompt how to obtain the data for last period
There are contracts on which there is a profit, but the contract formed on the basis of another for a long time closed on which too there was a profit.
The profit lies in the table of charges Summs
Id, Date, Sum, Id_Dog
1, 1/1/2010, 100, 1233
2, 1/1/2011, 200, 1233
3, 2/2/2012, 399, 1332
Contracts lie in the table with contracts doggs
Id_dog, d_start, d_end, Parent_dog
1233, 1/1/2000, 1/1/2012, NULL
1332, 1/2/2012, NULL, 1233
1300, 1/1/2000, 1/1/2012, NULL
The task: to obtain as a result of request the data and under the current contract, and on the previous.
I do so:
select sum (sum), id_dog from summs join doggs using (id_dog) where parent_dog is not null
But I do not understand, how it is possible to tighten also the total from the previous contract...
There was a thought to make through WITH, but do not understand how to connect through, let us assume, LAG\LEAD.