2

I have following situation where i need to get several values between two invoices date.

So query is giving data based on invoices now what i need to do is for some values fetch data between this invoice date and last invoice date

already tried ways 1) sub query will easily solve this but as i have to do this for 4-5 column and its a 15 gb database so that's not possible. 2) if i go like this

left join (select inv.date ,inv,actno from invoice inv) as invo on invo.actno=act.id and invo.date < inv.date

then it will give all the data less then that date but i need only one data that will be less than main invoice date.

3) we can not get second max value in subquery of from clause because outer invoice is not grouped so it might be max or midlle or least .

4) we can not send values of other table in subquery of join table.

ex

create table inv (id serial ,date timestamp without time zone);

insert into inv (date) values('2017-01-31 00:00:00'),('2017-01-30 00:00:00'),('2017-01-29 00:00:00'),('2017-01-28 00:00:00'),('2017-01-27 00:00:00');

select date as d1 from inv;
 id |        date         
----+---------------------
  1 | 2017-01-31 00:00:00
  2 | 2017-01-30 00:00:00
  3 | 2017-01-29 00:00:00
  4 | 2017-01-28 00:00:00
  5 | 2017-01-27 00:00:00

(5 rows)

I need this

 id |date                 |date                 | id 
  1 | 2017-01-31 00:00:00 | 2017-01-30 00:00:00 |  2
  2 | 2017-01-30 00:00:00 | 2017-01-29 00:00:00 |  3
  3 | 2017-01-29 00:00:00 | 2017-01-28 00:00:00 |  4
  4 | 2017-01-28 00:00:00 | 2017-01-27 00:00:00 |  5
  5 | 2017-01-27 00:00:00 |  

I can't do subquery in select as database is big and need to do this for 4-5 column

UPDATE 1

I need this from same table but using it twice in FROM clause as my requirement is that I need several data joined from invoice table and then there is 4-5 column in which I need things like sum of amount paid between last and this invoice.

So I can take both invoice date in subquery and get the data between them

UPDATE 2

lag will not solve this

select i.id,i.date, lag(date) over (order by date) from inv i order by id ;
id |        date         |         lag        
----+---------------------+---------------------
1 | 2017-01-31 00:00:00 | 2017-01-30 00:00:00
2 | 2017-01-30 00:00:00 | 2017-01-29 00:00:00
3 | 2017-01-29 00:00:00 | 2017-01-28 00:00:00
4 | 2017-01-28 00:00:00 | 2017-01-27 00:00:00
5 | 2017-01-27 00:00:00 | 
(5 rows)
Time: 0.480 ms
test=# select i.id,i.date, lag(date) over (order by date) from inv i where id=2 order by id ;
 id |        date         | lag 
----+---------------------+-----
  2 | 2017-01-30 00:00:00 | 
(1 row)
Time: 0.525 ms
test=# select i.id,i.date, lag(date) over (order by date) from inv i where id in (2,3) order by id ;
 id |        date         |         lag       
----+---------------------+---------------------
2 | 2017-01-30 00:00:00 | 2017-01-29 00:00:00
3 | 2017-01-29 00:00:00 | 

it will calculate on the data it will get from the table in that query it is bounded in that query see here 3 has a lag but could not get it cause query is not allowing it to have it ....something in left join needs to be done so the lag date can be taken from same table but calling it again in from clause Thanks Again buddy

9
  • 3
    select date as d1, lag(date) over () d2 from inv; Commented Jan 30, 2017 at 14:45
  • 1
    @VaoTsun I think you need an over(ORDER BY date) Commented Jan 30, 2017 at 14:49
  • You question's title is not consistent with your expected output. Commented Jan 30, 2017 at 14:49
  • @Gurv Maybe not, but for a first time post, I think he did a very good job explaining the problem. Commented Jan 30, 2017 at 14:50
  • @JuanCarlosOropeza taking in account he has single column, should work without specifying order by? not sure though Commented Jan 30, 2017 at 14:52

1 Answer 1

3

Like here?:

t=# select date as d1,
           lag(date) over (order by date) 
    from inv 
    order by 1 desc;


         d1          |         lag
---------------------+---------------------
 2017-01-31 00:00:00 | 2017-01-30 00:00:00
 2017-01-30 00:00:00 | 2017-01-29 00:00:00
 2017-01-29 00:00:00 | 2017-01-28 00:00:00
 2017-01-28 00:00:00 | 2017-01-27 00:00:00
 2017-01-27 00:00:00 |
(5 rows)

Time: 1.416 ms
Sign up to request clarification or add additional context in comments.

3 Comments

thanks Juan but this will not solve my problem it will surely get data as in my example but will not give me the id which i need .I have updated my question i did miss some point in it.
@VijayVerma: just add lag(id) over (order by date) to get the id as well
lag will not solve this test=# select i.id,i.date, lag(date) over (order by date) from inv i where id in (2,3) order by id ; id | date | lag ----+---------------------+--------------------- 2 | 2017-01-30 00:00:00 | 2017-01-29 00:00:00 3 | 2017-01-29 00:00:00 | it will calculate on the data it will get from the table in that query it is bounded in that query see here 3 has a lag but could not get it cause query is not allowing it to have it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.