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

SQL Statement

Former Member
0 Likes
758

Hello All,

KeyField1KeyField2KeyField3dateTimeField4
F11

F21

120150101131000F4A
F11F21220150201104000F4B
F11F21320150301112000F4C

F11

F21

4

20150301

172000

F4D

F12F22520150103154000F4L
F12F22620141012045300F4S

F12

F22

7

20150606

224000

F4C

F12F22820150509113700F4d
F13...
F14...
..........
............

I have above fields in my table.

User can give input of Key fields 'Key Field1' and 'KeyField2' .

For example user has passed

Key Field1 in('F11', 'F12')  and

Key Field2 in('F21', 'F22')

I require the latest one in these based on date and time i.e records highlighted in red with bigger font.

I know this can be done by getting the records with input key fields into internal table then sorting them by

KeyField1 Keyfield2 ASCENDING 

   Date Time Descending

and deleting adjacent duplicates comparing KeyField1 and KeyField2.

there are many similar way of dealing with this after bringing after bringing it into internal table.

Is there any way in Open SQL where we can get the exact required data(i.e the two records highlighted)

without getting additional records into application layer. The filtering sould occur at DB layer itself.

With regards,

Sandeep Akella

5 REPLIES 5
Read only

Former Member
0 Likes
718

I am afraid, you will have to go via the internal table processing way only.

Couldn't find a way to select the highlighted records via a SQL statement.

Read only

Ankit_Maskara
Product and Topic Expert
Product and Topic Expert
0 Likes
718

Hi,

You can try the subquery option.

write select as below -

select key_field1 key_field2 key_filed3 max(date) max(time) field4 from <table name>

into corresponding fields of table <tablename>

where key_field1 in ( select key_field1  where key_field1 in s_key_field1)

and     key_field2 in ( select key_field2  where key_field1 in s_key_field2).

Assumptions : Only one unique combination of key_field1 and key_field2 will exist as shown by your data sample.

Also i have denoted the input values via select options s_key_field1 and s_key_field2. The aggregation function works with Date and Time type fields.

BR.

Read only

SwadhinGhatuary
Active Contributor
0 Likes
718

Hi,

A dummy code

TABLES vbap.

TYPES:BEGIN OF ty_vbap,
  vbeln TYPE vbeln,
  posnr TYPE posnr,
  END OF ty_vbap.

DATA  lt_vbap TYPE TABLE OF ty_vbap.

SELECT-OPTIONS s_vbeln FOR vbap-vbeln.

SELECT vbeln MAX( posnr ) FROM vbap
  INTO TABLE lt_vbap
  WHERE vbeln IN s_vbeln
  GROUP BY vbeln.

BREAK-POINT.

But I suggest go for Internal table to get Highlighted row instead  of aggregation function,.

Read only

deepan_v_s
Active Participant
0 Likes
718

Hello Sandeep,

You can use a subquery to solve this.

Select * from table where

     key1 = 'value' AND

     key2 = 'value' AND

     date IN (Select MAX (date) from table)

     into INTERNAL_TABLE.

And sort the resulting IT based on TIME and KEY fields and pick up the latest record based on Date and Time.

This approach will reduce the number of records and load on the DB. As far i researched this is a good approach.

If you have Timestamp field in the table then it can be done efficiently based on MAX (Timestamp).

Hope this helps !!!

Regards,

Deepan Swaminathan.

Read only

0 Likes
718

It worked well for me.

Thank you.