Code: Alles auswählen.
REPORT.
TYPES:
BEGIN OF ty_db,
id TYPE i,
model TYPE char20,
farbe TYPE char20,
END OF ty_db,
tty_db TYPE STANDARD TABLE OF ty_db WITH EMPTY KEY.
DATA g_db TYPE tty_db.
g_db = VALUE #(
( id = '1' Model = 'Golf' Farbe = 'Rot' )
( id = '1' Model = 'Gold' Farbe = 'Blau' )
( id = '2' Model = 'Passat' Farbe = 'Rot' )
( id = '2' Model = 'Passat' Farbe = 'Weiss' )
( id = '3' Model = 'Polo' Farbe = 'Schwarz' )
( id = '3' Model = 'Polo' Farbe = 'Blau' )
( id = '4' Model = 'Tiguan' Farbe = 'Lila' )
( id = '5' Model = 'GTI' Farbe = 'Rot' ) ).
"SQL für die Farbe Rot. Aber die anderen Einträge mit gleicher ID sollen ebenfalls
"in der Ergebnismenge stehen
SELECT * FROM @g_db AS itab
WHERE farbe = 'Rot'
INTO TABLE @DATA(g_red).
DATA(l_red_ids) = VALUE rsdsselopt_t( FOR <line> IN g_red
( sign = 'I' option = 'EQ' low = <line>-id ) ).
"daher ein zweiter Select :(
SELECT * FROM @g_db AS itab
WHERE id IN @l_red_ids
INTO TABLE @DATA(g_found_entries).
cl_demo_output=>display( g_found_entries ).
Code: Alles auswählen.
select * from @g_db as db
where id in ( SELECT id from g_Db where farbe = 'Rot' ).
Code: Alles auswählen.
select * from @g_db as db
where id in ( SELECT id from g_Db where farbe = 'Rot' )
AND id in ( SELECT id from g_Db where farbe = 'Blau' ).
Code: Alles auswählen.
SELECT * FROM @db AS db
INTO @DATA(test)
WHERE e~id IN (
SELECT id FROM db AS d1
INNER JOIN db AS d2
ON d1~id = d2~id
WHERE d1~farbe = 'Rot'
AND d2~farbe = 'Blau' ).
Code: Alles auswählen.
SELECT t_id~* INTO test FROM g_db as t_farbe
JOIN g_db as t_id
ON t_id-id = t_farbe_id
WHERE t_farbe-farbe IN ('Rot','blau').
Code: Alles auswählen.
REPORT.
CLASS ltc_unit_test DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT.
PRIVATE SECTION.
TYPES:
tty_jest TYPE SORTED TABLE OF jest WITH UNIQUE KEY objnr stat.
CLASS-METHODS:
class_setup.
METHODS:
simple_select FOR TESTING,
subquery_with_inner_join FOR TESTING,
inner_join FOR TESTING,
subquery FOR TESTING,
two_selects FOR TESTING.
CLASS-DATA:
g_expected TYPE tty_jest.
DATA:
m_jest TYPE tty_jest.
ENDCLASS.
CLASS ltc_unit_test IMPLEMENTATION.
METHOD class_setup.
DATA(l_test_double) = cl_osql_test_environment=>create(
VALUE #( ( 'JEST' ) ) ).
l_test_double->insert_test_data( VALUE jest_tty(
( mandt = '100' objnr = 'OR000001000000' stat = 'I0001' )
( mandt = '100' objnr = 'OR000001000000' stat = 'I0002' )
( mandt = '100' objnr = 'OR000001000000' stat = 'I0003' )
( mandt = '100' objnr = 'OR000001000001' stat = 'I0001' )
( mandt = '100' objnr = 'OR000001000001' stat = 'I0002' )
( mandt = '100' objnr = 'OR000001000001' stat = 'I0004' )
( mandt = '100' objnr = 'OR000001000002' stat = 'I0001' )
( mandt = '100' objnr = 'OR000001000002' stat = 'I0004' )
( mandt = '100' objnr = 'OR000001000002' stat = 'I0005' )
( mandt = '100' objnr = 'OR000001000003' stat = 'I0001' )
( mandt = '100' objnr = 'OR000001000003' stat = 'I0004' )
( mandt = '100' objnr = 'OR000001000003' stat = 'I0006' )
( mandt = '100' objnr = 'OR000001000004' stat = 'I0001' ) ) ).
g_expected = VALUE #(
( mandt = '100' objnr = 'OR000001000000' stat = 'I0001' )
( mandt = '100' objnr = 'OR000001000000' stat = 'I0002' )
( mandt = '100' objnr = 'OR000001000000' stat = 'I0003' )
( mandt = '100' objnr = 'OR000001000001' stat = 'I0001' )
( mandt = '100' objnr = 'OR000001000001' stat = 'I0002' )
( mandt = '100' objnr = 'OR000001000001' stat = 'I0004' ) ).
ENDMETHOD.
METHOD simple_select.
SELECT * FROM jest INTO TABLE @m_jest.
cl_aunit_assert=>assert_equals( exp = 13 act = lines( m_jest ) ).
ENDMETHOD.
METHOD inner_join.
"???
"cl_aunit_assert=>assert_equals( exp = g_expected act = m_jest ).
ENDMETHOD.
METHOD subquery_with_inner_join.
SELECT * FROM jest
WHERE objnr IN (
SELECT j1~objnr FROM jest AS j1
INNER JOIN jest AS j2 ON j1~objnr EQ j2~objnr
WHERE j1~stat EQ 'I0001'
AND j2~stat EQ 'I0002' )
INTO TABLE @m_jest.
cl_aunit_assert=>assert_equals( exp = g_expected act = m_jest ).
ENDMETHOD.
METHOD subquery.
SELECT * FROM jest
WHERE objnr IN ( SELECT objnr FROM jest WHERE stat = 'I0001' )
AND objnr IN ( SELECT objnr FROM jest WHERE stat = 'I0002' )
INTO TABLE @m_jest.
cl_aunit_assert=>assert_equals( exp = g_expected act = m_jest ).
ENDMETHOD.
METHOD two_selects.
DATA l_object_numbers_for_select TYPE STANDARD TABLE OF jest.
DATA(l_rng_stat) = VALUE rsdsselopt_t(
( sign = 'I' option = 'EQ' low = 'I0001' )
( sign = 'I' option = 'EQ' low = 'I0002' ) ).
SELECT * FROM jest
WHERE stat IN @l_rng_stat
ORDER BY PRIMARY KEY
INTO TABLE @DATA(l_tmp_object_numbers).
LOOP AT l_tmp_object_numbers ASSIGNING FIELD-SYMBOL(<l>)
GROUP BY ( objnr = <l>-objnr size = GROUP SIZE )
REFERENCE INTO DATA(l_group).
IF l_group->size EQ 2.
INSERT VALUE #( objnr = l_group->objnr ) INTO TABLE l_object_numbers_for_select.
ENDIF.
ENDLOOP.
SELECT * FROM jest
FOR ALL ENTRIES IN @l_object_numbers_for_select
WHERE objnr EQ @l_object_numbers_for_select-objnr
INTO TABLE @m_jest.
cl_aunit_assert=>assert_equals( exp = g_expected act = m_jest ).
ENDMETHOD.
ENDCLASS.
Ok, die gibt es aber erst ab 7.53, und 7.53 gibt es nicht für SAP HCM, insofern habe ich damit keine Erfahrung.
Wenn ich drüber nachdenke, hast Du recht, und zwar dann, wenn dieselbe ID bei mehreren gewählten Farben (hier also bei rot und bei blau) auftaucht. Nach meinem Dafürhalten dürften das aber nur Duplikate sein, die man mit einem DISTINCT (oder einem FOR ALL ENTRIES IN) wegbekommen können müsste.