Inner join query
Hi all,
I want to query a inner join via some tools in sap GUI. till now, i could not query a inner join between two tables via SE11. can someone tell are there some other ways to achieve this?
any response will be awarded!
thanks and regards,
samson
hi samson,
Proper use of Inner Join
When multiple SAP tables are logically joined, it is always advisable to use inner join to read the data from them. This certainly reduces the load on the network.
Let us take an example of 2 tables, zairln and zflight. The table zairln has the field airln, which is the airline code and the field lnnam, which is the name of the airline. The table zflight has the field airln, the airline code and other fields which hold the details of the flights that an airline operates.
Since these 2 tables a re logically joined by the airln field, it is advisable to use the inner join.
Select aairln alnnam bfligh bcntry into table int_airdet
example 2:--
Inner join: If you have common fields between 2 or more tables, its betterto use Inner join.
Check the below example:
VBAK & VBAP table has common fields, hence we can use inner join.
SELECT akunnr avbeln anetwr abnddt a~knumv
avkbur aerdat avdatu aaugru
aktext bmatnr barktx bkwmeng b~kzwi6
bvolum bposnr b~kdmat
INTO CORRESPONDING FIELDS OF TABLE g_t_quot
FROM vbak AS a INNER JOIN vbap AS b
ON avbeln = bvbeln
WHERE a~vbeln IN so_vbeln
AND a~trvog ='2'
AND a~vkorg IN so_vkorg
AND a~vtweg IN so_vtweg
AND a~vkbur IN so_vkbur
AND a~audat IN so_audat
AND a~kunnr IN so_kunag.
FOR ALL ENTRIES:
If you get some data into one internal table and if you want to fetch data from other table based on it, use FOR ALL ENTRIES.
g_t_quot is an internal table.
SELECT spras augru bezei FROM tvaut INTO TABLE g_t_tvaut
FOR ALL ENTRIES IN g_t_quot
WHERE augru = g_t_quot-augru AND spras = sy-langu.
From zairln as a inner join zflight as b on aairln = bairln.
In order to restrict the data as per the selection criteria, a where clause can be added to the above inner join.
Similar Messages
-
Help with Inner Join query in SQL
Hope someone can help with this, as this is quite an involved project for me, and I'm just about there with it.
My table structure is :
table_packages
packageID
package
packageDetails
etc
table_destinations
destinationID
destination
destinationDetails
etc
table_packagedestinations
packageID
destinationID
..so nothing that complicated.
So the idea is that I can have a package details page which shows the details of the package, along any destinations linked to that package via the packagedestinations table.
So this is the last part really - to get the page to display the destinations, but I'm missing something along the way....
This is the PHP from the header, including my INNER JOIN query....
PHP Code:
<?php
$ParampackageID_WADApackages = "-1";
if (isset($_GET['packageID'])) {
$ParampackageID_WADApackages = (get_magic_quotes_gpc()) ? $_GET['packageID'] : addslashes($_GET['packageID']);
$ParamSessionpackageID_WADApackages = "-1";
if (isset($_SESSION['WADA_Insert_packages'])) {
$ParamSessionpackageID_WADApackages = (get_magic_quotes_gpc()) ? $_SESSION['WADA_Insert_packages'] : addslashes($_SESSION['WADA_Insert_packages']);
$ParampackageID2_WADApackages = "-1";
if (isset($_GET['packageID'])) {
$ParampackageID2_WADApackages = (get_magic_quotes_gpc()) ? $_GET['packageID'] : addslashes($_GET['packageID']);
mysql_select_db($database_connPackages, $connPackages);
$query_WADApackages = sprintf("SELECT packageID, package, costperpax, duration, baselocation, category, dateadded, agerange, hotel, educational_tours, field_trips, corporate_outings, plant_visits, budget_package, rollingtours, teambuilding, description, offer FROM packages WHERE packageID = %s OR ( -1= %s AND packageID= %s)", GetSQLValueString($ParampackageID_WADApackages, "int"),GetSQLValueString($ParampackageID2_WADApackages, "int"),GetSQLValueString($ParamSessionpackageID_WADApackages, "int"));
$WADApackages = mysql_query($query_WADApackages, $connPackages) or die(mysql_error());
$row_WADApackages = mysql_fetch_assoc($WADApackages);
$totalRows_WADApackages = mysql_num_rows($WADApackages);
$colname_educationalDestinations = "1";
if (isset($_GET['PackageID'])) {
$colname_educationalDestinations = (get_magic_quotes_gpc()) ? packageID : addslashes(packageID);
mysql_select_db($database_connPackages, $connPackages);
$query_educationalDestinations = sprintf("SELECT * FROM destinations INNER JOIN (packages INNER JOIN packagedestinations ON packages.packageID = packagedestinations.packageID) ON destinations.destinationID = packagedestinations.destinationID WHERE packages.packageID = %s ORDER BY destination ASC", GetSQLValueString($colname_educationalDestinations, "int"));
$educationalDestinations = mysql_query($query_educationalDestinations, $connPackages) or die(mysql_error());
$row_educationalDestinations = mysql_fetch_assoc($educationalDestinations);
$totalRows_educationalDestinations = mysql_num_rows($educationalDestinations);
?>
And where I'm trying to display the destinations themselves, I have :
<table>
<tr>
<td>Destinations :</td>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_educationalDestinations['destination']; ?></td>
</tr>
<?php } while ($row_educationalDestinations = mysql_fetch_assoc($educationalDestinations)); ?>
</table>
If anyone could have a quick look and help me out that would be much appreciated - not sure if its my SQL at the top, or the PHP in the page, but either way it would be good to get it working.
Thanks.First off, you need to get the database tables so that there is a relationship between them.
In fact, if there is a one to one relationship, then it may be better to store all of your information in one table such as
table_packages
packageID
package
packageDetails
destination
destinationDetails
etc
If there is a one to many relationship, then the following would be true
packages
packageID
package
packageDetails
etc
destinations
destinationID
packageID
destination
destinationDetails
etc
The above assumes that there are many destinations to one package with the relationship coloured orange.
Once you have the above correct you can apply your query as follows
SELECT *
FROM packages
INNER JOIN destinations
ON packages.packageID = destinations.packageID
WHERE packages.packageID = %s
ORDER BY destination ASC
The above query will show all packages with relevant destinations -
Inner Join. How to improve the performance of inner join query
Inner Join. How to improve the performance of inner join query.
Query is :
select f1~ablbelnr
f1~gernr
f1~equnr
f1~zwnummer
f1~adat
f1~atim
f1~v_zwstand
f1~n_zwstand
f1~aktiv
f1~adatsoll
f1~pruefzahl
f1~ablstat
f1~pruefpkt
f1~popcode
f1~erdat
f1~istablart
f2~anlage
f2~ablesgr
f2~abrdats
f2~ableinh
from eabl as f1
inner join eablg as f2
on f1ablbelnr = f2ablbelnr
into corresponding fields of table it_list
where f1~ablstat in s_mrstat
%_HINTS ORACLE 'USE_NL (T_00 T_01) index(T_01 "EABLG~0")'.
I wanted to modify the query, since its taking lot of time to load the data.
Please suggest : -
Treat this is very urgent.Hi Shyamal,
In your program , you are using "into corresponding fields of ".
Try not to use this addition in your select query.
Instead, just use "into table it_list".
As an example,
Just give a normal query using "into corresponding fields of" in a program. Now go to se30 ( Runtime analysis), and give the program name and execute it .
Now if you click on Analyze button , you can see, the analysis given for the query.The one given in "Red" line informs you that you need to find for alternate methods.
On the other hand, if you are using "into table itab", it will give you an entirely different analysis.
So try not to give "into corresponding fields" in your query.
Regards,
SP. -
Inner join query used with 7 Database tables
HI All,
In a report they used the Inner join Query with 6 Data base table..now there is a performance issue with at query.
its taking so much of time to trigger that query. Please help how to avoid that performance issue for that.
In that 2 database tables containing lakhs of records..
According to my knowledge it can be avoided by using secondary indexs for those 2 database tables..
and by replacing the Inner join Query with FOR ALL ENTRIES statement.
i want how to use the logic by using FORALL ENTRIES statement for this..
So, please give you proper suggestion to avoid this issue..
Thanking you.
Moderator message: Please Read before Posting in the Performance and Tuning Forum
Edited by: Thomas Zloch on Oct 16, 2011 10:27 PMHi,
And what do you mean with "they used"? If "SAP used" then yo will need to ask a SAP for note
FOR ALL ENTRIES is quite good described in help. Please search forum also.
Without query it won't be possible to tell how it can be optimized, however you can try to use SE30/SAT and ST05. Maybe it will help you.
BR
Marcin Cholewczuk -
Hi Guriji,s
I wrote one inner join query. it is working fine. but when i exceute this query it is fetching the data only 261 movement type but but i also fetch the data movement type 201. for this plz tell me the correction in this query.
select amatnr awerks abwart bbudat into table t_itab from mseg as a inner join mkpf as b
on amblnr = bmblnr
where awerks in plant and abwart = '261' or a~bwart = '201'
and btcode2 = 'mb1a' or btcode2 = 'mfbf' .
thanks.Hi Sachin
try to use like below
select amatnr awerks abwart bbudat into table t_itab from mseg as a inner join mkpf as b
on amblnr = bmblnr
where awerks in plant and ( abwart = '261' or a~bwart = '201' )
and ( btcode2 = 'mb1a' or btcode2 = 'mfbf' ).
or try to fill ranges like select-options or use like
select amatnr awerks abwart bbudat into table t_itab from mseg as a inner join mkpf as b
on amblnr = bmblnr
where awerks in plant and abwart in ('261', '201')
and btcode2 = 'mb1a' or btcode2 = 'mfbf' .
Regards
Praveen -
Hi ABAPers,
I was trying to develop a report for which query is something like this.
*& Report ZT_SERIAL
REPORT ZT_SERIAL.
TABLES : SER01,SER03,OBJK,LIKP,LIPS. " LIKP - SD invoice header table, LIPS - SD invoice details table
TYPE-POOLS: SLIS.
DATA: AFIELD TYPE SLIS_FIELDCAT_ALV.
DATA: FIELDCAT TYPE SLIS_T_FIELDCAT_ALV.
MANDATORY WHILE DECLARING ALV ***
DATA: L_FIELDCAT TYPE SLIS_FIELDCAT_ALV..
DATA: V_EVENTS TYPE SLIS_T_EVENT.
DATA: LAYOUT TYPE SLIS_LAYOUT_ALV.
DATA: P_FIELDTAB TYPE SLIS_T_FIELDCAT_ALV.
DATA: I_TITLE_SL_NO_TRACK TYPE LVC_TITLE VALUE 'SERIAL NO TRACKER'.
DATA : BEGIN OF ITAB OCCURS 0,
OBKNR LIKE OBJK-OBKNR, "OBJECT LIST
OBZAE LIKE OBJK-OBZAE, "OBJECT COUNTER
KUNDE LIKE SER01-KUNDE, "CUSTOMER NO
MATNR LIKE OBJK-MATNR, "PART NO
SERNR LIKE OBJK-SERNR, "SERIAL NO
MBLNR LIKE SER03-MBLNR, "GRN/MATERIAL DOC NO
LIEF_NR LIKE SER01-LIEF_NR,"OUTBOUND DELV NO
DATUM LIKE SER01-DATUM, "OUTBOUND DELV DATE
TASER LIKE OBJK-TASER, "HEADER TABLE
ANZSN LIKE SER01-ANZSN, "NO OF SERIAL NOS
VGBEL LIKE LIPS-VGBEL, "SALES ORDER NO
END OF ITAB.
SELECTION-SCREEN : BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.
SELECT-OPTIONS : S_KUNDE FOR SER01-KUNDE,
S_VGBEL FOR LIPS-VGBEL,
S_MATNR FOR OBJK-MATNR,
S_SERNR FOR OBJK-SERNR,
S_MBLNR FOR SER03-MBLNR,
S_LIFNR FOR SER01-LIEF_NR,
S_DATUM FOR SER01-DATUM.
SELECTION-SCREEN : END OF BLOCK B1.
PERFORM TRACKING.
PERFORM FLDCAT.
PERFORM BUILD_LAYOUT.
***CALL FUNCTION TO DISPLAY IN ALV FORMAT*******
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = SY-REPID
I_GRID_TITLE = I_TITLE_SL_NO_TRACK
IS_LAYOUT = LAYOUT
IT_FIELDCAT = P_FIELDTAB[]
I_SAVE = 'A'
IT_EVENTS = V_EVENTS
TABLES
T_OUTTAB = ITAB.
***END OF CALL FUNCTION
FORM TRACKING.
SELECT OBJKOBKNR OBJKOBZAE OBJKMATNR OBJKSERNR OBJK~TASER
SER01KUNDE SER01LIEF_NR SER01DATUM SER01ANZSN
SER03~MBLNR
LIPS~VGBEL
INTO TABLE ITAB FROM OBJK
INNER JOIN SER01 ON OBJKOBKNR = SER01OBKNR
INNER JOIN SER03 ON OBJKOBKNR = SER03OBKNR
INNER JOIN LIPS ON SER01LIEF_NR = LIPSVBELN
WHERE
OBJKTASER = 'SER01' OR OBJKTASER = 'SER03'.
OBJK~MATNR IN S_MATNR AND
OBJK~SERNR IN S_SERNR AND
SER01~KUNDE IN S_KUNDE AND
SER01~LIEF_NR IN S_LIFNR AND
SER01~DATUM IN S_DATUM AND
SER03~MBLNR IN S_MBLNR AND
LIPS~VGBEL IN S_VGBEL.
ENDFORM.
*& Form fldcat
text
FORM FLDCAT.
PERFORM FLDCAT1 USING 'KUNDE' 'Customer No'.
PERFORM FLDCAT1 USING 'VGBEL' 'Sales Order No'.
PERFORM FLDCAT1 USING 'MATNR' 'Tejas Part No'.
PERFORM FLDCAT1 USING 'SERNR' 'Serial No'.
PERFORM FLDCAT1 USING 'MBLNR' 'Material Doc No'.
PERFORM FLDCAT1 USING 'LIEF_NR' 'Outbound Delv No'.
PERFORM FLDCAT1 USING 'DATUM' 'Outbound Delv Date'.
ENDFORM. "fldcat
*& Form fldcat1
text
-->FNAM text
-->FTEXT text
FORM FLDCAT1 USING FNAM FTEXT.
DATA: L_FIELDCAT TYPE SLIS_FIELDCAT_ALV.
L_FIELDCAT-FIELDNAME = FNAM.
L_FIELDCAT-SELTEXT_M = FTEXT.
IF FNAM EQ 'MATNR'."OR fnam EQ 'PKUNAG'.
L_FIELDCAT-KEY = 'X'.
ENDIF.
LAYOUT-COLWIDTH_OPTIMIZE = 'X'.
APPEND L_FIELDCAT TO P_FIELDTAB.
CLEAR fldcat1.
ENDFORM. "fldcat1
*& Form build_layout
text
FORM BUILD_LAYOUT .
LAYOUT-NO_INPUT = 'X'.
LAYOUT-COLWIDTH_OPTIMIZE = 'X'.
LAYOUT-ZEBRA = 'X'.
LAYOUT-TOTALS_TEXT = 'Totals'(256).
layout-coltab_fieldname = 'CELL_COLOUR'.
*clear layout.
ENDFORM.
But when I run this query,the program gets terminated.Can you please help me in rectifying the query and generate the output?
Thanks in advance
BhavinHi
I have corrected the code please copy and past it and then try.
Report ZT_SERIAL.
TABLES : SER01,SER03,OBJK,LIKP,LIPS.
" LIKP - SD invoice header table, LIPS - SD invoice details table
TYPE-POOLS: SLIS.
DATA: AFIELD TYPE SLIS_FIELDCAT_ALV.
DATA: FIELDCAT TYPE SLIS_T_FIELDCAT_ALV.
+ MANDATORY WHILE DECLARING ALV ***
DATA: L_FIELDCAT TYPE SLIS_FIELDCAT_ALV..
DATA: V_EVENTS TYPE SLIS_T_EVENT.
DATA: LAYOUT TYPE SLIS_LAYOUT_ALV.
DATA: P_FIELDTAB TYPE SLIS_T_FIELDCAT_ALV.
DATA: I_TITLE_SL_NO_TRACK TYPE LVC_TITLE VALUE 'SERIAL NO TRACKER'.
DATA : BEGIN OF ITAB OCCURS 0,
OBKNR LIKE OBJK-OBKNR, "OBJECT LIST
OBZAE LIKE OBJK-OBZAE, "OBJECT COUNTER
KUNDE LIKE SER01-KUNDE, "CUSTOMER NO
MATNR LIKE OBJK-MATNR, "PART NO
SERNR LIKE OBJK-SERNR, "SERIAL NO
MBLNR LIKE SER03-MBLNR, "GRN/MATERIAL DOC NO
LIEF_NR LIKE SER01-LIEF_NR,"OUTBOUND DELV NO
DATUM LIKE SER01-DATUM, "OUTBOUND DELV DATE
TASER LIKE OBJK-TASER, "HEADER TABLE
ANZSN LIKE SER01-ANZSN, "NO OF SERIAL NOS
VGBEL LIKE LIPS-VGBEL, "SALES ORDER NO
END OF ITAB.
SELECTION-SCREEN : BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.
SELECT-OPTIONS : S_KUNDE FOR SER01-KUNDE,
S_VGBEL FOR LIPS-VGBEL,
S_MATNR FOR OBJK-MATNR,
S_SERNR FOR OBJK-SERNR,
S_MBLNR FOR SER03-MBLNR,
S_LIFNR FOR SER01-LIEF_NR,
S_DATUM FOR SER01-DATUM.
SELECTION-SCREEN : END OF BLOCK B1.
PERFORM TRACKING.
PERFORM FLDCAT.
PERFORM BUILD_LAYOUT.
***CALL FUNCTION TO DISPLAY IN ALV FORMAT*******
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = SY-REPID
I_GRID_TITLE = I_TITLE_SL_NO_TRACK
IS_LAYOUT = LAYOUT
IT_FIELDCAT = P_FIELDTAB[]
I_SAVE = 'A'
IT_EVENTS = V_EVENTS
TABLES
T_OUTTAB = ITAB.
***END OF CALL FUNCTION
FORM TRACKING.
*SELECT OBJK~OBKNR
OBJK~OBZAE
OBJK~MATNR
OBJK~SERNR
OBJK~TASER
SER01~KUNDE
SER01~LIEF_NR
SER01~DATUM
SER01~ANZSN
SER03~MBLNR
LIPS~VGBEL
INTO TABLE ITAB FROM OBJK
INNER JOIN SER01 ON OBJKOBKNR = SER01OBKNR
INNER JOIN SER03 ON OBJKOBKNR = SER03OBKNR
INNER JOIN LIPS ON SER01LIEF_NR = LIPSVBELN
WHERE
*( OBJKTASER = 'SER01' OR OBJKTASER = 'SER03' ) and
*OBJK-MATNR IN S_MATNR AND
*OBJK~SERNR IN S_SERNR AND
*SER01~KUNDE IN S_KUNDE AND
*SER01~LIEF_NR IN S_LIFNR AND
*SER01~DATUM IN S_DATUM AND
*SER03~MBLNR IN S_MBLNR AND
*LIPS~VGBEL IN S_VGBEL.
SELECT a~OBKNR "OBJK
a~OBZAE
a~MATNR
a~SERNR
a~TASER
b~KUNDE "SER01
b~LIEF_NR
b~DATUM
b~ANZSN
c~MBLNR "SER03
D~VGBEL "LIPS
INTO TABLE ITAB FROM ( OBJK as a
INNER JOIN SER01 as b ON aOBKNR = bOBKNR
INNER JOIN SER03 as c ON aOBKNR = cOBKNR
INNER JOIN LIPS as d ON bLIEF_NR = dVBELN )
WHERE
( aTASER = 'SER01' OR aTASER = 'SER03' ) and
a~MATNR IN S_MATNR AND
a~SERNR IN S_SERNR AND
b~KUNDE IN S_KUNDE AND
b~LIEF_NR IN S_LIFNR AND
b~DATUM IN S_DATUM AND
c~MBLNR IN S_MBLNR AND
d~VGBEL IN S_VGBEL.
ENDFORM.
*& Form fldcat
* text
FORM FLDCAT.
PERFORM FLDCAT1 USING 'KUNDE' 'Customer No'.
PERFORM FLDCAT1 USING 'VGBEL' 'Sales Order No'.
PERFORM FLDCAT1 USING 'MATNR' 'Tejas Part No'.
PERFORM FLDCAT1 USING 'SERNR' 'Serial No'.
PERFORM FLDCAT1 USING 'MBLNR' 'Material Doc No'.
PERFORM FLDCAT1 USING 'LIEF_NR' 'Outbound Delv No'.
PERFORM FLDCAT1 USING 'DATUM' 'Outbound Delv Date'.
ENDFORM. "fldcat
*& Form fldcat1
text
-->FNAM text
-->FTEXT text
FORM FLDCAT1 USING FNAM FTEXT.
DATA: L_FIELDCAT TYPE SLIS_FIELDCAT_ALV.
L_FIELDCAT-FIELDNAME = FNAM.
L_FIELDCAT-SELTEXT_M = FTEXT.
IF FNAM EQ 'MATNR'."OR fnam EQ 'PKUNAG'.
L_FIELDCAT-KEY = 'X'.
ENDIF.
LAYOUT-COLWIDTH_OPTIMIZE = 'X'.
APPEND L_FIELDCAT TO P_FIELDTAB.
* CLEAR fldcat1.
ENDFORM. "fldcat1
*& Form build_layout
* text
FORM BUILD_LAYOUT .
LAYOUT-NO_INPUT = 'X'.
LAYOUT-COLWIDTH_OPTIMIZE = 'X'.
LAYOUT-ZEBRA = 'X'.
LAYOUT-TOTALS_TEXT = 'Totals'(256).
* layout-coltab_fieldname = 'CELL_COLOUR'.
*clear layout.
ENDFORM.
Regards,
Venkat -
I have this query, however... I need the 'category' variable
to be 'LIKE' but the way I have it set up it doent work. Anyone
have an idea of how I might fix this.
<CFQUERY name="getworkshops" datasource="source">
SELECT m.category, m.proj_key, m.project, r.proj_key, r.url,
r.category, r.end_date, r.title, e.event_date, e.event_date_end,
e.category, e.proj_key
FROM (main m INNER JOIN resource r ON m.category LIKE
r.category)INNER JOIN event_cal e ON m.category LIKE e.category
WHERE m.category like '%workshop%' AND m.proj_key=r.proj_key
AND m.proj_key = e.proj_key
ORDER BY m.proj_key
</CFQUERY>You could move your join statements to your where clause:
SELECT m.category, m.proj_key, m.project, r.proj_key, r.url,
r.category, r.end_date, r.title, e.event_date, e.event_date_end,
e.category, e.proj_key
FROM main m, resource r, event_cal e
WHERE m.category LIKE '%'+r.category+'%'
AND m.category LIKE '%" + e.category + '%'
AND m.category like '%workshop%' AND m.proj_key=r.proj_key
AND m.proj_key = e.proj_key
ORDER BY m.proj_key
I had to guess on how you wanted to match m.category against
resource and event_cal, but you get the idea. -
Alias for field in inner join query
In my Oracle 9i Schema, I have two tables:TableOne
pocOne pocTwo
2 3
2 4
1 2
TableTwo
TableTwoId Name
1 Jones
2 Smith
3 Edwards
4 CamdenMy SQL that works to fetch all records with Smith works great:select Name from TableTwo
Inner Join TableOne
on TableTwo.TableTwoId in (TableOne.pocOne, TableOne.pocTwo)
where Name = 'Smith' Now I need to create an alias for the Name field. Here is my attempt:select myAliasName from TableTwo
Inner Join TableOne
on TableTwo.TableTwoId in (TableOne.pocOne, TableOne.pocTwo), (select Name as myAliasName from TableTwo)
where myAliasName = 'Smith' This attempt pulls up all the records instead of just Smith records. Please advise how I can create an alias for the Name field in my above query?Are you just looking for
select Name AS AliasName
from TableTwo Inner Join TableOne
on TableTwo.TableTwoId in (TableOne.pocOne, TableOne.pocTwo)
where Name = 'Smith' Justin -
Inner join query not working properly
Hi everyone,
It does not gives any error, but it is not fetching any values...but when i dont include fourth table CSKT AND THE FIELD LTEXT it is fetching the values.
SELECT SINGLE
T1~PERNR
T1~BEGDA
T1~ENAME
T1~PLANS
T1~KOSTL
T2~STRAS
T2~PSTLZ
T2~ORT01
T3~PLSTX
T4~LTEXT
INTO FS_TAB
FROM PA0001 AS T1
INNER JOIN PA0006 AS T2 ON T1PERNR eq T2PERNR
INNER JOIN T528T AS T3 ON T1PLANS EQ T3PLANS
INNER JOIN CSKT AS T4 ON T1KOSTL EQ T4KOSTL
WHERE T1~PERNR eq p_pernr.Change like this
SELECT SINGLE
T1~PERNR
T1~BEGDA
T1~ENAME
T1~PLANS
T1~KOSTL
T2~STRAS
T2~PSTLZ
T2~ORT01
T3~PLSTX
T4~LTEXT
INTO FS_TAB
FROM PA0001 AS T1
INNER JOIN PA0006 AS T2 ON T1~PERNR eq T2~PERNR
INNER JOIN T528T AS T3 ON T1~PLANS EQ T3~PLANS
LEFT OUTER JOIN CSKT AS T4 ON T1~KOSTL EQ T4~KOSTL
WHERE T1~PERNR eq p_pernr.
OR
SELECT SINGLE
T1~PERNR
T1~BEGDA
T1~ENAME
T1~PLANS
T1~KOSTL
T2~STRAS
T2~PSTLZ
T2~ORT01
T3~PLSTX
T4~LTEXT
INTO FS_TAB
FROM PA0001 AS T1
LEFT OUTER JOIN PA0006 AS T2 ON T1~PERNR eq T2~PERNR
LEFT OUTER JOIN T528T AS T3 ON T1~PLANS EQ T3~PLANS
LEFT OUTER JOIN CSKT AS T4 ON T1~KOSTL EQ T4~KOSTL
WHERE T1~PERNR eq p_pernr.
as long as the table is not used in where condition, u can use left outter join. -
hello all,
Below is a inner join query
SELECT aebeln aebelp apackno bzekkn b~ps_psp_pnr
FROM ( ( ekpo AS a INNER JOIN ekkn AS b
ON aebeln = bebeln
AND aebelp = bebelp )
INNER JOIN prps AS c
ON bps_psp_pnr = cpspnr )
INTO TABLE t_wbspo
WHERE a~ebeln IN s_ebeln
AND c~psphi IN s_psphi
AND stufe = c_task.
I want to modify this select and put one more join on ekkn-aufnr
and aufk-afunr .
Now for this i write the below code
SELECT aebeln aebelp apackno bzekkn bps_psp_pnr baufnr
FROM ( ( ( ekpo AS a INNER JOIN ekkn AS b
ON aebeln = bebeln
AND aebelp = bebelp )
INNER JOIN prps AS c
ON bps_psp_pnr = cpspnr )
INNER JOIN aufk AS d
ON baufnr = daufnr )
INTO TABLE t_wbspo
WHERE a~ebeln IN s_ebeln
AND c~psphi IN s_psphi
AND d~aufnr IN s_aufnr
AND stufe = c_task.
By the above way does it puts a join on ekkn-aufnr and aufk-aufnr ?
Is the above way the right way to do it ?
Please confirm
regardsHello Bhanu,
Do you face any performance issue? Query looks good now, further you can refer to following threads to know more on JOIN-
Re: Inner join
http://help.sap.com/saphelp_nw04/helpdata/en/cf/21ec77446011d189700000e8322d00/frameset.htm
Thank You,
Nishikant Kumbhar. -
Alternate for inner join to improve performance
Hi all,
I have used an inner join query to fetch data from five different tables into an internal table with where clause conditions.
The execution time is almost 5-6 min for this particular query(I have more data in all five DB tables- more than 10 million records in every table).
Is there any alternate for inner join to improve performance.?
TIA.
Regards,
KarthikHi All,
Thanks for all your interest.
SELECT a~object_id a~description a~descr_language
a~guid AS object_guid a~process_type
a~changed_at
a~created_at AS created_timestamp
a~zzorderadm_h0207 AS cpid
a~zzorderadm_h0208 AS submitter
a~zzorderadm_h0303 AS cust_ref
a~zzorderadm_h1001 AS summary
a~zzorderadm_h1005 AS summary_uc
a~zzclose_date AS clsd_date
d~stat AS status
f~priority
FROM crmd_orderadm_h AS a INNER JOIN crmd_link AS b ON a~guid = b~guid_hi
INNER JOIN crmd_partner AS c ON b~guid_set = c~guid
INNER JOIN crm_jest AS d ON objnr = a~guid
INNER JOIN crmd_activity_h AS f ON f~guid = a~guid
INTO CORRESPONDING FIELDS OF TABLE et_service_request_list
WHERE process_type IN lt_processtyperange
AND a~created_at IN lt_daterange
AND partner_no IN lr_partner_no
AND stat IN lt_statusrange
AND object_id IN lt_requestnumberrange
AND zzorderadm_h0207 IN r_cpid
AND zzorderadm_h0208 IN r_submitter
AND zzorderadm_h0303 IN r_cust_ref
AND zzorderadm_h1005 IN r_trans_desc
AND d~inact = ' '
AND b~objtype_hi = '05'
AND b~objtype_set = '07'.
f~priority
FROM crmd_orderadm_h AS a INNER JOIN crmd_link AS b ON a~guid = b~guid_hi
INNER JOIN crmd_partner AS c ON b~guid_set = c~guid
INNER JOIN crm_jest AS d ON objnr = a~guid
INNER JOIN crmd_activity_h AS f ON f~guid = a~guid
INTO CORRESPONDING FIELDS OF TABLE et_service_request_list
WHERE process_type IN lt_processtyperange
AND a~created_at IN lt_daterange
AND partner_no IN lr_partner_no
AND stat IN lt_statusrange
AND object_id IN lt_requestnumberrange
AND zzorderadm_h0207 IN r_cpid
AND zzorderadm_h0208 IN r_submitter
AND zzorderadm_h0303 IN r_cust_ref
AND zzorderadm_h1005 IN r_trans_desc
AND d~inact = ' '
AND b~objtype_hi = '05'
AND b~objtype_set = '07'. -
Replacing a inner join with for all entries
Hi Team,
In a already developed program I am replacing a inner join with select query follow up with for-all-entris and passing the data to final internal table but in both the case the result should be same then only my replacement will be correct. But my no records in both cases differs. This happening because when i am selecting data from first data base table is 32 lines. then I am doing fo-all-entries moving all the duplicate entries then the no records are four. but in final internal table i am looping the first internal table. So in final internal table the no of records are 32. But in inner join query the records are 16.So please let me know how resolve this issue?
Thanks and REgards
DeepaHi Thomas,
Thanks for ur suggestion.
The solved that in below.
In select query I did not change anything The way I had written the code was correct.
I think many of us know how to write that how to make the performance better in that way.
I made the change when I transfered the to final internal table.
The original Inner join code:
select a~field1 a~field2 a~field3 b~field2 b~field3 b~field4
from dbtab1 as a inner join dbtab2 as b
on a~field1 = b~field1 into it_final where
a~field1 in s_field1. [Field1 in both the table are key field]
Before code:
Sort itab1 by key-fields.
sort itab2 by keyfields.
loop at itab1 into wa1.
move: wa1-field1 to wa_final-field1,
wa1-field2 to wa_final-field2,
wa1-field3 to wa_final-field3.
read table itab2 into wa2 witk key field1 = wa1-field1 binary search.
if sy-subrc = 0.
move : wa2-field2 to wa_final-field4,
wa2-field3 to wa_final-field5,
wa2-field4 to wa_final-field6.
append wa_final to it_final.
endif.
Clear : wa1, wa2, wa_final.
endloop.
In this case if the one key fieild value is not present there in second internal table but its there in first internal table still it will read that row with 2nd internal values having zeroes. Normally what does not happen in inner join case if the key field value will same in both the case ,then that will fetch only those rows.
Changed Code
loop at itab1 into wa1.
read table itab2 into wa2 witk key field1 = wa1-field1 binary search.
if sy-subrc = 0.
move: wa1-field1 to wa_final-field1,
wa1-field2 to wa_final-field2,
wa1-field3 to wa_final-field3.
move : wa2-field2 to wa_final-field4,
wa2-field3 to wa_final-field5,
wa2-field4 to wa_final-field6.
append wa_final to it_final.
endif.
Clear : wa1, wa2, wa_final.
endloop.
In this case the values will read to final internal table if both key field matches.
With Regards
Deepa -
Hi,
I need a suggestion for improvement of my inner JOIN statement.
internal table itab is of below type
type: begin of ty_itab
field a
field b
field c
field d
end of ty_itab
I have to join two database table TABLE1 and TABLE2. Fields a and d are from TABLE2 and fields b and c are from TABLE 1.
But in the internal table I must have the fields in the order fielda,b,c,d.
Which of the below two options will be better for performance ? And why ?
option 1:
SELECT tab2~a
tab1~b
tab1~c
tab2~d into corresponding fields of itab from TABLE1 as tab1 and TABLE2 as tab2 on..........
option2:
SELECT tab1~b
tab1~c
tab2~a
tab2~d into corresponding fields of itab from TABLE1 as tab1 and TABLE2 as tab2 on..........
Thanks in advance...
Best Regards
Ananya Mukherjee
Moderator message - Cross post locked
Edited by: Rob Burbank on May 28, 2009 9:43 AMI am not satisfied with your two options. Because of following reason,
1. First I want to know which one is master table and item table. Because in inner join select query you want to use master table as first and you want to use the item table as second.
Example:
SELECT TAB2A TAB1B TAB1C TAB2D
INTO TABLE ITAB
FROM <MASTER TABLE1> AS TAB1 INNER JOIN <ITEM TABLE2> AS TAB2 ON..........
2. You should not use the INTO CORRESPONDING FIELDS OF ITAB. Instead of this you can change the field order in internal table or in select query.
Example:
Use following way
TYPE: BEGIN OF TY_ITAB
FIELD A
FIELD B
FIELD C
FIELD D
END OF TY_ITAB
SELECT TAB2A TAB1B TAB1C TAB2D
INTO TABLE ITAB
FROM <MASTER TABLE1> AS TAB1 INNER JOIN <ITEM TABLE2> AS TAB2 ON..........
Or , Anyway you are going to use in program only. It will not affect your logic if change the field order in the internal table.
TYPE: BEGIN OF TY_ITAB
FIELD B
FIELD C
FIELD A
FIELD D
END OF TY_ITAB
SELECT TAB1B TAB1C TAB2A TAB2D
INTO TABLE ITAB
FROM <MASTER TABLE1> AS TAB1 INNER JOIN <ITEM TABLE2> AS TAB2 ON..........
3. In inner join query, the order of Where condition also create the perform issue. Because in where condition maximum you try to use all the primary key from <MASTER TABLE1> and <ITEM TABLE2> and field order in where condition should be same order in <MASTER TABLE1> and <ITEM TABLE>.
This are the things you want to take care when you try to use INNER JOIN Query. If you have any clarification please let me know. I will try answer your Questions.
Ravi.
Edited by: RavivarmanP on May 28, 2009 6:55 PM -
I just starting to Learn and use Dynamic SQL.
I am trying use a inner join query in dynamic SQL
for Example say 'Select A.id as Itemid , A.name as Itemname from TableA A inner join TableB B On A.ID = B.ID where B.ID typeid = 1 '
for some reason this is not working . i for sure know the query works as when i run the query it works fine. when i try to run it dynamically it give me blank values.
Please let me know if you need more information.
Appreicate Your help!!
Thanks,
ChaitanyaHere is an example for dynamic SQL from BOL:
DECLARE @IntVariable int;
DECLARE @SQLString nvarchar(500);
DECLARE @ParmDefinition nvarchar(500);
/* Build the SQL string one time.*/
SET @SQLString =
N'SELECT BusinessEntityID, NationalIDNumber, JobTitle, LoginID
FROM AdventureWorks2012.HumanResources.Employee
WHERE BusinessEntityID = @BusinessEntityID';
SET @ParmDefinition = N'@BusinessEntityID tinyint';
/* Execute the string with the first parameter value. */
SET @IntVariable = 197;
EXECUTE sp_executesql @SQLString, @ParmDefinition,
@BusinessEntityID = @IntVariable;
/* Execute the same string with the second parameter value. */
SET @IntVariable = 109;
EXECUTE sp_executesql @SQLString, @ParmDefinition,
@BusinessEntityID = @IntVariable;
Basically any query can be run as dynamic SQL. However, static SQL is the first choice. Use dynamic SQL if needed.
Dynamic SQL examples:
http://www.sqlusa.com/bestpractices/dynamicsql/
Kalman Toth Database & OLAP Architect
SQL Server 2014 Design & Programming
New Book / Kindle: Exam 70-461 Bootcamp: Querying Microsoft SQL Server 2012 -
Inner join inside a internal table loop
Hello to all,
I have this internal table in which i have selected some records for particular coulms. For the rest of the colums i need to join 3 tables for the data. I had in mind, of looping the internal table and then having a inner join query and modifying the internal table(all inside the loop) Just wanted to know, whether its a qood practice performance wise ? IF not, what are the alternatives ..
Thanks,
Shehryar Daharshehryar,
say You have internal table ITAB.Already some columns are updated in internal table.say A,B,C,D.
To fill rest of the columns(E,F,G) you need inter join.
write the inner join of the table for all entries of itab where A eq itab-A.
LOOP ITAB.
READ TABLE ITAB1 WITH KEY X = ITAB-X.
IF SY-SUBRC EQ 0.
move ITAB record to I_FINAL.
move itab1 records to i_final.
append i_final.
clear i_final.
ENDIF.
ENDLOOP.
Don't forget to reward if useful...
Maybe you are looking for
-
Exchange did notPush. MobileMe do Push
Hello, I have set an account with MobileMe (home Email) and the Email are pushed into my Iphone I have set exchange on my Iphone (work email) but email are not pushed? I have to sync manually Any clue? Exchange will never push or is this a settings i
-
Context Mapping and data binding
Hi, Please explain about the context mapping and data dinding and also differences Thank's & Regard's. Sri
-
Im trying to add text from a result set to a text area but the text simply isnt showing. When i try String s =(res.getString("OrderID")); System.out.println(s); it prints the corretc result to the screen. However, when i try String s =(res.getString(
-
RAR Alert Monitor - Critical Actions Report - user ID is garbled
In the above report, the alert generation has data in it, showing that a transaction was executed, from a terminal, but the user ID is garbled. It appears like this: Alert Date Time 8/10/2010 - 10:32:34 AM User +LqvhQveEQJ (+LqvhQveEQJ) Risk V
-
E71 wifi and maps issues.
Does anyone have this problem and know how to fix it? the problem i have is trying to save the maps through wifi (i dont want to use packet data, dont have a plan, although it works and im gonna regret fiddling with it when i get my bill).... it just