Query

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a table in which I have 2 items.

Eg Table Sales
2/22 100
2/23 90

I need to show the difference between the days 2/22 and
2/23. I am drawing a blank.

Can someone help me out.

Thanks
 
Hi,



Assuming you have a date_time field, dt, then



SELECT a.dt, LAST(b.sales)-LAST(a.sales)

FROM (myTable As a INNER JOIN myTable As b
ON a.dt < b.dt)
INNER JOIN myTable As c
ON a.dt < c.dt

GROUP BY a.dt, b.dt
HAVING MIN(c.dt)= b.dt


Basically, we got three references to the original data. we group on each
date_time of a, each date_time of b that are occurring after a, and let c.dt
floating. When the groups are made, (with all possible combinations of a.dt
and b.dt), we keep only those where b.dt = MIN(c.dt), ie, only those of b.dt
immediately following a.dt.

There is a rule telling that we must either include the field in the group
by, either aggregate it, so here, we use LAST over the sales fields, to get
the single value associated to a given reference a, or b.



Hoping it may help,
Vanderghast, Access MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top