Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Date Difference in SQL Where Condition?

Former Member
0 Likes
489

Dear All,

I have a problem while fetching records from two tables LIPS and VAPMA on basis of date difference. My SQL conditions/statements are as follows.

DATA myvar type p.

1. SELECT SUM( lips~lfimg )

FROM lips INNER JOIN vapma ON lipsvgbel = vapmavbeln

AND vapmamatnr = lipsmatnr

INTO myvar

WHERE DATEDIFF(lipserdat , vapmaaudat) = 2.

2. SELECT SUM( lips~lfimg )

FROM lips INNER JOIN vapma ON lipsvgbel = vapmavbeln

AND vapmamatnr = lipsmatnr

INTO mywar

WHERE

lipserdat - vapmaaudat = 2.

Above SQL statements are not working, kindly help me out. Thanking you in advance.

Ahsan Majeed

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
454

Hi

Do like this:

data: begin of itab occurs 0,

vbeln like lips-vbeln,

posnr like lips-posnr,

erdat like sy-datum,

lfimg like lips-lfimg,

audat like sy-datum,

d_diff type i,

end of itab.

data : v_qty like lips-lfimg.

SELECT avbeln aposnr aerdat alfimg b~audat into table itab

FROM lips as a INNER JOIN vapma as b

ON avgbel = bvbeln.

clear v_qty.

sort itab by vbeln posnr.

loop at itab.

itab-d_diff = itab-erdat - itab-audat.

if itab-d_diff = 2.

v_qty = v_qty + itab-lfimg.

endif.

endloop.

Reward if useful

regards

Anji

2 REPLIES 2
Read only

Former Member
0 Likes
455

Hi

Do like this:

data: begin of itab occurs 0,

vbeln like lips-vbeln,

posnr like lips-posnr,

erdat like sy-datum,

lfimg like lips-lfimg,

audat like sy-datum,

d_diff type i,

end of itab.

data : v_qty like lips-lfimg.

SELECT avbeln aposnr aerdat alfimg b~audat into table itab

FROM lips as a INNER JOIN vapma as b

ON avgbel = bvbeln.

clear v_qty.

sort itab by vbeln posnr.

loop at itab.

itab-d_diff = itab-erdat - itab-audat.

if itab-d_diff = 2.

v_qty = v_qty + itab-lfimg.

endif.

endloop.

Reward if useful

regards

Anji

Read only

0 Likes
454

Dear Anji,

Thanks for your reply. Actually i want this logic in SQL statement, later i figure it out that this logic is not possible in sql where statement because this is incorrect statement.

Consider this:

field1 = 4

field2 = 2

WHERE field1 - field2 = 2

compiler interprets it as

WHERE 2 = 2 " this is illogical condition thats why its not possible

However i really appreciate your reply. Once again thank you and points for you.

Regards,

Ahsan Majeed