Skip to content

Commit b7ce7d3

Browse files
committed
Sub query with additional table
1 parent d2b40ed commit b7ce7d3

12 files changed

+481
-91
lines changed

src/z_lc_subquery.prog.abap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ REPORT z_lc_subquery.
4040
*REPORT demo_select_subquery_1.
4141

4242
DATA: name_tab TYPE TABLE OF scarr-carrname,
43-
name LIKE LINE OF name_tab.
43+
name LIKE LINE OF name_tab.
4444

4545
SELECT carrname
4646
INTO TABLE name_tab
4747
FROM scarr
48-
WHERE EXISTS ( select *
48+
WHERE EXISTS ( SELECT *
4949
FROM spfli
5050
WHERE carrid = scarr~carrid AND
5151
cityfrom = 'NEW YORK' ).
@@ -72,7 +72,7 @@ DATA: city TYPE sgeocity-city,
7272
SELECT SINGLE city latitude longitude
7373
INTO (city, lati, longi)
7474
FROM sgeocity
75-
WHERE city IN ( select cityfrom
75+
WHERE city IN ( SELECT cityfrom
7676
FROM spfli
7777
WHERE carrid = carr_id AND
7878
connid = conn_id ).
@@ -87,7 +87,7 @@ WRITE: city, lati, longi.
8787

8888
*REPORT demo_select_subquery_3.
8989

90-
DATA: wa TYPE sflight,
90+
DATA: wa TYPE sflight,
9191
plane LIKE wa-planetype,
9292
seats LIKE wa-seatsmax.
9393

@@ -121,7 +121,7 @@ AT LINE-SELECTION.
121121
INTO (plane, seats)
122122
FROM saplane AS plane
123123
WHERE seatsmax < wa-seatsmax AND
124-
seatsmax >= ALL ( select seatsocc
124+
seatsmax >= ALL ( SELECT seatsocc
125125
FROM sflight
126126
WHERE carrid = wa-carrid AND
127127
connid = wa-connid )

src/z_lc_subquery_case.prog.abap

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
*&---------------------------------------------------------------------*
2+
*& Report Z_LC_SUBQUERY
3+
*&---------------------------------------------------------------------*
4+
*&
5+
*&---------------------------------------------------------------------*
6+
REPORT z_lc_subquery_case.
7+
8+
9+
SELECT empid FROM zemp_master
10+
INTO TABLE @DATA(lt_empid)
11+
WHERE empid NOT IN ( SELECT empid FROM zemp_type WHERE emptype NE 'DEV' ).
12+
13+
14+
BREAK-POINT.
15+
*Given 2 tables, a manager table and an employee table, query to get all Managers who have at least one male & one female employee reporting to him.
16+
*This is a very simple question to begin with. I will be using aggregation functions for calculating employees under a manager.
17+
18+
*SELECT a~id,
19+
* a~name,
20+
* SUM( CASE WHEN b~gender = 'F' THEN 1 ELSE 0 END ) AS female,
21+
* SUM( CASE WHEN b~gender = 'M' THEN 1 ELSE 0 END ) AS male
22+
* FROM zmanager AS a
23+
* INNER JOIN zemployee AS b
24+
* ON a~id = b~manager
25+
* INTO TABLE @DATA(lt_)
26+
* GROUP BY a~id, a~name.
27+
*
28+
*DELETE lt_ WHERE ( female < 1 OR male < 1 ).
29+
30+
31+
*Given a table which includes field firstname and lastname,
32+
*query that returns the first and last name of each person in the table whose last name appears at least twice in the column “lastname”.
33+
*I have tried to do this using subquery approach.
34+
35+
*SELECT name, lastname FROM zemployee
36+
*INTO TABLE @DATA(lt1_)
37+
*WHERE lastname IN
38+
* ( SELECT lastname FROM zemployee
39+
* GROUP BY lastname HAVING COUNT(*) > 1 ).
40+
41+
*--------------------------------------------------------------------*
42+
*--------------------------------------------------------------------*
43+
44+
* Correlated, non-scalar subquery:
45+
46+
*REPORT demo_select_subquery_1.
47+
48+
*DATA: name_tab TYPE TABLE OF scarr-carrname,
49+
* name LIKE LINE OF name_tab.
50+
*
51+
*SELECT carrname
52+
* INTO TABLE name_tab
53+
* FROM scarr
54+
* WHERE EXISTS ( select *
55+
* FROM spfli
56+
* WHERE carrid = scarr~carrid AND
57+
* cityfrom = 'NEW YORK' ).
58+
*
59+
*LOOP AT name_tab INTO name.
60+
* WRITE: / name.
61+
*ENDLOOP.
62+
*
63+
**This example selects all lines from database table SCARR for airlines that fly from New York.
64+
*
65+
**Example
66+
*
67+
**Scalar subquery:
68+
*
69+
**REPORT demo_select_subquery_2.
70+
*
71+
*DATA: carr_id TYPE spfli-carrid VALUE 'LH',
72+
* conn_id TYPE spfli-connid VALUE '0400'.
73+
*
74+
*DATA: city TYPE sgeocity-city,
75+
* lati TYPE p DECIMALS 2,
76+
* longi TYPE p DECIMALS 2.
77+
*
78+
*SELECT SINGLE city latitude longitude
79+
* INTO (city, lati, longi)
80+
* FROM sgeocity
81+
* WHERE city IN ( select cityfrom
82+
* FROM spfli
83+
* WHERE carrid = carr_id AND
84+
* connid = conn_id ).
85+
*
86+
*WRITE: city, lati, longi.
87+
*
88+
**This example reads the latitude and longitude of the departure city of flight LH 402 from database table SGEOCITY.
89+
*
90+
**Example
91+
*
92+
**Scalar subquery:
93+
*
94+
**REPORT demo_select_subquery_3.
95+
*
96+
*DATA: wa TYPE sflight,
97+
* plane LIKE wa-planetype,
98+
* seats LIKE wa-seatsmax.
99+
*
100+
*SELECT carrid connid planetype seatsmax MAX( seatsocc )
101+
* INTO (wa-carrid, wa-connid, wa-planetype,
102+
* wa-seatsmax, wa-seatsocc)
103+
* FROM sflight
104+
* GROUP BY carrid connid planetype seatsmax
105+
* ORDER BY carrid connid.
106+
*
107+
* WRITE: / wa-carrid,
108+
* wa-connid,
109+
* wa-planetype,
110+
* wa-seatsmax,
111+
* wa-seatsocc.
112+
*
113+
* HIDE: wa-carrid, wa-connid, wa-seatsmax.
114+
*
115+
*ENDSELECT.
116+
*
117+
*AT LINE-SELECTION.
118+
*
119+
* WINDOW STARTING AT 45 3 ENDING AT 85 13.
120+
*
121+
* WRITE: 'Alternative Plane Types',
122+
* 'for', wa-carrid, wa-connid.
123+
*
124+
* ULINE.
125+
*
126+
* SELECT planetype seatsmax
127+
* INTO (plane, seats)
128+
* FROM saplane AS plane
129+
* WHERE seatsmax < wa-seatsmax AND
130+
* seatsmax >= ALL ( select seatsocc
131+
* FROM sflight
132+
* WHERE carrid = wa-carrid AND
133+
* connid = wa-connid )
134+
* ORDER BY seatsmax.
135+
*
136+
* WRITE: / plane, seats.
137+
*
138+
* ENDSELECT.
139+
*
140+
** The detail list displays all aircraft types that have fewer seats than the currently-allocated aircraft type,
141+
** but enough to carry all of the passengers currently booked on the flight.

src/z_lc_two_sum_new.prog.xml renamed to src/z_lc_subquery_case.prog.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
44
<asx:values>
55
<PROGDIR>
6-
<NAME>Z_LC_TWO_SUM_NEW</NAME>
6+
<NAME>Z_LC_SUBQUERY_CASE</NAME>
77
<SUBC>1</SUBC>
88
<RLOAD>E</RLOAD>
99
<FIXPT>X</FIXPT>
@@ -12,8 +12,8 @@
1212
<TPOOL>
1313
<item>
1414
<ID>R</ID>
15-
<ENTRY>Report Z_LC_TWO_SUM_NEW - Bruteforce</ENTRY>
16-
<LENGTH>36</LENGTH>
15+
<ENTRY>Program Z_LC_SUBQUERY</ENTRY>
16+
<LENGTH>21</LENGTH>
1717
</item>
1818
</TPOOL>
1919
</asx:values>

src/z_lc_two_sum.prog.abap

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
*&
55
*&---------------------------------------------------------------------*
66
REPORT z_lc_two_sum.
7-
7+
*Two sum works for Unsorted and Sorted arrays without any explicit sort
88

99
TYPES: BEGIN OF ty_hash,
1010
key TYPE i,
11-
* val TYPE i,
1211
END OF ty_hash.
1312

1413

@@ -20,7 +19,8 @@ DATA: gt_values TYPE STANDARD TABLE OF i,
2019

2120
DATA(target) = 9.
2221

23-
gt_values = VALUE #( ( 1 ) ( 2 ) ( 3 ) ( 4 ) ( 5 ) ).
22+
*gt_values = VALUE #( ( 1 ) ( 2 ) ( 3 ) ( 4 ) ( 5 ) ( 6 ) ).
23+
gt_values = VALUE #( ( 3 ) ( 6 ) ( 1 ) ( 4 ) ( 5 ) ( 2 ) ).
2424

2525
LOOP AT gt_values INTO gs_values.
2626

@@ -31,13 +31,11 @@ LOOP AT gt_values INTO gs_values.
3131
IF sy-subrc NE 0.
3232

3333
gs_hash-key = gs_values.
34-
* gs_hash-val = sy-tabix.
3534

3635
INSERT gs_hash INTO TABLE gt_hash.
3736
ELSE.
3837

3938
WRITE:/ gs_values , gs_hash-key.
40-
EXIT.
4139

4240
ENDIF.
4341

src/z_lc_two_sum_new.prog.abap

Lines changed: 0 additions & 77 deletions
This file was deleted.

src/z_lc_two_sum_new_working.prog.abap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ ENDCLASS.
1717

1818
START-OF-SELECTION.
1919
DATA(lv_ref) = NEW lcl_two_sum( ).
20-
DATA(a) = VALUE ty_array( ( 2 ) ( 7 ) ( 11 ) ( 15 ) ).
20+
* DATA(a) = VALUE ty_array( ( 2 ) ( 7 ) ( 11 ) ( 15 ) ).
21+
DATA(a) = VALUE ty_array( ( 15 ) ( 7 ) ( 11 ) ( 2 ) ).
2122
IF lv_ref IS NOT INITIAL.
2223
* DATA(indexs) = lv_ref->m_brute_force( EXPORTING array = a target = 9 ).
2324
DATA(indexs) = lv_ref->m_two_pointer(

src/zemp_master.tabl.xml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<abapGit version="v1.0.0" serializer="LCL_OBJECT_TABL" serializer_version="v1.0.0">
3+
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
4+
<asx:values>
5+
<DD02V>
6+
<TABNAME>ZEMP_MASTER</TABNAME>
7+
<DDLANGUAGE>E</DDLANGUAGE>
8+
<TABCLASS>TRANSP</TABCLASS>
9+
<CLIDEP>X</CLIDEP>
10+
<DDTEXT>EMployee Master</DDTEXT>
11+
<MAINFLAG>X</MAINFLAG>
12+
<CONTFLAG>A</CONTFLAG>
13+
<EXCLASS>1</EXCLASS>
14+
</DD02V>
15+
<DD09L>
16+
<TABNAME>ZEMP_MASTER</TABNAME>
17+
<AS4LOCAL>A</AS4LOCAL>
18+
<TABKAT>1</TABKAT>
19+
<TABART>APPL1</TABART>
20+
<BUFALLOW>N</BUFALLOW>
21+
</DD09L>
22+
<DD03P_TABLE>
23+
<DD03P>
24+
<FIELDNAME>MANDT</FIELDNAME>
25+
<KEYFLAG>X</KEYFLAG>
26+
<ROLLNAME>MANDT</ROLLNAME>
27+
<ADMINFIELD>0</ADMINFIELD>
28+
<NOTNULL>X</NOTNULL>
29+
<COMPTYPE>E</COMPTYPE>
30+
</DD03P>
31+
<DD03P>
32+
<FIELDNAME>EMPID</FIELDNAME>
33+
<KEYFLAG>X</KEYFLAG>
34+
<ROLLNAME>NUMC10</ROLLNAME>
35+
<ADMINFIELD>0</ADMINFIELD>
36+
<NOTNULL>X</NOTNULL>
37+
<COMPTYPE>E</COMPTYPE>
38+
</DD03P>
39+
<DD03P>
40+
<FIELDNAME>EMP_NAME</FIELDNAME>
41+
<ROLLNAME>CHAR80</ROLLNAME>
42+
<ADMINFIELD>0</ADMINFIELD>
43+
<COMPTYPE>E</COMPTYPE>
44+
</DD03P>
45+
<DD03P>
46+
<FIELDNAME>EMP_AGE</FIELDNAME>
47+
<ROLLNAME>INT2</ROLLNAME>
48+
<ADMINFIELD>0</ADMINFIELD>
49+
<COMPTYPE>E</COMPTYPE>
50+
</DD03P>
51+
</DD03P_TABLE>
52+
</asx:values>
53+
</asx:abap>
54+
</abapGit>

0 commit comments

Comments
 (0)