JOIN in select query
Hi All,
I need to do an enhancement in the following Select query.
SELECT
MARA data
a~matnr
MARC data
b~werks
MARA data
a~normt
a~ean11
a~labor
a~mstae
a~mstde
a~bismt
a~spart
a~zzsc
a~zzmc
a~zzpc
a~zzbu
a~zz4tpc
a~zz6tpc
a~kosch
a~iprkz
g~bmatn
a~normt
g~mfrnr
a~bstme
a~zeifo
a~ersda
MARC data
b~mtvfp
b~sernp
b~herkl
b~ekgrp
b~dispo
b~insmk
b~disgr
b~ladgr
b~lgfsb
b~mtver
b~perkz
b~eprio
b~wzeit
MVKE data
c~dwerk
c~kondm
c~bonus
c~mtpos
c~provg
c~mvgr1
c~mvgr2
c~mvgr3
c~prat9
c~prat1
MBEW data
d~bklas
d~stprs
INTO TABLE i_materials
FROM ( marc as b
inner jOIN mara AS a ON amatnr = bmatnr
inner JOIN mvke AS c ON cmatnr = amatnr
inner JOIN ampl AS g ON gbmatn = amatnr
LEFT OUTER JOIN mbew AS d ON dmatnr = bmatnr
AND dbwkey = bwerks )
WHERE a~matnr IN s_matnr
AND a~normt IN s_normt
AND a~mstae IN s_mstae
AND a~zz4tpc IN s_zz4tpc
AND a~zz6tpc IN s_zz6tpc
AND a~bismt IN s_bismt
AND a~iprkz IN s_iprkz
AND a~spart IN s_spart
AND a~zzsc IN s_zzsc
AND a~zzpc IN s_zzpc
AND a~zzmc IN s_zzmc
AND a~zzbu IN s_zzbu
AND a~ersda IN s_ersda
MARC Data
AND b~werks IN s_werks
AND b~ekgrp IN s_ekgrp
AND b~disgr IN s_disgr
AND b~perkz IN s_perkz
AND b~sernp IN s_sernp
AND b~ladgr IN s_ladgr
AND b~mtvfp IN s_mtvfp
AND b~lgfsb IN s_lgfsb
AND b~mtver IN s_mtver
AND b~eprio IN s_eprio
AND b~wzeit IN s_wzeit
AND b~herkl IN s_herkl
MVKE data
AND c~mtpos IN s_mtpos
AND c~kondm IN s_kondm
AND c~bonus IN s_bonus
AND c~provg IN s_provg
AND c~mvgr1 IN s_mvgr1
AND c~mvgr2 IN s_mvgr2
AND c~mvgr3 IN s_mvgr3
AMPL Data
AND g~reoam <> 'X'.
This query will pick the materials only if it is maintained across all the tables MARA,MARC,MVKE,AMPL,MBEW.
Now the requirement is, the program should pick the materials that are maintained in MARA table, doesn't matter if the material is not maintained in the other tables.
Please tell me how to modify this Select query. I dont have any idea on JOINs.
Thanks in Advance,
Neethu.
Hi Manoj,
I have modified the query like
select fields
FROM ( mara as a
left outer join marc AS b ON amatnr = bmatnr
left outer Join mvke AS c ON amatnr = cmatnr
left outer Join ampl AS g ON amatnr = gbmatn
left outer Join mbew AS d ON amatnr = dmatnr )
WHERE a~matnr IN s_matnr
AND a~normt IN s_normt
AND a~mstae IN s_mstae
AND a~zz4tpc IN s_zz4tpc
AND a~zz6tpc IN s_zz6tpc
AND a~bismt IN s_bismt
AND a~iprkz IN s_iprkz
AND a~spart IN s_spart
AND a~zzsc IN s_zzsc
AND a~zzpc IN s_zzpc
AND a~zzmc IN s_zzmc
AND a~zzbu IN s_zzbu
AND a~ersda IN s_ersda
MARC Data
AND b~werks IN s_werks
AND b~ekgrp IN s_ekgrp
AND b~disgr IN s_disgr
AND b~perkz IN s_perkz
AND b~sernp IN s_sernp
AND b~ladgr IN s_ladgr
AND b~mtvfp IN s_mtvfp
AND b~lgfsb IN s_lgfsb
AND b~mtver IN s_mtver
AND b~eprio IN s_eprio
AND b~wzeit IN s_wzeit
AND b~herkl IN s_herkl
MVKE data
AND c~mtpos IN s_mtpos
AND c~kondm IN s_kondm
AND c~bonus IN s_bonus
AND c~provg IN s_provg
AND c~mvgr1 IN s_mvgr1
AND c~mvgr2 IN s_mvgr2
AND c~mvgr3 IN s_mvgr3
AMPL Data
AND g~reoam <> 'X'.
I am getting the error, "No fields from the right-hand table of a LEFT OUTER JOIN may appear in the WHERE condition:"B~WERKS".
Now prob. is with WHERE condition. If you know the solution please let me know.
Similar Messages
-
Problem with JOINs in SELECT query
The parameter wa_dob-e_dob is not getting its definition ....
it contains DOB in current yr.
As i am using Joins also Workarea.... so is it coz Joins & WA cant be used simultaneously in a SELECT query....
Is there any other option to do the same...?
* Global Structure
TYPES : BEGIN OF t_data , "Structure
emp_dob TYPE pa0002-gbdat,
emp_Mailid TYPE pa0105-usrid,
END OF t_data,
BEGIN OF t_dob,
e_dob TYPE pa0002-gbdat,
e_pernr type pa0002-pernr,
END OF t_dob.
* Global Internal Tables (I_)
DATA : i_data TYPE TABLE OF t_data, "Internal Table for output data
i_dob TYPE TABLE OF t_dob. "Internal table for Dob
* Global Work Area (WA_)
DATA : wa_data TYPE t_data, "Work Area for output data
wa_dob TYPE t_dob. "Work Area for Dob
*this is the query where i am getting error
* Field "WA_DOB-E_DOB" Not found.
LOOP AT i_dob INTO wa_dob.
SELECT pa0002~gbdat pa0105~usrid INTO CORRESPONDING FIELDS OF wa_data
FROM pa0002
INNER JOIN pa0105
ON pa0002~pernr = pa0105~pernr
WHERE pa0105~subty EQ 'MAIL' AND
wa_dob-e_dob BETWEEN w_sysdate AND w_edit_sysdate.
ENDSELECT.
APPEND wa_data TO i_data.
CLEAR wa_data.
CLEAR wa_dob.
CLEAR l_temp_dob.
ENDLOOP.Hi,
you are checking the work area in the SELECT. IN WHERE condition of the select u should specify fields in either of the tables pa0002 , pa0105 for data population.
The select is on the database table and not on the work area.for checking the data in the work area do this way:
loop at it_dob into wa_dob.
if wa_dob-e_dob between the range u want (w_sysdate AND w_edit_sysdate)
endif.
endloop.
ie: wa_dob-e_dob >w_sysdate AND wa_dob-e_dob < w_edit_sysdate or
wa_dob-e_dob < w_sysdate AND wa_dob-e_dob > w_edit_sysdate
watever ur requirement is..
regards,
madhu -
How to replace Below Joins in select query ?
SELECT ko~ebeln ko~lifnr a1~name1
ko~bsart " Purchasing Document Type
po~menge " PO quantity
po~meins " PO Unit
po~netpr " Net Price
ko~kdate " End of validity period
po~pstyp " Item category in PO
po~wepos " Goods Receipt indicator
ko~spras " Language Key
ko~frggr " Release group
ko~frgsx " Release strategy
ko~frgke " Release Indicator
po~matnr
po~txz01
po~ebelp
po~werks
po~lgort
ko~waers
ko~ernam
ko~aedat
po~netwr
po~repos
INTO TABLE itab_details
FROM ekko AS ko INNER JOIN ekpo AS po
ON ko~ebeln = po~ebeln
INNER JOIN t161 AS t1
ON ko~bsart = t1~bsart
AND ko~bstyp = t1~bstyp
INNER JOIN lfa1 AS a1
ON ko~lifnr = a1~lifnr
WHERE ko~ebeln IN s_ebeln
AND po~loekz IN s_loekz
AND po~werks IN s_werks
AND po~lgort IN s_lgort
AND ko~lifnr IN s_lifnr
AND ko~bukrs IN s_bukrs
AND t1~bsart IN s_bsart
AND po~matnr IN s_matnr
AND ko~ernam IN s_ernam
AND ko~aedat IN s_aedat
AND po~knttp IN s_knttp
AND ko~ekgrp IN s_ekgrp
AND po~pstyp IN s_pstyp.HI Amresh,
Try like this.
use inner join between Ekko and Ekpo.
Then use for all entries for table t161
and finally use for all entries for table lfa1
and use read in loop.
Regards,
Vijay -
hey guys,
i've used a select query to get details from KNA1, KNVV, ADRC, KNVP, pa0002. tables by joining. but i'm getting only about 170 records. there are more than 3000 records in the database. for each tables. here is my select query
SELECT KNA1KUNNR KNA1NAME1 KNA1NAME2 ADRCSTR_SUPPL1 ADRCSTR_SUPPL2 ADRCSTR_SUPPL3 ADRCCITY1 KNVVVWERK KNVPPERNR PA0002VORNA PA0002NACHN KNVVVKBUR KNVVKDGRP KNA1STCD1 KNA1~STCEG
INTO CORRESPONDING FIELDS OF TABLE IT_FINAL FROM KNA1
INNER JOIN KNVV ON KNA1KUNNR = KNVVKUNNR
INNER JOIN ADRC ON KNA1ADRNR = ADRCADDRNUMBER
INNER JOIN KNVP ON KNA1KUNNR = KNVPKUNNR
INNER JOIN PA0002 ON KNVPPERNR = PA0002PERNR
WHERE KNA1KUNNR IN S_KUNNR AND KNVVVWERK in P_VWERK AND PA0002PERNR IN S_PERNR AND KNVVVKBUR IN S_VKBUR AND KNVV~KDGRP IN S_KDGRP.
can anyone tell me the problem in the above query.
thanks.Hi
you are using tables KNA1, KNVV, ADRC, KNVP, pa0002.
Create internal table for each table mentioned above with all the primary keys and find the links between them
then select data in one table and select data from another table into your internal table for all the entries exists in firs internal table.
go for below links.
http://www.sapdev.co.uk/abap/commands/select_forallentries.htm
http://wiki.sdn.sap.com/wiki/display/ABAP/FORALLENTRIES-Home+page
http://help.sap.com/abapdocu_70/en/ABENWHERE_LOGEXP_ITAB.htm
Thanks
lalit -
Dynamic From statement in select query and/or outer join not working
Dear Experts, I have a select query where the select columns are dynamic, the where condition is also dynamic. It is of the below format:
Select (dynamic columns) INTO <wa>
FROM a inner join b on af1 = bf1
inner join c on af2 = cf2......
WHERE (dynamic conditios)
ORDER BY ( dynamic sort condition).
Now I have to include some tables (dynamically depending on the user input) in the inner join statement which will give description for the selected fields. And these database tables may or may no be empty. So in this case, my select query will not return any data if these tables are empty. And I dont want that.
I tried using outer join for the extra tables but it gave me a runtime error. I also tried forming the inner join statement dynamically but it was not supporting.
Kindly give me pointers.
ThanksHey thanks for the reply, but the problem is not solved.
I am already using ( fileds, value) like table in my where condition and the select statement was working properly.
the problem is that now I have to include some tables in the join statement which can be empty and so i want to use Outer join.
But I am getting a runtime error as below:
Error analysis
An exception occurred that is explained in detail below.
The exception, which is assigned to class 'CX_SY_DYNAMIC_OSQL_SYNTAX', was not
caught in
procedure "ZATSCSNG_RFC_READ_TABLE" "(FUNCTION)", nor was it propagated by a
RAISING clause.
Since the caller of the procedure could not have anticipated that the
exception would occur, the current program is terminated.
The reason for the exception is:
The running ABAP program wanted to execute a SELECT statement whose
WHERE condition was (partly) specified dynamically. The part that is
specified in an internal table at runtime is compared to a field of the
right table of an LEFT OUTER JOIN. Such comparisons are not supported by
all database systems and are therefore not allowed. -
Select query based on joining of tables from different database is taking too long
Hi Team,
Select query on table with millions of records is taking very long time. It took 50mins, the below query is joining on multiple table from two databases DB1.dbo.Table1 contains 100 million records and also Table3 and Table4 (of different database) might
contain close to 1million records.
Select T1.*
From DB1.dbo.Table1 T1
Join DB1.dbo.Table2 T2 on RTRIM(T1.Col3) = RTRIM(T2.Col3)
Join Table4 CA on RTRIM(T1.Col1) + T2.Col2 = CA.Col1
Join Table3 U on CA.Col2 = U.Col2 AND RTRIM(T2.Col2) = U.Col3
Where U.Col4 NOT IN ('A1', 'A2', 'A3', 'A4', 'A5', 'A6','A7','A8','A9')
And (T1.flg IS NULL OR T1.flg = 'N')
And LTRIM(RTRIM(T2.Col2)) NOT IN ('B1','B2')
How can i improve the performance of this query. Actual thing is update the data in Db1.dbo.table1 based on the conditions but if the select is taking close to 1hr then update will take hours together. Indexes already implemented on all the tables.
Thanks,
Eshwar.
Please don't forget to Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful. It will helpful to other users.Thanks.
Join Table4 CA on RTRIM(T1.Col1) + T2.Col2 = CA.Col1
This join is not working with spaces if Rtrim is not used.
Thanks! Eshwar.
Please don't forget to Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful. It will helpful to other users. -
Absolute dynamic select query with dynamic join and where
Has anyone ever tried creating an absolutely dynamic SELECT query with dynamic Join and Where conditions.
I have a requirement of creating such a query in an Utility Class, and i have written the code. But its throwing my sysntax errors.
Please let me know where am I going wrong OR is it really possible to create such a dynamic Query??
SELECT (FIELDS) INTO TABLE IT_TABLES
FROM ( (ME->TABLE1) inner join ( me->table2 )
on ( on_condition ) )
WHERE (me->where_fields).
Ags.It worked for me in a following way:
select * into corresponding fields of table <result_table>
from (join_string)
where (l_where).
Where the contents of join_string were dynamically build using concatenation. So it will be something like
concatenate ME->TABLE1 'as a INNER JOIN' me->table2 'as b ON (' into join_string separated by space.
<...>
add here matching/reference colums, something like
concatenate 'a~' me->TABLE1_JOIN_COL into temp1.
concatenate 'b~' me->TABLE2_JOIN_COL into temp2.
concatenate join_string temp1 '=' temp2 into join_string separated by space.
<...>
concatenate join_string ')' into join_string separated by space.
And then use similar approach for l_where variable. -
Dear Experts,
I have writen a inner join code with MKPF and MSEG which taking too much time, while I have used index.
Indexes are:
MSEG
MATNR
WERKS
LGORT
BWART
SOBKZ
MKPF
BUDAT
MBLNR
My Select Query is :
SELECT B~MATNR
B~MAT_KDAUF
B~MAT_KDPOS
B~BWART
B~MENGE
B~MEINS
B~AUFNR
INTO TABLE IT_MSEG
FROM MKPF AS A
INNER JOIN MSEG AS B
ON AMBLNR EQ BMBLNR
AND AMJAHR EQ BMJAHR
WHERE A~BUDAT IN BUDAT
AND B~MATNR IN MATNR
AND B~WERKS IN WERKS
AND B~LGORT IN LGORT
AND B~BWART IN BWART.hi,
you can use for all entries it will work faster then joins
About it:*
FOR ALL ENTRIES WHERE
Syntax
... FOR ALL ENTRIES IN itab WHERE ... col operator itab-comp ...
Effect
If the addition FOR ALL ENTRIES is specified before the language element WHERE, then the components comp of the internal table itab can be used as operands when comparing with relational operators.
The internal table itab must have a structured line type and the component comp must be compatible with the column col.
The logical expression sql_cond of the WHERE condition can comprise various logical expressions by using AND and OR. However, if FOR ALL ENTRIES is specified, there must be at least one Comparison with a column of the internal table itab, which can be specified either statistically or dynamically (Release 6.40 and higher). In a statement with a SELECTstatement with FOR ALL ENTRIES, the addition ORDER BY can only be used with the addition PRIMARY KEY.
The whole logical expression sql_cond is evaluated for each individual line of the internal table itab. The resulting set of the SELECT statement is the union of the resulting sets from the individual evaluations. Duplicate lines are automatically removed from the resulting set. If the internal table itab is empty, the whole WHERE statement is ignored and all lines in the database are put in the resulting set.
Notes
In Release 6.10 and higher, the same internal table can be specified after FOR ALL ENTRIES and after INTO.
The addition FOR ALL ENTRIES is only possible before WHERE conditions of the SELECT statement.
Example
Exporting all flight data for a specified departure city. The relevant airlines and flight numbers are first put in an internal table entry_tab, which is evaluated in the WHERE condition of the subsquent SELECT statement.
PARAMETERS p_city TYPE spfli-cityfrom.
TYPES: BEGIN OF entry_tab_type,
carrid TYPE spfli-carrid,
connid TYPE spfli-connid,
END OF entry_tab_type.
DATA: entry_tab TYPE TABLE OF entry_tab_type,
sflight_tab TYPE SORTED TABLE OF sflight
WITH UNIQUE KEY carrid connid fldate.
SELECT carrid connid
FROM spfli
INTO CORRESPONDING FIELDS OF TABLE entry_tab
WHERE cityfrom = p_city.
SELECT carrid connid fldate
FROM sflight
INTO CORRESPONDING FIELDS OF TABLE sflight_tab
FOR ALL ENTRIES IN entry_tab
WHERE carrid = entry_tab-carrid AND
connid = entry_tab-connid.
hope it will help you
Rahul sharma
Edited by: RAHUL SHARMA on Sep 11, 2008 8:10 AM -
How to do outer join select query for an APEX report
Hello everyone,
I am Ann.
I have one select statement that calculate the statistics for one month(October 2012 in this example)
select ph.phase_number
, sum ( (case
WHEN ph.date_finished IS NULL OR ph.date_finished > last_day(TO_DATE('Oct 2012','MON YYYY'))
THEN last_day(TO_DATE('Oct 2012','MON YYYY'))
ELSE ph.date_finished
END )
- ph.date_started + 1) / count(def.def_id) as avg_days
from phase_membership ph
inner join court_engagement ce on ph.mpm_eng_id = ce.engagement_id
inner join defendant def on ce.defendant_id = def.def_id
where def.active = 1
and ph.date_started <= last_day(TO_DATE('Oct 2012','MON YYYY'))
and ph.active = 1
and UPPER(ce.court_name) LIKE '%'
group by rollup(phase_number)
Result is as below
Phase_Number AVG_DAYS
Phase One 8.6666666666666667
Phase Two 14.6
Phase Three 12
11.4615365
I have other select list mainly list the months between two date value.
select to_char(which_month, 'MON YYYY') as display_month
from (
select add_months(to_date('Aug 2012','MON YYYY'), rownum-1) which_month
from all_objects
where
rownum <= months_between(to_date('Oct 2012','MON YYYY'), add_months(to_date('Aug 2012','MON YYYY'), -1))
order by which_month )
Query result is as below
DISPLAY_MONTH
AUG 2012
SEP 2012
OCT 2012
Is there any way that I can join these two select statement above to generate a result like:
Month Phase Number Avg days
Aug 2012 Phase One 8.666
Sep 2012 Phase One 7.66
Oct 2012 Phase One 5.66
Aug 2012 Phase Two 8.666
Sep 2012 Phase Two 7.66
Oct 2012 Phase Two 5.66
Aug 2012 Phase Three 8.666
Sep 2012 Phase Three 7.66
Oct 2012 Phase Three 5.66
Or
Month Phase Number Avg days
Aug 2012 Phase One 8.666
Aug 2012 Phase Two 7.66
Aug 2012 Phase Three 5.66
Sep 2012 Phase One 8.666
Sep 2012 Phase Two 7.66
Sep 2012 Phase Three 5.66
Oct 2012 Phase One 8.666
Oct 2012 Phase Two 7.66
Oct 2012 Phase Three 5.66
And it can be order by either Phase Number or Month.
My other colleague suggest I should use an left outer join but after trying so many ways, I am still stuck.
One of the select I tried is
select a.display_month,b.* from (
select to_char(which_month, 'MON YYYY') as display_month
from (
select add_months(to_date('Aug 2012','MON YYYY'), rownum-1) which_month
from all_objects
where
rownum <= months_between(to_date('Oct 2012','MON YYYY'), add_months(to_date('Aug 2012','MON YYYY'), -1))
order by which_month )) a left outer join
( select to_char(ph.date_finished,'MON YYYY') as join_month, ph.phase_number
, sum ( (case
WHEN ph.date_finished IS NULL OR ph.date_finished > last_day(TO_DATE(a.display_month,'MON YYYY'))
THEN last_day(TO_DATE(a.display_month,'MON YYYY'))
ELSE ph.date_finished
END )
- ph.date_started + 1) / count(def.def_id) as avg_days
from phase_membership ph
inner join court_engagement ce on ph.mpm_eng_id = ce.engagement_id
inner join defendant def on ce.defendant_id = def.def_id
where def.active = 1
and ph.date_started <= last_day(TO_DATE(a.display_month,'MON YYYY'))
and ph.active = 1
and UPPER(ce.court_name) LIKE '%'
group by to_char(ph.date_finished,'MON YYYY') , rollup(phase_number)) b
on a.display_month = b.join_month
but then I get an error
SQL Error: ORA-00904: "A"."DISPLAY_MONTH": invalid identifier
I need to display a report on APEX with option for people to download at least CSV format.
I already have 1 inteactive report in the page, so don’t think can add another interactive report without using the iframe trick.
If any of you have any ideas, please help.
Thanks a lot.
AnnFirst of all, a huge thanks for following this Frank.
I have just started working here, I think the Oracle version is 11g, but not sure.
To run Oracle APEX version 4, I think they must have at least 10g R2.
This report is a bit challenging for me.I has never worked with PARTITION before.
About the select query you suggested, I run , and it seems working fine, but if I try this,
it return error ORA-01843: not a valid month
DEFINE startmonth = "Aug 2012";
DEFINE endmonth = "Oct 2012";
WITH all_months AS
select add_months(to_date('&startmonth','MON YYYY'), rownum-1) AS which_month
, add_months(to_date('&startmonth','MON YYYY'), rownum ) AS next_month
from all_objects
where
rownum <= months_between(to_date('&endmonth','MON YYYY'), add_months(to_date('&startmonth','MON YYYY'), -1))
select TO_CHAR (am.which_month, 'Mon YYYY') AS month
, ph.phase_number
, sum ( (case
WHEN ph.date_finished IS NULL OR ph.date_finished > last_day(TO_DATE(am.which_month,'MON YYYY'))
THEN last_day(TO_DATE(am.which_month,'MON YYYY'))
ELSE ph.date_finished
END )
- ph.date_started + 1) / count(def.def_id) as avg_days
FROM all_months am
LEFT OUTER JOIN phase_membership ph PARTITION BY (ph.phase_number)
ON am.which_month <= ph.date_started
AND am.next_month > ph.date_started
AND ph.date_started <= last_day(TO_DATE(am.which_month,'MON YYYY')) -- May not be needed
AND ph.active = 1
LEFT OUTER join court_engagement ce on ph.mpm_eng_id = ce.engagement_id
and ce.court_name IS NOT NULL -- or something involving LIKE
LEFT OUTER join defendant def on ce.defendant_id = def.def_id
AND def.active = 1
group by rollup(phase_number, am.which_month)
ORDER BY am.which_month
, ph.phase_number
;Here is the shorted versions of the three tables:
A_DEFENDANT, A_ENGAGEMENT, A_PHASE_MEMBERSHIP
CREATE TABLE "A_DEFENDANT"
"DEF_ID" NUMBER NOT NULL ENABLE,
"FIRST_NAME" VARCHAR2(50 BYTE),
"SURNAME" VARCHAR2(20 BYTE) NOT NULL ENABLE,
"DOB" DATE NOT NULL ENABLE,
"ACTIVE" NUMBER(2,0) DEFAULT 1 NOT NULL ENABLE,
CONSTRAINT "A_DEFENDANT_PK" PRIMARY KEY ("DEF_ID"))
Sample Data
Insert into A_DEFENDANT (DEF_ID,FIRST_NAME,SURNAME,DOB,ACTIVE) values (101,'Joe','Bloggs',to_date('12/12/99','DD/MM/RR'),1);
Insert into A_DEFENDANT (DEF_ID,FIRST_NAME,SURNAME,DOB,ACTIVE) values (102,'John','Smith',to_date('20/05/00','DD/MM/RR'),1);
Insert into A_DEFENDANT (DEF_ID,FIRST_NAME,SURNAME,DOB,ACTIVE) values (103,'Jane','Black',to_date('15/02/98','DD/MM/RR'),1);
Insert into A_DEFENDANT (DEF_ID,FIRST_NAME,SURNAME,DOB,ACTIVE) values (104,'Minnie','Mouse',to_date('13/12/88','DD/MM/RR'),0);
Insert into A_DEFENDANT (DEF_ID,FIRST_NAME,SURNAME,DOB,ACTIVE) values (105,'Daisy','Duck',to_date('05/08/00','DD/MM/RR'),1);
CREATE TABLE "A_ENGAGEMENT"
"ENGAGEMENT_ID" NUMBER NOT NULL ENABLE,
"COURT_NAME" VARCHAR2(50 BYTE) NOT NULL ENABLE,
"DATE_REFERRED" DATE,
"DETERMINATION_HEARING_DATE" DATE,
"DATE_JOINED_COURT" DATE,
"DATE_TREATMENT_STARTED" DATE,
"DATE_TERMINATED" DATE,
"TERMINATION_TYPE" VARCHAR2(50 BYTE),
"ACTIVE" NUMBER(2,0) DEFAULT 1 NOT NULL ENABLE,
"DEFENDANT_ID" NUMBER,
CONSTRAINT "A_ENGAGEMENT_PK" PRIMARY KEY ("ENGAGEMENT_ID"))
Insert into A_ENGAGEMENT (ENGAGEMENT_ID,COURT_NAME,DATE_REFERRED,DETERMINATION_HEARING_DATE,DATE_JOINED_COURT,DATE_TREATMENT_STARTED,DATE_TERMINATED,TERMINATION_TYPE,ACTIVE,DEFENDANT_ID) values (1,'AA',to_date('12/08/12','DD/MM/RR'),null,to_date('12/08/12','DD/MM/RR'),null,null,null,1,101);
Insert into A_ENGAGEMENT (ENGAGEMENT_ID,COURT_NAME,DATE_REFERRED,DETERMINATION_HEARING_DATE,DATE_JOINED_COURT,DATE_TREATMENT_STARTED,DATE_TERMINATED,TERMINATION_TYPE,ACTIVE,DEFENDANT_ID) values (2,'BB',to_date('01/09/12','DD/MM/RR'),null,to_date('02/09/12','DD/MM/RR'),null,null,null,1,102);
Insert into A_ENGAGEMENT (ENGAGEMENT_ID,COURT_NAME,DATE_REFERRED,DETERMINATION_HEARING_DATE,DATE_JOINED_COURT,DATE_TREATMENT_STARTED,DATE_TERMINATED,TERMINATION_TYPE,ACTIVE,DEFENDANT_ID) values (3,'AA',to_date('02/09/12','DD/MM/RR'),null,to_date('15/09/12','DD/MM/RR'),null,null,null,1,103);
Insert into A_ENGAGEMENT (ENGAGEMENT_ID,COURT_NAME,DATE_REFERRED,DETERMINATION_HEARING_DATE,DATE_JOINED_COURT,DATE_TREATMENT_STARTED,DATE_TERMINATED,TERMINATION_TYPE,ACTIVE,DEFENDANT_ID) values (4,'BB',to_date('01/10/12','DD/MM/RR'),null,to_date('02/10/12','DD/MM/RR'),null,null,null,1,105);
CREATE TABLE "A_PHASE_MEMBERSHIP"
"MPM_ID" NUMBER NOT NULL ENABLE,
"MPM_ENG_ID" NUMBER NOT NULL ENABLE,
"PHASE_NUMBER" VARCHAR2(50 BYTE),
"DATE_STARTED" DATE NOT NULL ENABLE,
"DATE_FINISHED" DATE,
"NOTES" VARCHAR2(2000 BYTE),
"ACTIVE" NUMBER(2,0) DEFAULT 1 NOT NULL ENABLE,
CONSTRAINT "A_PHASE_MEMBERSHIP_PK" PRIMARY KEY ("MPM_ID"))
Insert into A_PHASE_MEMBERSHIP (MPM_ID,MPM_ENG_ID,PHASE_NUMBER,DATE_STARTED,DATE_FINISHED,NOTES,ACTIVE) values (1,1,'PHASE ONE',to_date('15/09/12','DD/MM/RR'),to_date('20/09/12','DD/MM/RR'),null,1);
Insert into A_PHASE_MEMBERSHIP (MPM_ID,MPM_ENG_ID,PHASE_NUMBER,DATE_STARTED,DATE_FINISHED,NOTES,ACTIVE) values (2,1,'PHASE TWO',to_date('21/09/12','DD/MM/RR'),to_date('29/09/12','DD/MM/RR'),null,1);
Insert into A_PHASE_MEMBERSHIP (MPM_ID,MPM_ENG_ID,PHASE_NUMBER,DATE_STARTED,DATE_FINISHED,NOTES,ACTIVE) values (3,2,'PHASE ONE',to_date('12/09/12','DD/MM/RR'),null,null,1);
Insert into A_PHASE_MEMBERSHIP (MPM_ID,MPM_ENG_ID,PHASE_NUMBER,DATE_STARTED,DATE_FINISHED,NOTES,ACTIVE) values (4,3,'PHASE ONE',to_date('20/09/12','DD/MM/RR'),to_date('01/10/12','DD/MM/RR'),null,1);
Insert into A_PHASE_MEMBERSHIP (MPM_ID,MPM_ENG_ID,PHASE_NUMBER,DATE_STARTED,DATE_FINISHED,NOTES,ACTIVE) values (5,3,'PHASE TWO',to_date('02/10/12','DD/MM/RR'),to_date('15/10/12','DD/MM/RR'),null,1);
Insert into A_PHASE_MEMBERSHIP (MPM_ID,MPM_ENG_ID,PHASE_NUMBER,DATE_STARTED,DATE_FINISHED,NOTES,ACTIVE) values (6,4,'PHASE ONE',to_date('03/10/12','DD/MM/RR'),to_date('10/10/12','DD/MM/RR'),null,1);
Insert into A_PHASE_MEMBERSHIP (MPM_ID,MPM_ENG_ID,PHASE_NUMBER,DATE_STARTED,DATE_FINISHED,NOTES,ACTIVE) values (7,3,'PHASE THREE',to_date('17/10/12','DD/MM/RR'),null,null,0);
Insert into A_PHASE_MEMBERSHIP (MPM_ID,MPM_ENG_ID,PHASE_NUMBER,DATE_STARTED,DATE_FINISHED,NOTES,ACTIVE) values (8,1,'PHASE THREE',to_date('30/09/12','DD/MM/RR'),to_date('16/10/12','DD/MM/RR'),null,1);
The requirements are:
The user must be able to request the extract for one or more calendar months, e.g.
May 2013
May 2013 – Sep 2013.
The file must contain a separate row for each calendar month in the requested range. Each row must contain the statistics computed for that calendar month.
The file must also include a row of totals.
The user must be able to request the extract for either Waitakere or Auckland or Consolidated (both courts’ statistics accumulated).
Then the part that I am stuck is
For each monitoring phase:
Phase name (e.g. “Phase One”)
Avg_time_in_phase_all_particip
for each phase name,
Add up days in each “phase name” Monitoring Phase, calculated as:
If Monitoring Phase.Date Finished is NULL or > month end date,
+(*Month end date* Minus Monitoring Phase.Date Started Plus 1)+
Otherwise (phase is complete)
+(Monitoring Phase.Date Finished Minus Monitoring Phase.Date Started Plus 1.)+
Divide by the numbers of all participants who have engaged in “phase name”.
This is the words of the Business Analyst,
I try to do as required but still struggle to identify end_month for the above formula to display for the range of months.
Of course, I can write two nested cursor. The first one run the list of month, then for each month, run the parameterised report.
But I prefer if possible just use SQL statements, or at least a PL/SQL but return a query.
With this way, I can create an APEX report, and use their CSV Extract function.
Yes, you are right, court_name is one of the selection parameters.
And the statistics is not exactly for one month. It is kind of trying to identify all phases that are running through the specified month (even phase.date_started is before the month start).
This is the reason why I put the condition AND ph.date_started <= last_day(TO_DATE('Oct 2012','MON YYYY')) (otherwise I get negative avg_days)
User can choose either one court "AA" or "BB" or combined which is all figures.
Sorry for bombarding you a lot of information.
Thanks a lot, again.
Edited by: Ann586341 on Oct 29, 2012 9:57 PM
Edited by: Ann586341 on Oct 29, 2012 9:59 PM -
SELECT Query design with JOINS
Dear Users,
In one of our requirements, we have a SELECT query which joins a few tables together and selects a few fields from each table.
Example: Fields 1,2 and 3 from Table 1
Fields 4 and 5 from Table 2.
These fields are later displayed in the form of an ALV. However, the end user wants more fields on the output, let's say field 6 from Table 2. Due to this requirement, I have to amend the SELECT query to add field 6 of Table 2.
The above example is simple since it only involves 2 tables. However, in reality, we have a SELECT query with INNER JOINS on more than 6 tables and we don't want to change the code when someone asks for an additional field from one of these tables. Is there any way by which I can design my SELECT query (Using field symbols or dynamic selections etc) such that I don't have to amend the SELECT query and instead I should be able to maintain the field required by the user and it's source table name in a Z-Table and the SELECT automatically picks it up ? Thanks!
Vijaybetter you can create a view on your 6 or more tables and select the fields .
Use Select * from <view name> every time in a select statement.
If you need a additional column then just change the view , there is no need to change of your select query.
with regards
Edited by: Avenai on Jun 28, 2010 2:09 PM -
Select Query and Join Between int and Numeric(22)
I have two tables
tablea
Field1 int
tableb
Field1 Numeric(22)
I know this design is wrong but this is what I have ?
tablea contains about 12M records
Field1 in tablea is one to one related to Field1 in tableb
Now I have Select Query
Select tablea.* from tablea,tableb where tablea.field1=tableb.field1 and tableb.field2 > somrthing
Query here is
Does Join Between
int typeYes, avinash said rightly.
there is another way bit diffict.
use the indexes properly as available in the mseg table and mkpf table even then it is diffcult
else try using BAPI.
some bapi will give the entire details of the MATERAIL DOCUMENT
go to BAPI explorer tcode BAPI.
where you can find the BAPI name like the 'GET_LIST'.
Try this.
hope this will serve your purpose.
Thanks and regards
Ramchander Rao.K -
Hi All,
I had to modify the current select query coz its taking a lot of database time, please suggest the best solution for the below.
SELECT msegbukrs mkpfbudat mkpf~bldat
msegebeln mkpfxblnr mseg~mblnr
msegmjahr msegzeile mseg~xauto
msegparent_id msegwerks mseg~umwrk
msegumlgo msegparent_id ekko~reswk
msegwerks mseglgort mseg~matnr
msegerfmg msegerfme mseg~shkzg
msegsjahr msegsmbln mseg~smblp
msegcharg ekkobedat
INTO TABLE g_tbl_main
FROM mkpf JOIN mseg
ON mkpfmandt = msegmandt AND
mkpfmblnr = msegmblnr AND
mkpfmjahr = msegmjahr
JOIN ekko ON msegmandt = ekkomandt AND
msegebeln = ekkoebeln
JOIN likp ON mkpfxblnr = likpvbeln
CLIENT specified
WHERE mkpf~mandt = sy-mandt AND
mkpf~budat IN s_budat AND
ekko~bedat IN s_bedat AND
mseg~ebeln IN s_ebeln AND
mkpf~xblnr IN s_vbeln AND
likp~vstel IN s_vstel AND
ekko~reswk IN s_reswk AND
mseg~werks IN r_werks AND
mseg~lgort IN s_lgort AND
mseg~matnr IN s_matnr AND
mseg~smbln = space AND
mseg~bwart IN s_bwart AND
mseg~kzzug = c_true.Yes, avinash said rightly.
there is another way bit diffict.
use the indexes properly as available in the mseg table and mkpf table even then it is diffcult
else try using BAPI.
some bapi will give the entire details of the MATERAIL DOCUMENT
go to BAPI explorer tcode BAPI.
where you can find the BAPI name like the 'GET_LIST'.
Try this.
hope this will serve your purpose.
Thanks and regards
Ramchander Rao.K -
Confusion in Select query having a inner join on single table
Hi,
I was going through coding and came a accross a select query which has a inner join on a single table. I am getting confused while analysing this . Please someone can help me analysing this query.
select
m~MATERIAL
s~NUMERATOR s~DENOMINTR
m~GROSS_WT m~UNIT_OF_WT
into table itab
from ( table1as s
inner join table1 as m
on m~MATERIAL = s~MATERIAL
and m~MAT_UNIT = 'CS'
and m~SOURSYSTEM = s~SOURSYSTEM )
where s~MAT_UNIT = 'EA'
and s~SOURSYSTEM = 'LD'
and s~OBJVERS = 'A'
and s~MATERIAL IN ( Select mat_sales
from Table2 group by mat_sales ).
Thank you
KusumaI don't see any use of the INNER JOIN here.
But what's the meaning of the last selection clause?
s~MATERIAL IN ( Select mat_sales
from Table2 group by mat_sales ).
Is that native SQL or something?
Pushpraj -
Issue with select query join....
Hello,
I am facing a problem with a select query. I get a message that itab_data is not long enough.
DATA: itab_data like ptrv_head occurs 0 with header line.
SELECT * from PTRV_HEAD
as a inner join PA0002 as b
on a~pernr = b~pernr
inner join PA0001 as c
on a~pernr = c~pernr
INTO TABLE itab_data
where a~PERNR in S_PERNR
and a~REINR in S_TRIP
and a~ZLAND in S_LAND
and a~DATV1 in S_BEGIN
and a~DATB1 in S_END
and b~NACHN in S_FIRST
and b~VORNA in S_LAST
and c~BUKRS in S_BUKRS
and c~KOSTL in S_KOSTL
and c~PERSG in S_EMPGP
and c~PERSK in S_SUBGP
and c~begda in ldbbegda
and c~endda in ldbendda.
Regards,
Jainam.
Edited by: Jainam Shah on Mar 27, 2009 4:13 PM
Edited by: Jainam Shah on Mar 27, 2009 4:13 PMHi,
Try this..
DATA: t_dfies TYPE STANDARD TABLE OF dfies.
DATA: t_fields TYPE STANDARD TABLE OF char40.
DATA: s_dfies TYPE dfies,
s_fields TYPE char40.
* Get the fields
CALL FUNCTION 'DDIF_FIELDINFO_GET'
EXPORTING
tabname = 'PTRV_HEAD'
TABLES
dfies_tab = t_dfies[]
EXCEPTIONS
not_found = 1
internal_error = 2
OTHERS = 3.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
* Build the fields to be selected.
LOOP AT t_dfies INTO s_dfies.
CONCATENATE 'A~' s_dfies-fieldname INTO s_fields.
APPEND s_fields TO t_fields.
CLEAR: s_fields.
ENDLOOP.
* Select.
SELECT (t_fields) from PTRV_HEAD
Thanks
Naren -
Multiple left outer join in single select query
My client requires to apply multiple left outer join in single select query.
Tell me how it is possible . tell me if we can use native sql query.HI vineet,
Using multile left join is possible but it reduce performance as it takes a long time to retrieve data.
The following code would help you in knowing how to use multiple left join
REPORT test.
PARAMETERS:
p_auart LIKE vbak-auart.
DATA:
BEGIN OF fs_sales,
auart LIKE vbak-auart,
vbeln LIKE vbak-vbeln,
posnr LIKE vbap-posnr,
matnr LIKE vbap-matnr,
netwr LIKE vbap-netwr,
END OF fs_sales,
fs_temp LIKE fs_sales.
DATA:
t_sales LIKE STANDARD TABLE OF fs_sales.
SELECT k~auart
k~vbeln
p~posnr
p~matnr
p~netwr
INTO TABLE t_sales
FROM vbak AS k
OUTER JOIN vbap AS p
ON k~vbeln = p~vbeln
WHERE auart = p_auart
AND p~netwr LT 1000.
IF sy-subrc EQ 0.
WRITE:/5 'Sales Document'(001),/,
15 'Sales Document Item'(002),
40 'Material Number'(003).
sort t_sales by netwr ascending.
LOOP AT t_sales INTO fs_sales.
WRITE:/ fs_sales-vbeln under text-001.
LOOP AT t_sales INTO fs_temp WHERE vbeln = fs_sales-vbeln.
WRITE:/ fs_temp-posnr under text-002,
fs_temp-matnr under text-003.
ENDLOOP.
ENDLOOP.
ELSE.
MESSAGE 'No Record Found' TYPE 'I'.
ENDIF.
But i would suggest you to go with views instead of using multiple joins..
Best of luck,
Bhumika
Maybe you are looking for
-
I have purple lines running through keyboard and picture are distorted
I have purple lines run through the keys on typing pad when I press them as well as distorted pictures . Is this a software or hardware problem?
-
Turning off 'Sleep' in Restoring mode
I'm recovering from a bad crash. I'm in the process of restroing my computer from a time machine backup on a netword drive, but the screen dims and the computer goes to sleep every half and hour or so. The time remaining is 51 hours, 48 minutes. I
-
Hi. I know theres an extremely hard chance to get a virus on a Mac but is there anything on a Mac for disk cleanups or wiping out "cache"/ temporary internet files for spyware? I am a new mac user so please help. thanks for your time
-
Need Clarification On Internal tables in Start Routine
Hi, I have intenal table some IT_A which deletes the requests which are populated in to it. Now I need to populate requests into IT_A from another internal table like IT_B which are of different structure. I have three fields in common in both inter
-
Opening Documents in Teaming 2.0 with IE
Hi, We have Teaming 2.0. While working in Firefox, opening a document with the Icon, it opens the Office Document as Read Only (which is good). Doing the same with IE, it opens the Document in Read Write. If the Users just click "Save" - the Document