Need help in Select Query
Hello,
I am using oracle 9i and windows XP:
Please help me to making a query to get output as mentioned below:
create table myt(col1 varchar2(10));
insert into myt values('TABLE1');
insert into myt values('TABLE2');
insert into myt values('TABLE3');
COMMIT;
create table table1(name varchar2(10),amount number);
create table table2(name varchar2(10),amount number);
create table table3(name varchar2(10),amount number);
insert into table1 values('James',1000);
insert into table1 values('David',1500);
insert into table1 values('James',2000);
insert into table1 values('David',1735);
insert into table2 values('Menon',500);
insert into table2 values('Martin',700);
COMMIT;
Required Output:
Table Name
Table1
James: 3000
David: 3235
Sum: 6235
Table2
Menon: 500
Martin: 700
Sum: 1200
-------Regards
In your solution i added some modifications and got the output:
declare
cr_lf varchar2(2) := chr(13) || chr(10);
the_cursor sys_refcursor;
the_sql varchar2(4000) := 'select * from ' || cr_lf || '(' || cr_lf;
a_table varchar2(10);
ln varchar2(10);
a_name varchar2(10);
an_amount number:=0;
tan_amt number:=0;
i number;
j varchar2(4000);
begin
for the_row in (select col1 from myt) loop
the_sql := the_sql || 'select ''' || the_row.col1 || ''',name,sum(amount) from ' || the_row.col1 || ' group by name ' || cr_lf;
the_sql := the_sql || 'union all' || cr_lf;
end loop;
the_sql := substr(the_sql,1,length(the_sql) - 11) || ')' || cr_lf;
i := 1;
j := instr(the_sql,cr_lf,i);
while i > 0 loop
exit when j = 0;
dbms_output.put_line(substr(the_sql,i,j - i));
i := j + 2;
j := instr(the_sql,cr_lf,i);
end loop;
dbms_output.put_line(substr(the_sql,i));
--dbms_output.put_line(the_sql);
open the_cursor for the_sql;
dbms_output.put_line('--------------------------------------------------------------------------');
dbms_output.put_line(rpad('TABLE',20) || ' ' || rpad('NAME',20) || ' ' || rpad('AMOUNT',20));
dbms_output.put_line('--------------------------------------------------------------------------');
loop
ln:= a_table;
fetch the_cursor into a_table,a_name,an_amount;
exit when the_cursor%notfound;
if ln<>a_table then
dbms_output.put_line('--------------------------------------------------------------------------');
dbms_output.put_line(rpad('SUM',40) || lpad(to_char(tan_amt),10));
dbms_output.put_line('--------------------------------------------------------------------------');
tan_amt:=0;
end if;
tan_amt:= tan_amt+an_amount;
dbms_output.put_line(rpad(a_table,20) || ' ' || rpad(a_name,20) || ' ' || rpad(an_amount,20));
end loop;
dbms_output.put_line('--------------------------------------------------------------------------');
--dbms_output.put_line(rpad('SUM',45) || to_char(tan_amt));
dbms_output.put_line(rpad('SUM',40) || lpad(to_char(tan_amt),10));
dbms_output.put_line('--------------------------------------------------------------------------');
close the_cursor;
end;
select * from
select 'TABLE1',name,sum(amount) from TABLE1 group by name
union all
select 'TABLE2',name,sum(amount) from TABLE2 group by name
union all
select 'TABLE3',name,sum(amount) from TABLE3 group by name
TABLE NAME AMOUNT
TABLE1 David 6470
TABLE1 James 6000
SUM 12470
TABLE2 Martin 1400
TABLE2 Menon 1000
SUM 2400
--------------------------------------------------------------------------So many thanks for providing solution to me.
Regards
Similar Messages
-
Hi,
I have a query which will produce me the below data:
DataTime SLA Met Time
20101212 12/13/2010 12:48:29 PM
20101211 12/12/2010 4:50:38 PM
20101210 12/11/2010 2:07:02 PM
20101209 12/11/2010 9:46:30 AM
20101208 12/10/2010 4:46:19 AM
Now with the above data I want to find "sla status". Below is the condition:
1. if datatime is 20101212 (yyyymmdd) and if sla is met on or before 20101212 6pm then it has to print sla status as "MET", by any chance if sla is met after 6pm then it has to print as "Not Met".
2. So we dont have any static data in table and we have around 20k rows in the table.
3. Datatime is a Number column.
Please help me on this. Thanksuser497267 wrote:
Hi,
I have a query which will produce me the below data:
DataTime SLA Met Time
20101212 12/13/2010 12:48:29 PM
20101211 12/12/2010 4:50:38 PM
20101210 12/11/2010 2:07:02 PM
20101209 12/11/2010 9:46:30 AM
20101208 12/10/2010 4:46:19 AM
Now with the above data I want to find "sla status". Below is the condition:
1. if datatime is 20101212 (yyyymmdd) and if sla is met on or before 20101212 6pm then it has to print sla status as "MET", by any chance if sla is met after 6pm then it has to print as "Not Met".
2. So we dont have any static data in table and we have around 20k rows in the table.
3. Datatime is a Number column.
Please help me on this. ThanksDatatime is a number column? {noformat}*shudders*{noformat} Why?! Why is it not just DATE format? What's the difference between 20100101 and 20091231? 1 day if we're talking dates and 8870 if we're talking numbers. By taking out the fact that you're talking about dates in that column, you restrict the information available to the optimizer, and you open yourself up to poorly performing SQL.
As it is, if you're looking to check the sla met time is before 6pm on the datatime, then you need to have a case statement, convert the datatime to a date (to_date() ) and then add 18 hours to that time and compare it with the sla_met_time. -
Need help with SQL Query with Inline View + Group by
Hello Gurus,
I would really appreciate your time and effort regarding this query. I have the following data set.
Reference_No---Check_Number---Check_Date--------Description-------------------------------Invoice_Number----------Invoice_Type---Paid_Amount-----Vendor_Number
1234567----------11223-------------- 7/5/2008----------paid for cleaning----------------------44345563------------------I-----------------*20.00*-------------19
1234567----------11223--------------7/5/2008-----------Adjustment for bad quality---------44345563------------------A-----------------10.00------------19
7654321----------11223--------------7/5/2008-----------Adjustment from last billing cycle-----23543556-------------------A--------------------50.00--------------19
4653456----------11223--------------7/5/2008-----------paid for cleaning------------------------35654765--------------------I---------------------30.00-------------19
Please Ignore '----', added it for clarity
I am trying to write a query to aggregate paid_amount based on Reference_No, Check_Number, Payment_Date, Invoice_Number, Invoice_Type, Vendor_Number and display description with Invoice_type 'I' when there are multiple records with the same Reference_No, Check_Number, Payment_Date, Invoice_Number, Invoice_Type, Vendor_Number. When there are no multiple records I want to display the respective Description.
The query should return the following data set
Reference_No---Check_Number---Check_Date--------Description-------------------------------Invoice_Number----------Invoice_Type---Paid_Amount-----Vendor_Number
1234567----------11223-------------- 7/5/2008----------paid for cleaning----------------------44345563------------------I-----------------*10.00*------------19
7654321----------11223--------------7/5/2008-----------Adjustment from last billing cycle-----23543556-------------------A--------------------50.00--------------19
4653456----------11223--------------7/5/2008-----------paid for cleaning------------------------35654765-------------------I---------------------30.00--------------19
The following is my query. I am kind of lost.
select B.Description, A.sequence_id,A.check_date, A.check_number, A.invoice_number, A.amount, A.vendor_number
from (
select sequence_id,check_date, check_number, invoice_number, sum(paid_amount) amount, vendor_number
from INVOICE
group by sequence_id,check_date, check_number, invoice_number, vendor_number
) A, INVOICE B
where A.sequence_id = B.sequence_id
Thanks,
NickIt looks like it is a duplicate thread - correct me if i'm wrong in this case ->
Need help with SQL Query with Inline View + Group by
Regards.
Satyaki De. -
Need Help on below Query.
Hi All,
Need Help on below Query.
Consider,
"test9" Table Data in COLUMN "Name" AS
Name
=====
'a'
'b'
'c'
'd'
'e'
I am writing a query as :
SELECT * FROM test9 WHERE Name IN ('a','b','c','d','e','f','g')
I want result set as , It should show data as -
'f'
'g'
i.e. data which does not exists in the table and which is give in in clause
Is it possible in a single query.You can put the data that is to be checked for into a table instead or an inline view, for example:
with t as
(select 'a' as c1 from dual
union all
select 'b' from dual
union all
select 'c' from dual
union all
select 'd' from dual
union all
select 'e' from dual)
select c1 from (select 'a' as c1 from dual
union all
select 'b' from dual
union all
select 'c' from dual
union all
select 'd' from dual
union all
select 'e' from dual
union all
select 'f' from dual
union all
select 'g' from dual)
minus
select c1 from t
C
f
g
2 rows selected. -
Please, need help with a query
Hi !
Please need help with this query:
Needs to show (in cases of more than 1 loan offer) the latest create_date one time.
Meaning, In cases the USER_ID, LOAN_ID, CREATE_DATE are the same need to show only the latest, Thanks!!!
select distinct a.id,
create_date,
a.loanid,
a.rate,
a.pays,
a.gracetime,
a.emailtosend,
d.first_name,
d.last_name,
a.user_id
from CLAL_LOANCALC_DET a,
loan_Calculator b,
bv_user_profile c,
bv_mr_user_profile d
where b.loanid = a.loanid
and c.NET_USER_NO = a.resp_id
and d.user_id = c.user_id
and a.is_partner is null
and a.create_date between
TO_DATE('6/3/2008 01:00:00', 'DD/MM/YY HH24:MI:SS') and
TO_DATE('27/3/2008 23:59:00', 'DD/MM/YY HH24:MI:SS')
order by a.create_dateTake a look on the syntax :
max(...) keep (dense_rank last order by ...)
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions056.htm#i1000901
Nicolas. -
Please need help with this query
Hi !
Please need help with this query:
Needs to show (in cases of more than 1 loan offer) the latest create_date one time.
Meaning, In cases the USER_ID, LOAN_ID, CREATE_DATE are the same need to show only the latest, Thanks!!!
select distinct a.id,
create_date,
a.loanid,
a.rate,
a.pays,
a.gracetime,
a.emailtosend,
d.first_name,
d.last_name,
a.user_id
from CLAL_LOANCALC_DET a,
loan_Calculator b,
bv_user_profile c,
bv_mr_user_profile d
where b.loanid = a.loanid
and c.NET_USER_NO = a.resp_id
and d.user_id = c.user_id
and a.is_partner is null
and a.create_date between
TO_DATE('6/3/2008 01:00:00', 'DD/MM/YY HH24:MI:SS') and
TO_DATE('27/3/2008 23:59:00', 'DD/MM/YY HH24:MI:SS')
order by a.create_datePerhaps something like this...
select id, create_date, loanid, rate, pays, gracetime, emailtosend, first_name, last_name, user_id
from (
select distinct a.id,
create_date,
a.loanid,
a.rate,
a.pays,
a.gracetime,
a.emailtosend,
d.first_name,
d.last_name,
a.user_id,
max(create_date) over (partition by a.user_id, a.loadid) as max_create_date
from CLAL_LOANCALC_DET a,
loan_Calculator b,
bv_user_profile c,
bv_mr_user_profile d
where b.loanid = a.loanid
and c.NET_USER_NO = a.resp_id
and d.user_id = c.user_id
and a.is_partner is null
and a.create_date between
TO_DATE('6/3/2008 01:00:00', 'DD/MM/YY HH24:MI:SS') and
TO_DATE('27/3/2008 23:59:00', 'DD/MM/YY HH24:MI:SS')
where create_date = max_create_date
order by create_date -
Hi everyone...
I need help with this query. The table 'cobros' has a primary key defined by id_cliente + id_cobro. I pretend to classify rows by COUNT(id_cobro) which are between a date range.
A client could have 1 or 2 or 3 rows per day, no more for a specific date. I would like to get first, all clients with COUNT(id_cobro) = 1, all clients with COUNT(id_cobro) = 2, and finally all clients with COUNT(id_cobro) = 3.
Something similar to:
1 SELECT id_cliente, COUNT(id_cobro) FROM cobros
2 WHERE fecha_proximo_cobro >= '2011-05-30 00:00'
3 AND fecha_proximo_cobro <= '2011-05-30 23:59'
4 AND COUNT(id_cobro) = 1
5 GROUP BY id_cliente
The fourth line is the problem. It doesn't work.
Thanks in advance!!!
MarioMaybe you are looking for something like this?
SELECT id_cliente
, COUNT(*) AS cnt
FROM cobros
WHERE fecha_promixo_cobro BETWEEN TO_DATE('2011-05-30 00:00','YYYY-MM-DD HH24:MI') AND TO_DATE('2011-05-30 23:59','YYYY-MM-DD HH24:MI')
GROUP BY id_cliente
ORDER BY 2
, 1Also, NEVER rely on implicit data type conversion as you have (you provide a STRING not a DATE).
Edited by: Centinul on Jun 2, 2011 12:36 PM
Fixed syntax error. -
I need to add a single field from with_item table . need to write select query with reference to company code , account doc no , fiscal year
Hi Arun ,
Can you explain little bit more ??
what is account doc no?
what are the transactions should be displayed in your output??
-Rajesh N -
Need help with conditional query
guys this is just an extension of this post that Frank was helping me with. im reposting because my requirements have changes slightly and im having a hell of a time trying to modify the query.
here is the previous post.
need help with query that can look data back please help.
CREATE TABLE "FGL"
"FGL_GRNT_CODE" VARCHAR2(60),
"FGL_FUND_CODE" VARCHAR2(60),
"FGL_ACCT_CODE" VARCHAR2(60),
"FGL_ORGN_CODE" VARCHAR2(60),
"FGL_PROG_CODE" VARCHAR2(60),
"FGL_GRNT_YEAR" VARCHAR2(60),
"FGL_PERIOD" VARCHAR2(60),
"FGL_BUDGET" VARCHAR2(60)
)data
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7600','4730','02','11','00','400');
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','10','1','100');
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','10','1','0');
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7600','4730','02','11','1','400');
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('360055','360055','7200','4730','02','10','1','400');
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('360055','360055','7600','4730','02','10','1','400');
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','10','14','200');
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7600','4730','02','10','14','100');
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','10','14','200');
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','10','2','100');
Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','11','2','600');
I need to find the greatest grant year for the grant by a period parameter.
once i find the greatest year i need to check the value of period 14 for that grant for the previous year and add it to the budget amount for that grant. however if their is an entry in the greatest year for period 00 then i need to ignore the period 14 of previous year and do this calculation current period +(current period - greatest year 00)
hope that makes sense so in other words with the new data above. if i was querying period two of grant year 11. i would end up with $800
because the greatest year is 11 it contains a period 0 with amount of $400 so my total should be
period 2 amount $ 600
period 0 amount $ 400 - period 2 amount of $600 = 200
600+200 = $800
if i query period 1 of grant 360055 i would just end up with 800 of grnt year 10.
i have tried to modify that query you supplied to me with no luck. I have tried for several day but im embarrased to say i just can get it to do what im trying to do .
can you please help me out.
here is the query supplied by frank kulash who gracefully put this together for me.
WITH got_greatest_year AS
SELECT fgl.* -- or whatever columns are needed
, MAX ( CASE
WHEN fgl_period = :given_period
THEN fgl_grnt_year
END
) OVER () AS greatest_year
FROM fgl
SELECT SUM (fgl_budget) AS total_budget -- or SELECT *
FROM got_greatest_year
WHERE ( fgl_grnt_year = greatest_year
AND fgl_period = :given_period
OR ( fgl_grnt_year = greatest_year - 1
AND fgl_period = 14
;MiguelHi, Miguel,
Are you waying that, when the greatest year that has :given_period also has period='00' (or '0', or whatever you want to use), then you want to double the budget from the given_period (as well as subtract the budget from the '00', and not count the pevious year's '14')? If so, add another condition to the CASE statement which decides what you're SUMming:
WITH got_greatest_year AS
SELECT TO_NUMBER (fgl_grnt_year) AS grnt_year
, fgl_period
, TO_NUMBER (fgl_budget) AS budget
, MAX ( CASE
WHEN fgl_period = :given_period
THEN TO_NUMBER (fgl_grnt_year)
END
) OVER () AS greatest_year
FROM fgl
, got_cnt_00 AS
SELECT grnt_year
, fgl_period
, budget
, greatest_year
, COUNT ( CASE
WHEN grnt_year = greatest_year
AND fgl_period = '00'
THEN 1
END
) OVER () AS cnt_00
FROM got_greatest_year
SELECT SUM ( CASE
WHEN grnt_year = greatest_year -- New
AND fgl_period = :given_period -- New
AND cnt_00 > 0 THEN budget * 2 -- New
WHEN grnt_year = greatest_year
AND fgl_period = :given_period THEN budget
WHEN grnt_year = greatest_year
AND fgl_period = '00' THEN -budget
WHEN grnt_year = greatest_year - 1
AND fgl_period = '14'
AND cnt_00 = 0 THEN budget
END
) AS total_budget
FROM got_cnt_00
;You'll notice this is the same as the previous query I posted, except for 3 lines maked "New". -
Hi,
I need help with the following query. I want the balance (bal) with the latest exchange rate available.
Sample table & data
with
FX_RATE as
select 11 as id_date, 1 as id_curr, 47 as EXCH_rate from dual union
select 12, 1, 48 from dual union
select 13, 2, 54 from dual union
select 14, 2, 55 from dual union
select 15, 3, 56 from dual union
select 15, 2, 49 from dual),
TBL_NM as
select 13 as p_date, 2 as p_curr, 200 as bal from dual union
select 14, 2, 200 from dual union
select 15, 2, 200 from dual union
select 16, 2, 200 from dual union
select 17, 2, 200 from dual union
select 11, 5, 100 from dual
select p_date, p_curr, bal * nvl(exch_rate,1) from TBL_NM T LEFT OUTER JOIN FX_RATE F1 on (id_curr = p_curr and F1.id_date = T.p_Date)In the above query for p_date 16 & 17 and p_curr 2 it returns just balance multiplied by exchange rate 1"default". But i want the balance to have data as per latest exchange rate which is of exchange rate 15.
I tried this but returns error ORA-01799: a column may not be outer joined to a subquery ..
with
FX_RATE as
select 11 as id_date, 1 as id_curr, 47 as EXCH_rate from dual union
select 12, 1, 48 from dual union
select 13, 2, 54 from dual union
select 14, 2, 55 from dual union
select 15, 3, 56 from dual union
select 15, 2, 49 from dual),
TBL_NM as
select 13 as p_date, 2 as p_curr, 200 as bal from dual union
select 14, 2, 200 from dual union
select 15, 2, 200 from dual union
select 16, 2, 200 from dual union
select 17, 2, 200 from dual union
select 11, 5, 100 from dual
select p_date, p_curr, bal * nvl(exch_rate,1) from TBL_NM T LEFT OUTER JOIN FX_RATE F1
on (id_curr = p_curr and F1.id_date = (select max(F2.id_date) from FX_RATE F2 where F2.id_curr = T.p_curr and F2.id_Date <= T.p_date))Please advice on how i can achieve this ..The entire query wud be like this .. I've to incorporate in here
CREATE MATERIALIZED VIEW MV_DUMMY
BUILD IMMEDIATE
REFRESH FORCE ON DEMAND
AS
SELECT T.ID_TSACTION_RELEASED
BAL.ID_CONTRACT_BALANCE AS ID_CONTRACT_BALANCE,
T.N_REFERENCE_NUMBER
T.INSTRUMENT_N_REFERENCE
T.ITEM_NUMBER
T.EXTERNAL_SYSTEM_ID
T.SEQUENCE_NUMBER
T.ID_RELEASED_DATE
ROUND(BAL.LC_AVAILABLE_BALANCE * NVL(FX1.EXCHANGE_RATE,1) / NVL(FX2.EXCHANGE_RATE,1) , 4)
BAL.LIABILITY_BALANCE
BAL.LIABILITY_BALANCE * NVL(FX3.EXCHANGE_RATE,1)
BAL.LIABILITY_CHANGE_USD
BAL.MEMO_LIABILITY_BALANCE
BAL.MEMO_LIABILITY_BALANCE * NVL(FX3.EXCHANGE_RATE,1)
BAL.MEMO_LIABILITY_CHANGE_USD
BAL.ORIGINAL_FACE_AMOUNT
decode(T.TENOR_CODE,'Time','T','Sight','S','Split Sight Time','SST','Split Multiple Time','SMT',T.TENOR_CODE)
CASE
WHEN GTSPROD.PRODUCT_CATEGORY IN ('ILC','IB','AIR','STG','NLC','NP')
THEN T.ID_LIABILITY_CIF
WHEN GTSPROD.PRODUCT_CATEGORY IN ('ELC','EB','XLC','XP','EC','LN','SCF-AR')
THEN T.Id_Beneficiary
WHEN GTSPROD.PRODUCT_CATEGORY IN ('TLC','IC','OA','SCF-AP')
THEN T.ID_Applicant
END PRIMARY_CUSTOMER_ID,
CASE
WHEN GTSPROD.PRODUCT_CATEGORY IN ('ILC','IB','AIR','STG','NLC','NP')
THEN plbcif.EXTERNAL_SYSTEM_ID
WHEN GTSPROD.PRODUCT_CATEGORY IN ('ELC','EB','XLC','XP','EC','LN','SCF-AR')
THEN PBCIF.EXTERNAL_SYSTEM_ID
WHEN GTSPROD.PRODUCT_CATEGORY IN ('TLC','IC','OA','SCF-AP')
THEN pappcif.EXTERNAL_SYSTEM_ID
END PRIMARY_CUSTOMER_EXT_SYS_ID,
CASE
WHEN GTSPROD.PRODUCT_CATEGORY IN ('ILC','IB','AIR','STG','NLC','NP')
THEN plbcif.CIF_NAME
WHEN GTSPROD.PRODUCT_CATEGORY IN ('ELC','EB','XLC','XP','EC','LN','SCF-AR')
THEN PBCIF.CIF_NAME
WHEN GTSPROD.PRODUCT_CATEGORY IN ('TLC','IC','OA','SCF-AP')
THEN pappcif.CIF_NAME
END PRIMARY_CUSTOMER_NAME,
CASE
WHEN GTSPROD.PRODUCT_CATEGORY IN ('ILC','IB','AIR','STG','NLC','NP')
THEN plbbac.BAC
WHEN GTSPROD.PRODUCT_CATEGORY IN ('ELC','EB','XLC','XP','EC','LN','SCF-AR')
THEN pbbac.BAC
WHEN GTSPROD.PRODUCT_CATEGORY IN ('TLC','IC','OA','SCF-AP')
THEN pappbac.BAC
END PRIMARY_CUST_BAC_CODE,
CASE
WHEN GTSPROD.PRODUCT_CATEGORY IN ('ILC','IB','AIR','STG','NLC','NP')
THEN nvl(plbmg.MARKET,'NOT APPLICABLE')
WHEN GTSPROD.PRODUCT_CATEGORY IN ('ELC','EB','XLC','XP','EC','LN','SCF-AR')
THEN nvl(pbmg.MARKET,'NOT APPLICABLE')
WHEN GTSPROD.PRODUCT_CATEGORY IN ('TLC','IC','OA','SCF-AP')
THEN nvl(pappmg.MARKET,'NOT APPLICABLE')
END PRIMARY_CUST_MARKET,
CASE
WHEN GTSPROD.PRODUCT_CATEGORY IN ('ILC','IB','AIR','STG','NLC','NP')
THEN nvl(plbmg.SUB_MARKET,'NOT APPLICABLE')
WHEN GTSPROD.PRODUCT_CATEGORY IN ('ELC','EB','XLC','XP','EC','LN','SCF-AR')
THEN nvl(pbmg.SUB_MARKET,'NOT APPLICABLE')
WHEN GTSPROD.PRODUCT_CATEGORY IN ('TLC','IC','OA','SCF-AP')
THEN nvl(pappmg.SUB_MARKET,'NOT APPLICABLE')
END PRIMARY_CUST_SUB_MARKET
FROM F_TSACTION_RELEASED T
LEFT OUTER JOIN D_BAC_CODE BAC
ON (T.BAC_CODE_LIABILITY = BAC.BAC_CODE)
LEFT OUTER JOIN REF_BAC_SORT_CODE REF_BAC
ON (T.BAC_CODE_LIABILITY = REF_BAC.BAC)
LEFT OUTER JOIN F_CONTRACT_BALANCE BAL
ON (T.ID_TSACTION_RELEASED = BAL.ID_TSACTION_RELEASED)
LEFT OUTER JOIN D_MARKET_SEGMENT MG
ON (T.ID_MARKET_SEGMENT = MG.ID_MARKET_SEGMENT)
LEFT OUTER JOIN D_DATE DT
ON (DT.ID_DATE = T.ID_RELEASED_DATE)
LEFT OUTER JOIN D_DATE DB
ON (DB.ID_DATE = BAL.ID_RELEASED_DATE)
LEFT OUTER JOIN D_PROCESSING_UNIT PU
ON (PU.ID_PROCESSING_UNIT = T.ID_PROCESSING_UNIT)
LEFT OUTER JOIN D_BIR_PRODUCT BIRPROD
ON (BIRPROD.ID_BIR_PRODUCT=T.ID_BIR_PRODUCT)
LEFT OUTER JOIN D_GTS_PRODUCT_TYPE GTSPROD
ON (GTSPROD.ID_GTS_PRODUCT_TYPE= T.ID_GTS_PRODUCT_TYPE)
LEFT OUTER JOIN D_GTS_TSACTION_TYPE GTST
ON (GTST.ID_GTS_TSACTION_TYPE = T.ID_GTS_TSACTION_TYPE)
LEFT OUTER JOIN D_CURRENCY CCYT
ON (CCYT.ID_CURRENCY = T.ID_TSACTION_CURRENCY)
LEFT OUTER JOIN d_cif lcif
ON (lcif.id_cif = T.id_liability_cif)
LEFT OUTER JOIN d_cif lbcif
ON (lbcif.id_cif = bal.id_liability_cif)
LEFT OUTER JOIN d_cif bcif
ON (bcif.id_cif = T.id_BENEFICIARY)
LEFT OUTER JOIN d_cif icif
ON (icif.id_cif = T.id_ISSUING_BANK)
LEFT OUTER JOIN d_cif acif
ON (acif.id_cif = T.id_ADVISING_BANK)
LEFT OUTER JOIN d_cif appcif
ON (appcif.id_cif = T.id_applicant)
LEFT OUTER JOIN d_state astate
ON (astate.id_state = acif.id_state)
LEFT OUTER JOIN d_state bstate
ON (bstate.id_state = bcif.id_state)
LEFT OUTER JOIN d_state lstate
ON (lstate.id_state = lcif.id_state)
LEFT OUTER JOIN d_state lbstate
ON (lbstate.id_state = lbcif.id_state)
LEFT OUTER JOIN d_state istate
ON (istate.id_state = icif.id_state)
LEFT OUTER JOIN d_state appstate
ON (appstate.id_state = appcif.id_state)
LEFT OUTER JOIN D_TSACTION_SOURCE TSrc
ON (T.ID_TSACTION_SOURCE = TSrc.ID_TSACTION_SOURCE)
LEFT OUTER JOIN D_COUNTRY LCTRY
ON (LCTRY.ID_COUNTRY = lcif.ID_COUNTRY)
LEFT OUTER JOIN D_COUNTRY LBCTRY
ON (LBCTRY.ID_COUNTRY = lbcif.ID_COUNTRY)
LEFT OUTER JOIN D_COUNTRY BCTRY
ON (BCTRY.ID_COUNTRY = bcif.ID_COUNTRY)
LEFT OUTER JOIN D_COUNTRY ICTRY
ON (ICTRY.ID_COUNTRY = icif.ID_COUNTRY)
LEFT OUTER JOIN D_COUNTRY ACTRY
ON (ACTRY.ID_COUNTRY = acif.ID_COUNTRY)
LEFT OUTER JOIN D_COUNTRY APPCTRY
ON (APPCTRY.ID_COUNTRY = appcif.ID_COUNTRY)
LEFT OUTER JOIN D_COUNTRY PCTRY
ON (PCTRY.ID_COUNTRY = T.ID_PRESENTER_COUNTRY)
LEFT OUTER JOIN D_LOCATION LOC
ON (LOC.ID_LOCATION = T.ID_PROCESSING_LOCATION)
LEFT OUTER JOIN D_CURRENCY BCCYT
ON (BCCYT.ID_CURRENCY = BAL.ID_LIABILITY_CURRENCY)
LEFT OUTER JOIN D_CURRENCY BALCYT
ON (BALCYT.ID_CURRENCY = BAL.ID_BALANCE_CURRENCY)
LEFT OUTER JOIN d_liability_type li
ON (li.id_liability_type = BAL.id_liability_type)
LEFT OUTER JOIN d_cif plbcif
ON (plbcif.id_cif = T.id_liability_cif)
LEFT OUTER JOIN REF_BAC_SORT_CODE plbbac
ON (plbcif.bac_code=plbbac.bac)
LEFT OUTER JOIN D_MARKET_SEGMENT plbmg
ON (plbbac.SORT_CODE=plbmg.MARKET_SEGMENT)
LEFT OUTER JOIN d_cif pbcif
ON (pbcif.id_cif = T.id_BENEFICIARY)
LEFT OUTER JOIN REF_BAC_SORT_CODE pbbac
ON (pbcif.bac_code=pbbac.bac)
LEFT OUTER JOIN D_MARKET_SEGMENT pbmg
ON (pbbac.SORT_CODE=pbmg.MARKET_SEGMENT)
LEFT OUTER JOIN d_cif pappcif
ON (pappcif.id_cif = T.id_applicant)
LEFT OUTER JOIN REF_BAC_SORT_CODE pappbac
ON (pappcif.bac_code=pappbac.bac)
LEFT OUTER JOIN D_MARKET_SEGMENT pappmg
ON (pappbac.SORT_CODE=pappmg.MARKET_SEGMENT)
LEFT OUTER JOIN D_CURRENCY LOCALCYT
ON (LOCALCYT.alpha_code = PU.local_ccy)
LEFT OUTER JOIN D_BRANCH Branch
ON (T.ID_BRANCH = Branch.ID_BRANCH )
LEFT OUTER JOIN F_USD_FX_RATE_HISTORY FX1
ON (BAL.ID_BALANCE_CURRENCY = FX1.ID_CURRENCY and FX1.ID_DATE = (select max(FX11.ID_DATE) from F_USD_FX_RATE_HISTORY FX11 where BAL.ID_BALANCE_CURRENCY = FX11.ID_CURRENCY and FX11.ID_DATE <= BAL.id_released_date))
LEFT OUTER JOIN F_USD_FX_RATE_HISTORY FX2
ON (LOCALCYT.ID_CURRENCY = FX2.ID_CURRENCY and FX2.ID_DATE = (select max(FX22.ID_DATE) from F_USD_FX_RATE_HISTORY FX22 where LOCALCYT.ID_CURRENCY = FX22.ID_CURRENCY and FX22.ID_DATE <= BAL.id_released_date))
LEFT OUTER JOIN F_USD_FX_RATE_HISTORY FX3
ON (BAL.ID_LIABILITY_CURRENCY = FX3.ID_CURRENCY and FX3.ID_DATE = (select max(FX33.ID_DATE) from F_USD_FX_RATE_HISTORY FX33 where BAL.ID_LIABILITY_CURRENCY = FX33.ID_CURRENCY and FX33.ID_DATE <= BAL.id_released_date))Note the lines
ROUND(BAL.MN_AVAILABLE_BALANCE * NVL(FX1.EXCHANGE_RATE,1) / NVL(FX2.EXCHANGE_RATE,1) , 4)
BAL.LIABILITY_BALANCE * NVL(FX3.EXCHANGE_RATE,1)
BAL.MEMO_LIABILITY_BALANCE * NVL(FX3.EXCHANGE_RATE,1)
And
LEFT OUTER JOIN F_USD_FX_RATE_HISTORY FX1
ON (BAL.ID_BALANCE_CURRENCY = FX1.ID_CURRENCY and FX1.ID_DATE = (select max(FX11.ID_DATE) from F_USD_FX_RATE_HISTORY FX11 where BAL.ID_BALANCE_CURRENCY = FX11.ID_CURRENCY and FX11.ID_DATE <= BAL.id_released_date))
LEFT OUTER JOIN F_USD_FX_RATE_HISTORY FX2
ON (LOCAMNYT.ID_CURRENCY = FX2.ID_CURRENCY and FX2.ID_DATE = (select max(FX22.ID_DATE) from F_USD_FX_RATE_HISTORY FX22 where LOCAMNYT.ID_CURRENCY = FX22.ID_CURRENCY and FX22.ID_DATE <= BAL.id_released_date))
LEFT OUTER JOIN F_USD_FX_RATE_HISTORY FX3
ON (BAL.ID_LIABILITY_CURRENCY = FX3.ID_CURRENCY and FX3.ID_DATE = (select max(FX33.ID_DATE) from F_USD_FX_RATE_HISTORY FX33 where BAL.ID_LIABILITY_CURRENCY = FX33.ID_CURRENCY and FX33.ID_DATE <= BAL.id_released_date))Thsi is where I need to incorporate the change -
Dear all,
I have data in a table with 3 columns.
Tabke name---test & columns are ....
Proeductioncode varchar2(5);
revisionno varchar2(3);
dateadopted char(8);
SELECT PRODUCTIONCODE,
REVISIONNO,
dateadopted
FROM test
where
productioncode ='CI50E';
output should be
productioncode revisionno dateadopted
CI50E 004 20110125
CI50E 001 20101104
CI50E 003 20110320
CI50E 002 20101214
My requirement is
I wanna display records which are dateadopted is > sysdate and
one record which is less than sysdate, which is the max(<sysdate)..
from the data above, the output should be
CI50E 003 20110320
CI50E 004 20110125
Please help me get this output. we need to write it in a select query.Sorry I don't have a database available to test - missed to join by productioncode :( grouping by revisionno caused more than one row being returned :(
No need to send data. I added a data providing subquery. Will take shot when I get to the database, but I might not be able to post from the office
with
/* generating test data */
test as
(select 'CI50E' productioncode,'004' revisionno,'20110125' dateadopted from dual union all /* 003 if increasing by date */
select 'CI50E','001','20101104' from dual union all
select 'CI50E','003','20110320' from dual union all /* 004 if increasing by date */
select 'CI50E','002','20101214' from dual union all
select 'XI50Y','001','20110220' from dual union all
select 'XI50Y','002','20110220' from dual union all
select 'XI50Y','003','20110304' from dual
/* end of test data generation */
SELECT productioncode,revisionno,dateadopted
FROM (select productioncode,revisionno,dateadopted
from test a
where (dateadopted > to_char(sysdate,'yyyymmdd'))
or dateadopted = (select max(dateadopted)
from test
where productioncode = a.productioncode
and dateadopted < to_char(sysdate,'yyyymmdd')
ORDER BY PRODUCTIONCODERegards
Etbin
Edited by: Etbin on 20.2.2011 21:58
did some alignment
Edited by: Etbin on 21.2.2011 7:39
missing right parenthesis added, group by clause removed since there is just a single product
Edited by: Etbin on 21.2.2011 7:52
some more data edit -
I am trying to build a query that sums up 12 columns depending on some accounting variables.
example:
SELECT gbmcu, gbco, gbfy,'',SUM(gban01 gban02 gban03 gban04 gban05+gban06+gban07+gban08+gban09+gban10+gban11+gban12) AS Oil_Volumes
FROM PRODDTA.F0902
WHERE GBCO = '00099'
AND GBFY = 5
AND GBLT = 'QU' AND GBOBJ = 5015 AND GBSUB = '105'
GROUP BY gbmcu, gbco, gbfy
The condition that changes is :AND GBLT = 'QU' AND GBOBJ = 5015 AND GBSUB = '105'
I need to do this for 17 different conditions - I need to make the query as optimized as possible.
I tried using a case statement but that takes forever (the table is over 4 million records to scan through).
Please let me know if anyone has any suggestions on how to create something to perform these calculations.
thanks,
PamI think I would tend to write that query as:
SELECT gbmcu, gbco, gbfy,
SUM(CASE WHEN gblt = 'QA' AND gbobj = 5015 AND gbsub = '105' THEN
gban01+gban02+gban03+gban04+gban05+gban06+gban07+gban08+gban09+gban10+gban11+gban12 END) Oil_Sales
SUM(CASE WHEN gblt = 'QU' AND gbobj = 5015 AND gbsub = '105' THEN
gban01+gban02+gban03+gban04+gban05+gban06+gban07+gban08+gban09+gban10+gban11+gban12 END) Oil_Volumes
FROM proddta.f0902
WHERE gbco = '00099' and
gbfy = 5 and
gblt IN ('QA', 'QU') and
gbpbj = 5015 and
gbsub = '105'
GROUP BY gbmcu, gbco, gbfySUM the CASE rather than CASE SUM. Also, as written, your query will look at all of the records in f0902 whether or not they meet one of the Case criteria, so I would put the required values in the WHERE clause as well. Your samples only show gblt changing, so you may need to make the gbpbj and gbsub predicates IN lists as well.
If the query runs in 1.5 hours calculating one value, I would expect it to be about the same calculating 17 values, since I would bet that most of the run time is accessing the table rather than doing the math. It would almost certainly be faster than running essentially the same query 17 times.
Indexes on some or all of the columns in the WHERE clause may help, depending on how selective the columns are. At a guess, I would suggest that gbco and gbfy would be good candidates.
Finally, are you sure that gbsub is a character field? The only example we have is a number.
HTH
John -
Need help writing a query for following scenario
Hi all, I need some help writing a query for the following case:
One Table : My_Table
Row Count: App 5000
Columns of Interest: AA and BB
Scenario: AA contains some names of which BB contains the corresponding ID. Some
names are appearing more than once with different IDs. For example,
AA BB
Dummy 10
Me 20
Me 30
Me 40
You 70
Me 50
Output needed: I need to write a query that will display only all the repeating names with their corresponding IDs excluding all other records.
I would appreciate any input. ThanksIs it possible to have a records with the same values for AA and BB? Are you interested in these rows or do you only care about rows with the same value of AA and different BB?
With a slight modification of a previous posting you can only select those rows that have distinct values of BB for the same value of AA
WITH t AS (
SELECT 'me' aa, 10 bb FROM dual
UNION ALL
SELECT 'me' aa, 20 bb FROM dual
UNION ALL
SELECT 'you' aa, 30 bb FROM dual
UNION ALL
SELECT 'you' aa, 30 bb FROM dual
SELECT DISTINCT aa, bb
FROM (SELECT aa, bb, COUNT(DISTINCT bb) OVER(PARTITION BY aa) cnt FROM t)
WHERE cnt > 1; -
Hi All,
Please help me on the below join condition,
>SELECT * FROM ESTIMATE_LINE WHERE JOB_ID = '214582' AND ESTIMATE_ID = 2;
Output : Returns 1 row,
>SELECT * FROM ESTIMATE_ACCOUNTING_LINE WHERE JOB_ID = '214582' AND ESTIMATE_ID = 2;
Output : Returns 3 rows,
I would like to add these two outputs as on, Finally i've to get 4 rows in the output,
For that am writing the query like this,
SELECT EL.*,EAL.*
FROM ESTIMATE_LINE EL,
ESTIMATE_ACCOUNTING_LINE EAL
WHERE EL.JOB_ID = EAL.JOB_ID (+)
AND EL.ESTIMATE_ID = EAL.ESTIMATE_ID (+)
AND EAL.JOB_ID = '214582'
AND EL.ESTIMATE_ID = 2,
It is not giving the exact output, the common fields are there in the join condition,
Can some one please re write the query for expected output..
I am trying it but not getting the exact output,Unable to find out where the mistake is ...
Thanks,
graHi,
First, let me make sure I undersatnd the problem.
You have a query that lookas for information about a given job_id and estimate_id the estimate_line table. This query produces X rows.
You have another query that lookas for information about the same job_id and estimate_id the estimate_accounting__line table. This query produces Y rows.
You want to get the same results in one query that produces X+Y rows.
That sounds like a job for UNION, not a join.
If the two queries produces the same number of columns, and the data types of the columns are similiar, then you can just write the two queries together. Instead of a semicolon at the end of the first query, you'll say UNION (or UNION ALL):
SELECT * FROM ESTIMATE_LINE WHERE JOB_ID = '214582' AND ESTIMATE_ID = 2
UNION ALL
SELECT * FROM ESTIMATE_ACCOUNTING_LINE WHERE JOB_ID = '214582' AND ESTIMATE_ID = 2;If the columns are not the same, you'll need to call some conversion functions, or add literal columns (such as NULL) to the queries.
If you need help, post a little sampe data (CREATE TABLE and INSERT statements) and the results you want from that data. -
Hi
I need help to optimize the below query.... I takes to long to execute.
SELECT vbak~waerk
vbapvbeln vbapposnr
vbapnetwr vbapbrgew vbap~erdat
INTO CORRESPONDING FIELDS OF TABLE tmp_orders
FROM vbak INNER JOIN vbap ON vbakvbeln = vbapvbeln
WHERE vbap~matnr EQ t_mchb-matnr
AND vbap~netwr GT 0
AND vbap~brgew GT 1
AND vbak~auart IN ('ZCAO', 'ZDDO').
Please help...
Edited by: Alvaro Tejada Galindo on Mar 18, 2008 6:02 PMHi,
try this logic and see:
data: begin of itab_for_vbak occurs 0,
waerk type WAERK,
vbeln type vbeln,
end of itab_for_vbak,
wa_for_vbak like itab_for_vbak.
data: begin of itab_for_vbap occurs 0,
vbeln like vbap-VBELN,
posnr like vbap-POSNR,
netwr like vbap-NETWR,
brgew like vbap-brgew,
erdat like ERDAT
end of itab_for_vbap,
wa_for_vbap like itab_for_vbap.
SELECT
waerk
vbeln
from
vbak
into table itab_for_vbak
where
vbak~auart IN ('ZCAO', 'ZDDO').
select
vbeln
posnr
netwr
brgew
erdat
into table itab_for_vbap
where
vbap~matnr EQ t_mchb-matnr
AND vbap~netwr GT 0
AND vbap~brgew GT 1.
loop at itab_for_vbak into wa_for_vbak.
read table itab_for_vbap with key vbeln = wa_for_vbak-vbeln.
if sy-subrc eq 0.
tmp_orders-waerk = wa_for_vbak-waerk.
tmp_orders-vbeln = wa_for_vbap-vbeln.
tmp_orders-posnr = wa_for_vbap-posnr.
tmp_orders-brgew = wa_for_vbap-brgew.
tmp_orders-erdat = wa_for_vbap-erdat.
append tmp_orders.
endif.
endloop.
Please let me know how it has improved your performance or still if you have issues on this.
Thanks,
Vishnu.
Maybe you are looking for
-
I've got a mini running Leopard and a MBP running Snow Leopard. I'm trying to use screen sharing in finder to connect to the mini from the MBP. There seems to be something preventing screen sharing whenever a user is logged in to the mini, even if th
-
When I have finished working on an image in Photoshop and I hit the "save" button, it goes instead to the "save as" screen. This never happened in previous Photoshop versions. When I get to Lightroom, I have a grey screen the says "File could not be
-
Hello, How do I add a column to a Report after it has been created? I cannot find a way to do it readily in the "Report Column Attributes" as I thought it might be the more rational place to put such a time. Nor is there any information in the PDF Ma
-
Configure shared services when installing hyperion planning
i installed oracle10.2 DB and shared services on windows(only installed DB and Shared Services!). now use configuration tool to configure shared services(connect it to DB). user:hypuser password:XXX but the error: unable to connect to the database fo
-
Does anyone know how to get a disappeared photoshop icon back? It's nowhere, not in the application's folder, not on the desktop, etc. Thanks.