tags:

views:

133

answers:

1

I want to get one record before start date and end date

DtpFrom means – 'date picker from
DtpTo means – 'date picker to

VB 6 CODE

sdate = DateToString(dtpFrom)
edate = DateToString(dtpTo)

QUERY

Where DATE BETWEEN '" & sdate & "' AND '" & edate & "'"

I want to get one record before sdate and edate

I tried this code

VB 6 CODE

s1sdate = -sdate
e1edate= -edate

QUERY

Where DATE BETWEEN '" & s1date & "' AND '" & e1date & "'"

But it is going one day minus

Example

Selecting 03/05/2009 to 03/06/2009 from date picker, but it showing record from
02/05/2009 to 02/06/2009.

I want to display one record before from the selecting date and ending date, not one day before, because my table is not a continous date.

ADDITIONAL EXAMPLE:

If we have a table and rows [ ID(int) , Value(Money) ] and we have some rows in it

ID --Value
1------70 
2------100 
3------150 
8------200 
20-----250 
45-----280

and we want to make Query that get each row ID, Value and the previous Row Value in which data appear as follow

ID --- Value ---Prev_Value 
1 ----- 70 ---------- 0 
2 ----- 100 -------- 70 
3 ----- 150 -------- 100 
8 ----- 200 -------- 150 
20 ---- 250 -------- 200 
45 ---- 280 -------- 250

i make the following query but i think it's so bad in performance in huge amount of data

select  t1.id, t1.value, t2.value  from  table  t1 inner join table  t2  on t1.id = t2.id where t2.value =  (select max(value)  from  table t where t.value< t1.value and t.id = t1.id ) and  T1.value BETWEEN '" & sdate & "' AND '" & edate & "'

Need VB 6 CODE OR ACCESS QUERY HELP.

A: 
SELECT * FROM whatever
WHERE DATE < sdate
ORDER BY DATE DESC
LIMIT 1

I don't think end date really matters if you want "the one before the records between start and end date"

But if you want the record right before start date AND the record right before end date you could just repeat the above query with edate instead of sdate.

This is what the query actually means: Select every record with a DATE before this one (sdate). For all of those records, order them by DATE in a descending manner (DESC). Only return the first one (LIMIT 1).

Note: Some implementations vary. You may need to use "TOP 1" after the word "SELECT" instead of "LIMIT 1"

colithium
Am Selecting the date between 20090101 to 20090601 and i used your query, but it showing the record before 20090101. Because you used < sdate. I want to select one record before sdate and edate.
Gopal
I'm not sure what that means, can you explain and give examples of data and what you WANT it to return? (Edit your question)
colithium
@Colithium - editied my Question, provided another example
Gopal
Is 'value' a currency value or a datetime value? You use it both ways
colithium