Help with a query.. distinct
Hi All,
i have this table and data:
CREATE TABLE TB_INV_KX
(FCH_FECHA_HORA timestamp,
ID_PRODUCTO VARCHAR2(10),
CTD_INV_FIN VARCHAR2(10)
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('16/08/12 15:39:56,000000000','DD/MM/RR HH24:MI:SS,FF'),'16','502');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('16/08/12 15:12:43,000000000','DD/MM/RR HH24:MI:SS,FF'),'16','854');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('16/08/12 10:31:25,000000000','DD/MM/RR HH24:MI:SS,FF'),'16','856');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('16/08/12 10:31:25,000000000','DD/MM/RR HH24:MI:SS,FF'),'17','463');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('16/08/12 10:31:25,000000000','DD/MM/RR HH24:MI:SS,FF'),'18','200');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('16/08/12 10:31:12,000000000','DD/MM/RR HH24:MI:SS,FF'),'16','847');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('16/08/12 10:31:12,000000000','DD/MM/RR HH24:MI:SS,FF'),'17','445');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('16/08/12 10:31:12,000000000','DD/MM/RR HH24:MI:SS,FF'),'18','209');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('16/08/12 10:30:47,000000000','DD/MM/RR HH24:MI:SS,FF'),'16','847');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('16/08/12 10:30:47,000000000','DD/MM/RR HH24:MI:SS,FF'),'16','856');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('16/08/12 10:30:47,000000000','DD/MM/RR HH24:MI:SS,FF'),'17','445');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('16/08/12 10:30:47,000000000','DD/MM/RR HH24:MI:SS,FF'),'17','463');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('16/08/12 10:30:47,000000000','DD/MM/RR HH24:MI:SS,FF'),'18','191');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('16/08/12 10:30:47,000000000','DD/MM/RR HH24:MI:SS,FF'),'18','200');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('16/08/12 10:30:47,000000000','DD/MM/RR HH24:MI:SS,FF'),'25','500');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('16/08/12 10:28:45,000000000','DD/MM/RR HH24:MI:SS,FF'),'16','839');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('16/08/12 10:28:45,000000000','DD/MM/RR HH24:MI:SS,FF'),'16','848');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('16/08/12 10:28:45,000000000','DD/MM/RR HH24:MI:SS,FF'),'17','429');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('16/08/12 10:28:45,000000000','DD/MM/RR HH24:MI:SS,FF'),'17','447');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('16/08/12 10:28:45,000000000','DD/MM/RR HH24:MI:SS,FF'),'18','199');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('16/08/12 10:28:45,000000000','DD/MM/RR HH24:MI:SS,FF'),'18','208');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('16/08/12 10:28:45,000000000','DD/MM/RR HH24:MI:SS,FF'),'25','509');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('15/08/12 10:52:34,000000000','DD/MM/RR HH24:MI:SS,FF'),'16','80');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('15/08/12 10:52:34,000000000','DD/MM/RR HH24:MI:SS,FF'),'17','70');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('15/08/12 10:52:34,000000000','DD/MM/RR HH24:MI:SS,FF'),'18','100');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('15/08/12 10:52:34,000000000','DD/MM/RR HH24:MI:SS,FF'),'19','100');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('15/08/12 10:35:55,000000000','DD/MM/RR HH24:MI:SS,FF'),'23','100');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('15/08/12 10:35:04,000000000','DD/MM/RR HH24:MI:SS,FF'),'23','100');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('15/08/12 10:25:25,000000000','DD/MM/RR HH24:MI:SS,FF'),'18','100');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('15/08/12 10:24:56,000000000','DD/MM/RR HH24:MI:SS,FF'),'19','100');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('10/08/12 15:14:53,902000000','DD/MM/RR HH24:MI:SS,FF'),'2','667');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('10/08/12 15:14:53,902000000','DD/MM/RR HH24:MI:SS,FF'),'3','98');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('10/08/12 15:14:53,902000000','DD/MM/RR HH24:MI:SS,FF'),'3','100');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('10/08/12 15:14:53,902000000','DD/MM/RR HH24:MI:SS,FF'),'3','789');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('10/08/12 15:14:53,902000000','DD/MM/RR HH24:MI:SS,FF'),'4','150');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('10/08/12 15:14:53,902000000','DD/MM/RR HH24:MI:SS,FF'),'4','455');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('10/08/12 15:14:53,902000000','DD/MM/RR HH24:MI:SS,FF'),'4','567');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('10/08/12 15:14:53,902000000','DD/MM/RR HH24:MI:SS,FF'),'4','789');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('10/08/12 15:14:53,902000000','DD/MM/RR HH24:MI:SS,FF'),'5','87');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('10/08/12 15:14:53,902000000','DD/MM/RR HH24:MI:SS,FF'),'5','333');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('10/08/12 15:14:53,902000000','DD/MM/RR HH24:MI:SS,FF'),'5','879');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('10/08/12 15:14:53,902000000','DD/MM/RR HH24:MI:SS,FF'),'6','100');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('10/08/12 15:14:53,902000000','DD/MM/RR HH24:MI:SS,FF'),'7','567');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('10/08/12 15:14:53,902000000','DD/MM/RR HH24:MI:SS,FF'),'7','789');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('10/08/12 15:14:53,902000000','DD/MM/RR HH24:MI:SS,FF'),'8','987');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('10/08/12 15:14:53,902000000','DD/MM/RR HH24:MI:SS,FF'),'9','77');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('10/08/12 15:14:53,902000000','DD/MM/RR HH24:MI:SS,FF'),'9','333');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('10/08/12 15:14:53,902000000','DD/MM/RR HH24:MI:SS,FF'),'18','6');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('10/08/12 15:14:53,902000000','DD/MM/RR HH24:MI:SS,FF'),'18','546');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('10/08/12 15:14:53,902000000','DD/MM/RR HH24:MI:SS,FF'),'19','34');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('10/08/12 15:14:53,902000000','DD/MM/RR HH24:MI:SS,FF'),'20','546');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('10/08/12 15:14:53,902000000','DD/MM/RR HH24:MI:SS,FF'),'20','567');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('10/08/12 15:14:53,902000000','DD/MM/RR HH24:MI:SS,FF'),'21','34');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('10/08/12 15:14:53,902000000','DD/MM/RR HH24:MI:SS,FF'),'21','150');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('10/08/12 15:14:53,902000000','DD/MM/RR HH24:MI:SS,FF'),'21','200');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('10/08/12 15:14:53,902000000','DD/MM/RR HH24:MI:SS,FF'),'22','150');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('10/08/12 15:14:53,902000000','DD/MM/RR HH24:MI:SS,FF'),'22','546');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('10/08/12 15:14:53,902000000','DD/MM/RR HH24:MI:SS,FF'),'23','78');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('10/08/12 15:14:53,902000000','DD/MM/RR HH24:MI:SS,FF'),'29','67');
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('10/08/12 15:14:53,902000000','DD/MM/RR HH24:MI:SS,FF'),'29','78');when i do:
SELECT DISTINCT ID_PRODUCTO,CTD_INV_FIN,FCH_FECHA_HORA FROM TB_INV_KX WHERE CTD_INV_FIN >=0 ORDER BY FCH_FECHA_HORA DESC;--the example result is
16 502 16/08/12 15:39:56,000000000
16 854 16/08/12 15:12:43,000000000
18 200 16/08/12 10:31:25,000000000
17 463 16/08/12 10:31:25,000000000
16 856 16/08/12 10:31:25,000000000
18 209 16/08/12 10:31:12,000000000
17 445 16/08/12 10:31:12,000000000
16 847 16/08/12 10:31:12,000000000
18 191 16/08/12 10:30:47,000000000
18 200 16/08/12 10:30:47,000000000
17 445 16/08/12 10:30:47,000000000
17 463 16/08/12 10:30:47,000000000
25 500 16/08/12 10:30:47,000000000
16 847 16/08/12 10:30:47,000000000
the the expect is:
16 502 16/08/12 15:39:56,000000000
18 200 16/08/12 10:31:25,000000000
17 463 16/08/12 10:31:25,000000000
25 500 16/08/12 10:30:47,000000000
Hi,
xDeviates wrote:
Hi All,
i have this table and data:
CREATE TABLE TB_INV_KX
(FCH_FECHA_HORA timestamp,
ID_PRODUCTO VARCHAR2(10),
CTD_INV_FIN VARCHAR2(10)
Insert into TB_INV_KX (FCH_FECHA_HORA,ID_PRODUCTO,CTD_INV_FIN) values (to_timestamp('16/08/12 15:39:56,000000000','DD/MM/RR HH24:MI:SS,FF'),'16','502');
Thanks for posting the CREATE TABLE and INSERT statements. Don't forget to post the explanation of why you want the results you post from that sample data. You already know the reasons, so it should be easy; nobody else knwos the reasons, so they have to guess (if they feel like guessing), and often guess wrong.
when i do:
SELECT DISTINCT ID_PRODUCTO,CTD_INV_FIN,FCH_FECHA_HORA FROM TB_INV_KX WHERE CTD_INV_FIN >=0 ORDER BY FCH_FECHA_HORA DESC;--the example result is
16 502 16/08/12 15:39:56,000000000
16 854 16/08/12 15:12:43,000000000
18 200 16/08/12 10:31:25,000000000
17 463 16/08/12 10:31:25,000000000
16 856 16/08/12 10:31:25,000000000
18 209 16/08/12 10:31:12,000000000
17 445 16/08/12 10:31:12,000000000
16 847 16/08/12 10:31:12,000000000
18 191 16/08/12 10:30:47,000000000
18 200 16/08/12 10:30:47,000000000
17 445 16/08/12 10:30:47,000000000
17 463 16/08/12 10:30:47,000000000
25 500 16/08/12 10:30:47,000000000
16 847 16/08/12 10:30:47,000000000Not when I run it: I get 60 rows of output, not just the 16 above.
the the expect is:
16 502 16/08/12 15:39:56,000000000
18 200 16/08/12 10:31:25,000000000
17 463 16/08/12 10:31:25,000000000
25 500 16/08/12 10:30:47,000000000Why just those 4 rows? Why don't you want, to give just a couple of examples:
ID_PRODUCT LAST_CTD_I FCH_FECHA_HORA
3 100 10-AUG-12 03.14.53.902000 PM
29 67 10-AUG-12 03.14.53.902000 PM? This is another reason why you need to post an explanation.
Maybe you're looking for something like this:
SELECT id_producto
, MIN (ctd_inv_fin) KEEP (DENSE_RANK LAST ORDER BY fch_fecha_hora)
AS last_ctd_inv_fin
, MAX (fch_fecha_hora) AS fch_fecha_hora
FROM tb_inv_kx
WHERE ctd_inv_fin >= 0
GROUP BY id_producto
ORDER BY id_producto
;You'll notice this is similar to what Solomon posted earlier, but it includes the WHERE clause from the query you posted. Solomon showed how to get the latest fch_fecha_hora for each producto_id. I used the analytic LAST fucntion to get the ctd_inv_fin that is related to the latest fch_fecha_hora for each id_producto.
Similar Messages
-
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 -
I need help with SQL query (if it can be accomplished with query at all).
I'm going to create a table with structure similar to:
Article_Name varchar2(30), Author_Name varchar2(30), Position varchar2(2). Position field is basicly position of an article author in the author list, e.g. if there is one author, his/her position is 0, if 2, then 1st author is 0, second is 1, etc.
Article_Name Author_Name Position
Outer Space Smith 0
Outer Space Blake 1
How can I automate creation of Position, based on number of authors on the fly? Let's say I have original table without Position, but I want to create a new table that will have this information.
RegardsIf you have an existing table whose structure doesn't tell you what position the author is in, what's the algorithm you'd use to determine who was the first author, the second author, etc? If you issue a select query on a table without providing an "order by" clause, Oracle makes no guarantees about the order in which it retrieves rows.
As an aside, why would you store position number in a varchar2 field? If it's a number, it ought to be stored as a number.
Justin -
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 urgent help with the query - Beginer
Hello - I need help with a query to populate data in a table.Here is the scenario.
Source1
MnthID BranchCod CustID SegCode FXStatus ProfStatus Profit
200712 B1 C1 20 Y Y 100
Source2
MnthID BranchCod CustID ProdCode ProdIndex
200712 B1 C1 12 1
200712 B1 C2 12 0
Destination
MnthID BranchCod SegCode ProdCode CountSegCust CountProdCust ProfitProdCust
Condition and Calculations:
1)Source1 customer are base customers.If Source2 has customers who is not in source1 then that customer's record should not be fetched.
2)SegCode, FX Status, ProfStatus is one variable in destination table. [ SegCode = SegCode+ FXStatus (if FXStatus = Y)+ ProfStatus (if FXStatus = Y) ]
3)CountSegCust = CountCustID Groupby MnthID,BranchCod,SegCode Only.
4)CountProdCust = CountCustID Groupby MnthID,BranchCod,SegCode,ProdCode (when ProdIndex = 1)
5)ProfitProdCust = Sum of Profit of Customers Groupby MnthID,BranchCod,SegCode,ProdCode (when ProdIndex = 1)
Apologies for bad formatting.
Thanks in advance!!A total guess indeed.
It's not clear whether some aggregation can be done (summing counts of grouped data might cause some customers being counted more than once)
insert into destination
select mnthid,branchcod,segcode,prodcode,countsegcust,countprodcust,profitprodcust
from (select s1.mnthid,
s1.branchcod,
s1.segcode || case s1.fxstatus when 'Y' then s1.fxstatus || s1.profstatus end segcode,
s2.prodcode,
count(s1.custid) over (partition by s1.mnthid,
s1.branchcod,
s1.segcode || case s1.fxstatus when 'Y' then s1.fxstatus || s1.profstatus end
order by null
) countsegcust,
count(case proindex when 1
then custid
end
) over (partition by s1.mnthid,
s1.branchcod,
s1.segcode || case s1.fxstatus when 'Y' then s1.fxstatus || s1.profstatus end
s2.prodcode
order by null
) countprodcust,
sum(case proindex when 1
then profit
end
) over (partition by s1.mnthid,
s1.branchcod,
s1.segcode || case s1.fxstatus when 'Y' then s1.fxstatus || s1.profstatus end
s2.prodcode
order by null
) profitprodcust,
row_number() over (partition by s1.mnthid,
s1.branchcod,
s1.segcode || case s1.fxstatus when 'Y' then s1.fxstatus || s1.profstatus end
s2.prodcode
order by null
) the_row
from source1 s1,source2 s2
where s1.mnthid = s2.mnthid
and s1.branchcod = s2.branchcod
and s1.custid = s2.custid
where the_row = 1Regards
Etbin -
I created a blogging tool for my students to use as I teach
them internet safety and cyber citizenship. I am no CF master, but
I dabble a little bit here and there. I need some help with this
query. It is running extremely slow, which means I have probably
created some horrendous loop in this query. If any one out there
has a better solution for this query, I and my middle school
students would be extremely grateful.
Here's what I would like it to do. I have two tables, one for
the blog messages and another for comments. The comments are linked
to their respective blog messages through a common database field.
When someone clicks on a link to read a student's blog, a query
runs which pulls all of the blog messages for that user, the
comments, and it also counts the number of comments entries for
each message so that I can place a total # of comments under each
blog message.Not sure why you have this like this: (Select
count(commentid) from comments where comments.blogid = blog.blogid)
or this twice: blog.blogusersid = #fname#
You need to make sure that the comments.blogid and
blog.blogid fields are indexed. Does this query work any faster?
<cfquery Name="Myblog" datasource="blog">
SELECT b.blogid, b.btitle, b.bcontent, b.bdate,
b.blogusersid, b.fname, b.lname, b.blogpict, b.pictlocation,
b.userid, c.commentid, c.blogid, b.lastupdated, COUNT(c.commentid)
AS cc
FROM blog AS b
INNER JOIN comments AS c ON c.blogid = b.blogid
WHERE b.blogusersid = #fname#
GROUP BY b.blogid, b.btitle, b.bcontent, b.bdate,
b.blogusersid, b.fname, b.lname, b.blogpict, b.pictlocation,
b.userid, c.commentid, c.blogid, b.lastupdated
ORDER BY b.bdate
</cfquery>
..... but I'm not sure that you will be getting the comment
count that you want with either query.
Phil -
Hi guys can someone help with a query regarding the 'podcast app' why do they not have all the episodes that relate to one show available why only half or a selected amount
THanks...but some days they have all the episodes right back to the very first show...ive downloaded a few but they are only available every now and then which makes no sense...why not have them available the whole time ??
-
Can any one help with this query please
I have a table something as below
Things_t
Things Characteristic Char Value
Item 1 Colour Red
Item 1 Packaging
Item 2 Shape Square
Item 2 Brand Spunk
Now i want to reterive an item with none of its char values as Null. Using the query “ select distinct things from things_t where char value is Null ” will fetch the item 1 also together with item 2. i want to fetch a record from thing for which none of the char values are Null such as Item 2. Can you please help me with this query.Try this:
WITH t AS
(SELECT 1 item_id, 17436 chr_id, 14225034 chr_val_id FROM dual UNION
SELECT 1 item_id, 39 chr_id, 14276173 chr_val_id FROM dual UNION
SELECT 1 item_id, 17774 chr_id, NULL chr_val_id FROM dual UNION
SELECT 1 item_id, 265 chr_id, 20502978 chr_val_id FROM dual UNION
SELECT 1 item_id, 16978 chr_id, 797233 chr_val_id FROM dual UNION
SELECT 1 item_id, 13092 chr_id, 5666917 chr_val_id FROM dual UNION
SELECT 1 item_id, 15228 chr_id, 1209758 chr_val_id FROM dual UNION
SELECT 2 item_id, 112 chr_id, 12705342 chr_val_id FROM dual UNION
SELECT 2 item_id, 6945 chr_id, NULL chr_val_id FROM dual UNION
SELECT 2 item_id, 70 chr_id, 12597376 chr_val_id FROM dual UNION
SELECT 2 item_id, 16832 chr_id, NULL chr_val_id FROM dual UNION
SELECT 2 item_id, 7886 chr_id, 9588619 chr_val_id FROM dual UNION
SELECT 2 item_id, 6986 chr_id, 2659351 chr_val_id FROM dual UNION
SELECT 3 item_id, 9531 chr_id, 8910943 chr_val_id FROM dual UNION
SELECT 3 item_id, 9798 chr_id, 8717531 chr_val_id FROM dual UNION
SELECT 3 item_id, 17446 chr_id, 12266441 chr_val_id FROM dual UNION
SELECT 3 item_id, 4830 chr_id, 13683090 chr_val_id FROM dual UNION
SELECT 3 item_id, 9518 chr_id, 834772 chr_val_id FROM dual UNION
SELECT 3 item_id, 11031 chr_id, 20233753 chr_val_id FROM dual UNION
SELECT 3 item_id, 12564 chr_id, 2282478 chr_val_id FROM dual)
SELECT DISTINCT item_id
FROM t
MINUS
SELECT DISTINCT item_id
FROM t
WHERE chr_val_id IS NULLOr this:
SELECT item_id
FROM (SELECT item_id,
MIN(NVL(chr_val_id, -1)) min_chr_val_id
FROM t
GROUP BY item_id)
WHERE min_chr_val_id != -1Edited by: lee200 on Oct 15, 2012 9:22 AM -
Need some help with count query
Hello,
I'm terrible @ oracle, so bear with me. I am trying to use the count function with my query but it counts rows from different tables.
I want my query to basically add a column from another table but have it not be a part of the count.
Say, table1 table2 table3. My count is right between the first two tables (Buyers,5).But since in table3 there are duplicate values(or accounts in this case(3), the count multiples by that many (15).
I need it to read Buyers,5,account #. I've tried distinct and a union but to no avail.
Help me please.If I understand you correctly, you want to count the records in table1 and table2 which have a one-to-one relationship, but you need to display additional data from table3 which has a one-to-many relationship. If this is correct, you need to use an in-line view to join table1 and table2 then join that in-line view to table3 to display the data from it. Something along the lines of:
SELECT v.col1, v.col2, v.col3, v.col4, v.cnt, t3.col1 t3col1, t3.col2 t3col2
FROM (SELECT t1.col1, t1.col2, t2.col1 col3, t2.col2 col4, COUNT(*) cnt
FROM table1 t1, table2 t2
WHERE <join conditions between t1 and t2> and
<other conditions if required>
GROUP BY t1.col1, t1.col2, t2.col1, t2.col2) v,
table3 t3
WHERE <join conditions between v and t3>
<other conditions if required>John -
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". -
select * from testme;
NAME VAL
A 1
A 2
A 3
B 1
B 2
B 3
C 1
C 2
I want to get the maximum of name and the maximum of corresponding name's value.
ie., the answer should be
Name VAL
C 2
Please help me with the query.
ThnxSQL> create table testme
2 as
3 select 'A' name, 1 val from dual union all
4 select 'A', 2 from dual union all
5 select 'A', 3 from dual union all
6 select 'B', 1 from dual union all
7 select 'B', 2 from dual union all
8 select 'B', 3 from dual union all
9 select 'C', 1 from dual union all
10 select 'C', 2 from dual
11 /
Tabel is aangemaakt.
SQL> select max(name) name
2 , max(val) keep (dense_rank last order by name) val
3 from testme
4 /
NAME VAL
C 2Regards,
Rob. -
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 -
Hello all,
Total newbie to this pl/sql stuff. So, deseperately need help in my query.
BOOKING_ID BOOKING_STATUS BOOKING_DATE BOOKING_TIME BOOKING_DATE_TIME
1234567 CANCELLED 20090301 37252 5/1/2010 10:20
1234567 CANCELLED 20090301 44229 5/1/2010 12:17
1234567 BOOKED 20090301 39462 5/1/2010 10:57
1234567 CANCELLED 20090301 43549 5/1/2010 12:05
9671111 BOOKED 20090301 68124 5/1/2010 12:57
9671111 CANCELLED 20090301 45001 5/1/2010 12:05
How do I write my query such that I would get the following results:
BOOKING_ID BOOKING_STATUS BOOKING_DATE BOOKING_TIME BOOKING_DATE_TIME
9671111 BOOKED 20090301 68124 2/4/2010 12:17
Basically, I am looking at the latest BOOKING_TIME and making sure the BOOKING_STATUS=BOOKED, if not, don't even bother bring back the result. Hence, you see that BOOKING_ID=1234567 is not required since at the latest BOOKING_TIME=44229, the BOOKING_STATUS=CANCELLED.
Any help is greatly appreciated.
Thank you in advance for your help.
Stanley HoHi, Stanley,
Welcome to the forum!
Whenever you have a question, please post your sample data in a form that people can actually use. CREATE TABLE and INSERT statements are perfect.
For example:
CREATE TABLE booking
( booking_id NUMBER (8)
, booking_status VARCHAR2 (10)
, booking_date_time DATE
INSERT INTO booking (booking_id, booking_status, booking_date_time)
VALUES (1234567, 'CANCELLED', TO_DATE ('5/1/2010 10:20', 'MM/DD/YYYY HH24:MI'));
INSERT INTO booking (booking_id, booking_status, booking_date_time)
VALUES (1234567, 'CANCELLED', TO_DATE ('5/1/2010 12:17', 'MM/DD/YYYY HH24:MI'));
INSERT INTO booking (booking_id, booking_status, booking_date_time)
VALUES (1234567, 'BOOKED', TO_DATE ('5/1/2010 10:57', 'MM/DD/YYYY HH24:MI'));
INSERT INTO booking (booking_id, booking_status, booking_date_time)
VALUES (1234567, 'CANCELLED', TO_DATE ('5/1/2010 12:05', 'MM/DD/YYYY HH24:MI'));
INSERT INTO booking (booking_id, booking_status, booking_date_time)
VALUES (9671111, 'BOOKED', TO_DATE ('5/1/2010 12:57', 'MM/DD/YYYY HH24:MI'));
INSERT INTO booking (booking_id, booking_status, booking_date_time)
VALUES (9671111, 'CANCELLED', TO_DATE ('5/1/2010 12:05', 'MM/DD/YYYY HH24:MI'));What you want is called a Top-N Query .
Here's one way to do it:
WITH got_rnum AS
SELECT booking.*
, ROW_NUMBER () OVER ( PARTITION BY booking_id
ORDER BY booking_date_time DESC
) AS rnum
FROM booking
SELECT booking_id
, booking_status
, TO_CHAR (booking_date_time, 'YYYYMMDD') AS booking_date
, TO_CHAR (booking_date_time, 'SSSSS') AS booking_time
, TO_CHAR (booking_date_time, 'MM/DD/YYYY HH24:MI') AS booking_date_time
FROM got_rnum
WHERE rnum = 1
AND booking_status = 'BOOKED'
;Notice that you don't need PL/SQL to do this; plain old SQL is good enough.
Of course, if you're using PL/SQL for other reasons, you can use a query like this within PL/SQL.
Dates (including time of day) should always be stored in DATE columns.
If you have a DATE column, like booking_date_time, then there's no need for redundant date and time columns.
You can always display just the year-month-day, or just the time, in any format, as I did above.
The output from the query above, with the data above, is:
BOOKING_ID BOOKING_ST BOOKING_ BOOKI BOOKING_DATE_TIM
9671111 BOOKED 20100501 46620 05/01/2010 12:57I realize the booking_date and booking_time columns aren't quite what you posted. If they are not derivable from booking_date_time, then you probably do need separate columns for them, and those columns can easily be added to the query above.
Edited by: Frank Kulash on Feb 5, 2010 4:41 PM
KEEP (DENSE_RANK ...) , like Max used below, is a great tool to have in your kit. The problem with it is that you have to repeat a lot of stuff for every column, so the more columns you have in your output, the more tedious it gets. ROW_NUMBER sclaes much better, and is adaptable to more situations. I suggest you master ROW_NUMBER first, and look into KEEP (DENSE_RANK ...) later. -
PROCEDURE Mktg_Obj_Cmpgn(p_attr_tbl_nm IN attr.attr_tbl_nm%TYPE,p_actn_cd IN chg_log.actn_cd%TYPE) IS
v_tbl_nm varchar2(100);
v_actn_cd varchar2(4);
BEGIN
v_tbl_nm := p_attr_tbl_nm;
v_actn_cd := p_actn_cd;
--dbms_output.put_line(v_tbl_nm);
IF v_actn_cd = 'CRET' THEN
SELECT to_clob(XMLELEMENT("requestmessage",XMLATTRIBUTES(xmlforest(
seq_pblsh_rqst.nextval AS "publish_id")),
XMLAGG(XMLELEMENT("campaign",XMLATTRIBUTES(XMLFOREST(chg.actn_cd AS "ACTION",
pgm.program_idseq AS "parententityvalue",
ent_sbsrb.ENT_ID AS "entid",
pgm.campaign_idseq AS "campaignid",
bus_unt.portfolio_subtype_id AS "campaignsegment",
pgm_typ.pgm_type_nm AS "campaigntype",
LKP.lookup_key_desc AS "campaignclass",
pgm.editor_userid AS "campaignlastmodifiedby",
pgm.edited_dtm AS "campaignlastmodifieddate",
pgm.mkt_initv_proc_tx AS "campaignlink",
ofr.offer_idseq AS "offerlink")))),
XMLAGG(XMLELEMENT("PROCESSCODE", XMLATTRIBUTES(XMLFOREST(action as "link",PROGRAM.program_idseq AS "parententityvalue",
ENT_SBSRB_ATTR.ent_id AS "entid"))))))AS "v_clob" into v_clob1
FROM dual,
chg_log chg,
program pgm,
ent_sbsrb_attr ent_sbsrb,
business_unit bus_unt,
program_type pgm_typ,
offer ofr,
(SELECT lookup_keyword,lookup_key_desc,lookup_type
FROM lookup_key
WHERE lookup_type = 'PRTFL_CMPGN_ID')LKP
WHERE chg.attr_id = ent_sbsrb.attr_id
and chg.parnt_ent_val_tx = to_char(pgm.program_idseq)
and pgm.business_unit_idseq = bus_unt.business_unit_idseq
and pgm.pgm_type_cd = pgm_typ.pgm_type_cd
and ofr.program_idseq = pgm.program_idseq
and pgm.pgm_cls_cd(+) = LKP.lookup_keyword;
END IF;
end Mktg_Obj_Cmpgn;
I have this procedure within a package. v_clob1 is declared in packagebody as a CLOB variable. when i try to compile it i am getting the below given error.can anyone please help me?
(1): PL/SQL: ORA-19208: parameter 1 of function XMLELEMENT must be aliased
(2): PL/SQL: SQL Statement ignoredhi sir,
wanna ask u guyz one query
let say i have table name account(parent) and service(child)
account
id name status
1 john 0
2 ki 1
3 kdf 2
service
id trans_id name status
1 1 et 9999
1 2 eee 2222
2 3 ere 999
2 4 wew 0
i plan to use something like this
delete account from account t1,service t2 where t1id=t2.id and t1.status<>0;
but sql command not correct so plz help me
now i need a query to delete records from service table only but
account.status <> 0 and accound.id=service.id
i dunt want to delete record from account table
so can u help me with this query
thanks
Message was edited by:
mani_um -
I am new to XML parsing. I am trying to convert xml data into relational data and need some help with this.
with table1 AS
(select xmltype(
'<BuildingData_AllResponse xmlns="http://secure.rutgers.edu/spacemanagement/">
<BuildingData_AllResult>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Table">
<xs:complexType>
<xs:sequence>
<xs:element name="BldgNum" type="xs:int" minOccurs="0"/>
<xs:element name="BldgName" type="xs:string" minOccurs="0"/>
<xs:element name="Campus" type="xs:string" minOccurs="0"/>
<xs:element name="StreetNo" type="xs:string" minOccurs="0"/>
<xs:element name="Address" type="xs:string" minOccurs="0"/>
<xs:element name="City" type="xs:string" minOccurs="0"/>
<xs:element name="State" type="xs:string" minOccurs="0"/>
<xs:element name="Zip" type="xs:string" minOccurs="0"/>
<xs:element name="Lat" type="xs:double" minOccurs="0"/>
<xs:element name="Long" type="xs:double" minOccurs="0"/>
<xs:element name="BldgDesc" type="xs:string" minOccurs="0"/>
<xs:element name="DeptName" type="xs:string" minOccurs="0"/>
<xs:element name="OrgID" type="xs:int" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<NewDataSet xmlns="">
<Table diffgr:id="Table1" msdata:rowOrder="0">
<BldgNum>1113</BldgNum>
<BldgName>Trinity House</BldgName>
<Campus>College Avenue</Campus>
<StreetNo>14</StreetNo>
<Address>Stone Street</Address>
<City>New Brunswick</City>
<State>NJ</State>
<Zip>08901</Zip>
<Lat>0</Lat>
<Long>0</Long>
</Table>
<Table diffgr:id="Table2" msdata:rowOrder="1">
<BldgNum>1114</BldgNum>
<BldgName>Second Reformed Church</BldgName>
<Campus>College Avenue</Campus>
<StreetNo>100</StreetNo>
<Address>College Avenue</Address>
<City>New Brunswick</City>
<State>NJ</State>
<Zip>08901</Zip>
</Table>
<Table diffgr:id="Table3" msdata:rowOrder="2">
<BldgNum>3000</BldgNum>
<BldgName>Old Queens </BldgName>
<Campus>College Avenue</Campus>
<StreetNo>83</StreetNo>
<Address>SOMERSET STREET</Address>
<City>New Brunswick</City>
<State>NJ</State>
<Zip>08901-1281</Zip>
<Lat>40.4987594033</Lat>
<Long>-74.4462681014</Long>
<BldgDesc>Old Queens, originally called the Queens Building was erected in 1809. It was named after Charlotte Sophia the wife of George III, King of England. Charlotte Sophia was the youngest daughter of Charles Lewis, brother of Frederic, third Duke of Mecklenburg- Strelitz. She married George III on Sept.8, 1761 and was crowned on Sept. 22. Queen Charlotte was buried in St. George's Chapel, Winsdor. Source: Catalogue of Building and Place Names at Rutgers.</BldgDesc>
<DeptName>Division of Continuing Studies, VP</DeptName>
<OrgID>10003</OrgID>
</Table>
</NewDataSet>
</diffgr:diffgram>
</BuildingData_AllResult>
</BuildingData_AllResponse>'
) ruloc from dual
SELECT loc.*
FROM table1 PO,
XMLTable('/BuildingData_AllResponse/BuildingData_AllResult/diffgr:diffgram/NewDataSet/Table/BldgNum' PASSING PO.ruloc
COLUMNS "BldgNum" CHAR(10) PATH 'BldgNum',
"Name" CHAR(150) PATH 'BldgName',
"Campus" CHAR(50) PATH 'Campus') AS loc
In the above example, for simplicity I am pulling only 3 columns, but I am looking to pull all elements within <TABLE> as columns values.Hi,
Please always give your database version (select * from v$version), and the error message (if any).
The XML document you're querying defines multiple namespaces.
You have to declare those useful to resolve the XPath expression with the XMLNamespaces clause :
SELECT loc.*
FROM table1 PO,
XMLTable(
XMLNamespaces(
'urn:schemas-microsoft-com:xml-diffgram-v1' as "diffgr"
, 'http://secure.rutgers.edu/spacemanagement/' as "ns0"
, '/ns0:BuildingData_AllResponse/ns0:BuildingData_AllResult/diffgr:diffgram/NewDataSet/Table'
PASSING PO.ruloc
COLUMNS
"BldgNum" CHAR(10) PATH 'BldgNum',
"Name" CHAR(150) PATH 'BldgName',
"Campus" CHAR(50) PATH 'Campus'
) AS loc
;
Maybe you are looking for
-
Mq3_6-plt-win.zip seems to be corrupt
Hi, I am trying to install the latest Sun Java System Message Queue 3 2005Q1 Platform Edition on Windows XP. The installation crashes with complaining about a missing JaxRPC.cab. A brief check confirms it does not exist in the zip file. Any help? Tha
-
Mac/iPod back to school rebate offer Proof of purchase issue
I brought a macbook and applied for the iPod rebate the other day. I'm now stuck with trying to provide evidence as the EAN label on my Macbook box won't come off, and the man at the apple store said that I would need the barcode if I had any trouble
-
Hi, I am creating a new environment on PT 8.50 for Portal 9.1 with DB as Oracle 11G R2 on Linux OS which is on domain xyz.com and i have an old environment of portal8.8 on domain abc.com.WE are trying to provide a link from Portal9.1 to Portal8.8 whi
-
Filling range using..... bdcdata
Hi , I have a requuirement that , need to call a standard report in a new session when i click on first level output.Am using ' FM ABAP4_CALL_TRANSACTION' STARTING NEW TASK to create anew session by filling bdcdata but problem is...i wanted to submi
-
Service Tax reg No. in Invoice Posting
Hello Gurus, When i post Invoice i am getting error message is enter service tax reg number for G/L account, Business Areaxxxx Please advice me how can i resolve the problem thanks in advance. gopi.