Help in the query below
HelloI have the following string
Select 1, 3, abc, SBS, 234, 1, 1 abc, 4' of the double
and I have translations for
ABC, SBS and abc 1 open as
X, Y and Z
which i as a result of a table as
1 X abc
2 dce Y
ABC 3 1 Z
now, I want to replace these tranlations in my original string such that my spaces etc. are intact (note that I can't use the function replace directly)
MY final string should look like
1, 3, X, Y, Z, 1, 4, 234
pointers how I can achieve this.
Thank you
Like this
with t
as
(
select '1, ,3, , , abc, ,dce,234, , , abc 1 ,1 ,4 ' str
from dual
), t1
as
(
select 1 rno, 'abc' a, 'X' b from dual union all
select 2, 'dce', 'Y' from dual union all
select 3, 'abc 1', 'Z' from dual
)
select ltrim(sys_connect_by_path(new_token,','), ',') str
from (
select t.rno, t.token, t1.b, decode(t1.b,null,t.token,t1.b) new_token
from (
select level rno,
regexp_substr(str,'[^,]+',1,level) token
from t
connect by level <= length(str)-length(replace(str,','))+1
) t
left join t1 on trim(t.token) = trim(t1.a)
)
where connect_by_isleaf = 1
start with rno = 1
connect by rno = prior rno + 1
Tags: Database
Similar Questions
-
Help with the query to select only one record from the result set in double
Hello
Please help with the query. Version of Oracle database we use is 10g R2.
I have a vision that is duplicated IDS, but they are used across the different functions. See below examples of data. Please help me with a query to select only one record (based on ID regardless of the area) from the bottom of the result set of duplicate records. For what is the point of view is there unique records, given the combination of the fields ID, Org, DF, dry, Sub-Sec
ID Org DF Sec Sub-Sec (163) CQ CPDM CPDM HD (163) PC ENG ENG ENG (163) CQ ASIC ASIC IS 8888 TS TAC TAC TAC (163) TS HE HE SW 6789 CQ INFO INFO FOS 6789 PC SECSYS SECSYS INFO1 6789 TS SECSYS SECSYS INFO2 9009 PC BMS BMS BMS1 My result set must eliminate the duplicate identifiers regardless of whoever we choose of the result set. (I mean without distinction Org, DF, s, Sub-s). My expected result set should be.
ID DSB DF SEC Sub-Sec (163) CQ CPDM CPDM HD 8888 TS TAC TAC TAC 6789 CQ INFO INFO FOS 9009 PC BMS BMS BMS1 Thank you
Orton
Hello
This sounds like a job for ROW_NUMBER:
WITH got_r_num AS
(
SELECT id, DSB, df, s, sub_sec org
ROW_NUMBER () OVER (PARTITION BY ID.
ORDER BY org
) AS r_num
OF view_x
)
SELECT id, DSB, df, sub_sec s,
OF got_r_num
WHERE r_num = 1
;
He is a Top - N query example, where you choose the elements of N (N = 1 in this case) from the top of an ordered list.
I hope that answers your question.
If not, post a small example of data (CREATE TABLE and INSERT, only relevant columns instructions) to your sample data and the results desired from these data. (I know that you said that you were a view selection. Just for this thread, pretending it is a picture and post simple CREATE TABLE and INSERT statements to simulate your point of view).
Point where the above query is to produce erroneous results, and explain, using specific examples, how you get the right results from data provided in these places. (I didn't quite understand the explanation above. I don't know why you want toID ORG DF DRY SUB_SEC
-- --- -- --- -------
1234 CQ DPRK DPRK HD
and is not
1234 IS CQ ASIC, ASIC
or
TS 1234 IT IT SW
or
1234 CQ ASIC ASIC HD
)
If you change the query at all, post your modified version.
Always say what version of Oracle you are using (for example, 11.2.0.2.0).See the FAQ forum: https://forums.oracle.com/message/9362002
-
small request I have given as below,
Month ProductNo CustomerNo units
9001 1001 Jan - 09 100
9002 1002 Jan - 09 200
9003 1003 Jan - 09 300
Jan 9001 400 ABCCustomer
9002 1004 Jan - 09 500
for all record - if column - customerNo starts with digital it must show as CN_ * other wise of the same name.
Result must be
9001 100 CN_1001-09 Jan
9002 200 CN_1002-09 Jan
9003 300 CN_1003-09 Jan
Jan 9001 400 ABCCustomer
9002 500 CN_1004-09 Jan
Can help get the queryA logic that is easier to implement this is as below
SQL> with t as (select 'jan' Month, 9001 ProductNo,'1001-09' CustomerNo,100 Units from dual union all 2 select 'jan', 9002, '1002-09', 200 from dual union all 3 select 'jan', 9003, '1003-09', 300 from dual union all 4 select 'jan', 9001, 'ABCCustomer', 400 from dual union all 5 select 'Jan', 9002, '1004-09', 500 from dual) 6 select month,productno,case when upper(substr(customerno,1,1))=lower(substr(customerno,1,1)) 7 then 'CN_'||customerno else customerno end customerno,units from t; MON PRODUCTNO CUSTOMERNO UNITS --- ---------- -------------- ---------- jan 9001 CN_1001-09 100 jan 9002 CN_1002-09 200 jan 9003 CN_1003-09 300 jan 9001 ABCCustomer 400 Jan 9002 CN_1004-09 500
TRY WITH THIS upper (substr(customerno,1,1)) = lower (substr(customerno,1,1))
-
need help with the query, thx
Hello
I need a little help with the query I have to write;
the table has 4 columns:
what I am asking is the following result: salary according to the latest available yearcol1 col2 col3 col4 emp_name empl_id salary year content of data: col1 col2 col3 col4 smith 12 1200 1999 smith 12 1340 2000 smith 12 1500 2001 jones 13 1550 1999 jones 13 1600 2000 aron 14 1200 2002
i.e.
ID appreciate some guidance on how to achievesmith 12 1500 2001 jones 13 1600 2000 aron 14 1200 2002
Thank you
Rgdsselect *from (select col1, col2,col3, col4,row_number() over(partition by col1 order by col4 desc) rn from
) where rn=1 -
Help with the query to create hourly statistics
Hello!
I have an array of jobs. Each task has a start_date and a column end_date. end_date can be null if the job is still running.
I need to create a query to display the number of jobs running for all hours during the last two weeks.
A job can run for more than an hour.
I tried to define what it means for a job during an interval:
Job.Start_date < = Interval.end AND Job.Finish_date > = Interval.start
Can help you with this query?
Thank you!
Mihai
Hi, Mihai,
User810719-Oracle wrote:
Hello!
I have an array of jobs. Each task has a start_date and a column end_date. end_date can be null if the job is still running.
I need to create a query to display the number of jobs running for all hours during the last two weeks.
A job can run for more than an hour.
I tried to define what it means for a job during an interval:
Job.Start_date <= interval.end="" and="" job.finish_date="">= Interval.start...
=>
You gave essentially the solution yourself. You just need to outside join your jobs table to a table (or, in the example below, a result set that acts like a table) containing 1 row for each interval. You can use NVL to equate finish_dates with an effective DATE NULL, so they will be counted:
WITH intervals AS
(
SELECT TRUNC (SYSDATE, 'HH') - ((LEVEL-1)/24) AS interval_start
, TRUNC (SYSDATE, 'HH') - ((LEVEL-2)/24) AS interval_end
OF the double
CONNECT BY LEVEL<= 14="" *="">=>
)
SELECT i.interval_start
EARL of (j.start_date) AS jobs_running
Intervals I have
LEFT OUTER JOIN jobs j WE j.start_date<=>=>
AND NVL (j.finish_date
i.interval_end
) > = i.interval_start
;
If you would care to post a small example of data (CREATE TABLE and INSERT statements) and the results desired from this data, I was able to test this.
Simplify the problem for display. Do what you are interested only for the past 6 hours, not the last 2 weeks. We will find a solution that can easily adapt to any number or intervals.
-
Hi, can someone help me with the query. I try to display comm based on calculation below
SAL < 5000 then 10%
5000 - 10000 then 15%
> 10000 then 20%
This is the query
DECLARE
v_comm number (7.2);
v_Name emp.ename%type;
v_empno emp.empno%type;
v_sal emp.sal%type;
BEGIN
SELECT ename, empno, sal IN
v_Name, v_empno, v_sal
EMP;
v_comm: = (IF v_sal < v_sal*.10 then 5000;)
ELSIF v_sal between 5001 AND 10000 THEN v_sal*.15;
Of ANOTHER v_sal > 10001 THEN v_sal*.20;
END IF ;);
dbms_output.put_line(v_Name||) e with empno' | v_empno | "win a comm' | v_comm);
END; /
also tried
DECLARE
v_comm number (7.2);
v_Name emp.ename%type;
v_empno emp.empno%type;
v_sal emp.sal%type;
BEGIN
SELECT ename, empno, sal IN
v_Name, v_empno, v_sal
EMP;
IF v_sal < v_comm then 5000: = v_sal*.10;
ELSIF v_sal between 5001 AND 10000 THEN v_comm: = v_sal*.15;
Of ANOTHER v_sal > 10001 THEN v_comm: = v_sal*.20;
END IF ;);
dbms_output.put_line(v_Name||) e with empno' | v_empno | "win a comm' | v_comm);
END;
/
Hello
Look at the syntax of the IF in the PL/SQL manual. Comapre to the instruction BOX and also the expression BOX.
Perhaps, instead of an IF statement
v_comm: = (IF v_sal<5000 then="">5000>
ELSIF v_sal between 5001 AND 10000 THEN v_sal*.15;
ELSE v_sal > 10001 THEN v_sal*.20;
END IF ;);
you wanted to write an expression BOX, like this:
v_comm: = BOX
WHEN v_sal< 5000 ="" then="" v_sal="" *="">
WHEN v_sal BETWEEN 5001 AND 10000 THEN v_sal *.15
WHEN v_sal > 10001 THEN v_sal *.20
END;
Or perhaps cela:
v_comm: = v_sal * CASE
WHEN v_sal<= 5000="" then="">= >
WHEN v_sal<= 10000="" then="">=>
OF AUTRE.20
END;
You can also use a CASE expression in the SELECT statement.
-
Help with the query to return the last possible value
Can someone please help me to create a query for the situation below?
Table: TABLEA
Columns:
FACID VARCHAR2 (10),
DEPTID VARCHAR2 (10),
CHARGENUMBER NUMBER (10)
I have the following data:
A, B, 1
A, B, 2
C, D, 3
C, D, 4
I will return the following:
A, B, 2
C, D, 4
In other words, I would return the last possible CHARGENUMBER for FACID and DEPTID.
The table has no index, and it is responsible for a worksheet in that order.
Thank you very much!Hello
If you have a TIMESTAMP column, called entry_dt, you can use a Top - N query like this to find the last x entries.
WITH got_rnum AS ( SELECT my_table.* , RANK () OVER (ORDER BY entry_dt DESC) AS rnum FROM my_table ) SELECT * -- or list all columns except rnum FROM got_rnum WHERE rnum <= x ;
It is very common to have a trigger to ensure that columns like entry_dt are met.
If you have only one statement that inserts with hundreds of lines, they can all have the same entry_dt. (The value of SYSTIMESTAMP is constant throughout a statement, even if it takes a few seconds).
The above query uses RANK, so if you tell him you want the last 10 entries, it can return more than 10, because it includes all lines with exactly the same entry_dt as the 10th. If you want to exactly 10 rows returned, even if there is a tie for 10th place, then use ROW_NUMBER instead of RANK; the rest of the query is the same. -
Hi all
I have the below requirement.
I'm looking for a way to identify products with the exact same specifications.Master table: Product ID Product Name 1 TV 2 Cellphone 3 Laptop 4 DVD Player Details table: Product ID Spec type Spec Detail 1 Color Blue 1 Weight 20 2 Color Blue 2 Weight 20 3 Color Blue 3 Weight 25 4 Color Blue 4 Weight 20 4 Height 10
That is to say, in the example TV and cell phone above has exact specifications.
So the result of the query must be TV and phone cell phone here.
Is it possible to do in a query, or we need a sql block to perform this action.
Any help would be greatly appreciated.
Thank you
AgathyaHi agathya,
We do not know how you want to handle the situation where more than two products have the same specifications, so I extended your sample data slightly to take account of this situation as well:
SQL> create table products (id,name) 2 as 3 select 1, 'TV' from dual union all 4 select 2, 'Cellphone' from dual union all 5 select 3, 'Laptop' from dual union all 6 select 4, 'DVD Player' from dual union all 7 select 5, 'Radio' from dual union all 8 select 6, 'VCR' from dual 9 / Tabel is aangemaakt. SQL> create table product_specifications (product_id,spec_type,spec_detail) 2 as 3 select 1, 'Color' , 'Blue' from dual union all 4 select 1, 'Weight', '20' from dual union all 5 select 2, 'Color' , 'Blue' from dual union all 6 select 2, 'Weight', '20' from dual union all 7 select 3, 'Color' , 'Blue' from dual union all 8 select 3, 'Weight', '25' from dual union all 9 select 4, 'Color' , 'Blue' from dual union all 10 select 4, 'Weight', '20' from dual union all 11 select 4, 'Height', '10' from dual union all 12 select 5, 'Color' , 'Blue' from dual union all 13 select 5, 'Weight', '20' from dual union all 14 select 6, 'Color' , 'Blue' from dual union all 15 select 6, 'Weight', '20' from dual union all 16 select 6, 'Height', '10' from dual 17 / Tabel is aangemaakt. SQL> select p1.name 2 , p2.name 3 from products p1 4 , products p2 5 , product_specifications ps1 6 , product_specifications ps2 7 where p1.id = ps1.product_id 8 and p2.id = ps2.product_id 9 and p1.id < p2.id 10 group by p1.id 11 , p1.name 12 , p2.id 13 , p2.name 14 having sqrt(count(*)) = 15 count(case when ps1.spec_type = ps2.spec_type and ps1.spec_detail = ps2.spec_detail then 1 end) 16 / NAME NAME ---------- ---------- TV Cellphone TV Radio Cellphone Radio DVD Player VCR 4 rijen zijn geselecteerd.
But why devil you or your specifications product model predecessor like that? You are much more complicated that it should be. And the data type of size and weight should be not the same as those of color. But now they are. And how do limit you the allowed values for the colors? Color, weight and size are characteristic of your products so that they should have been modeled like this.
See how easily your questions becomes after remodeling it:
SQL> drop table product_specifications purge 2 / Tabel is verwijderd. SQL> drop table products purge 2 / Tabel is verwijderd. SQL> create table products (id,name,color,weight,height) 2 as 3 select 1, 'TV', 'Blue', 20, null from dual union all 4 select 2, 'Cellphone', 'Blue', 20, null from dual union all 5 select 3, 'Laptop', 'Blue', 25, null from dual union all 6 select 4, 'DVD Player', 'Blue', 20, 10 from dual union all 7 select 5, 'Radio', 'Blue', 20, null from dual union all 8 select 6, 'VCR', 'Blue', 20, 10 from dual 9 / Tabel is aangemaakt. SQL> select * from products 2 / ID NAME COLO WEIGHT HEIGHT ---------- ---------- ---- ---------- ---------- 1 TV Blue 20 2 Cellphone Blue 20 3 Laptop Blue 25 4 DVD Player Blue 20 10 5 Radio Blue 20 6 VCR Blue 20 10 6 rijen zijn geselecteerd. SQL> select p1.name 2 , p2.name 3 from products p1 4 , products p2 5 where p1.id < p2.id 6 and ( p1.color = p2.color or (p1.color is null and p2.color is null)) 7 and ( p1.weight = p2.weight or (p1.weight is null and p2.weight is null)) 8 and ( p1.height = p2.height or (p1.height is null and p2.height is null)) 9 / NAME NAME ---------- ---------- TV Cellphone TV Radio Cellphone Radio DVD Player VCR 4 rijen zijn geselecteerd.
I hope this helps.
Kind regards
Rob. -
Need help with the query. Help, please
Hey everyone, need your help. Thank you in advance. In my view, there is function Pivot. Just do not know how to use this function. I have the query that works. The result is:
11-111-1111 Vlad 16505 01/04/2013 5 August 13 dental plan pre-tax amount 29,65
11-111-1111 Vlad 16505 01/04/2013 dental pre-tax 5 August 13 Plan level EE + SP
11-111-1111 16505 Vlad 01/04/2013 5 August 13 pre-tax Option TOP dental plan
11-111-1111 Vlad 16505 01/04/2013 5 August 13 pre-tax dental care plan pay the value
11-111-1111 16505 Vlad 01/04/2013 dental pre-tax 5 August 13 Plan period Type
11-111-1111 Vlad 16505 01/04/2013 amount pre-tax medical Plan of 5 August 13 149
11-111-1111 Vlad 16505 01/04/2013 5 August 13 pre-tax Medical Plan level EE + SP
11-111-1111 Vlad 16505 01/04/2013 5 August 13 pre-tax Plan medical Option MED
11-111-1111 Vlad 16505 01/04/2013 5 August 13 plan pre-tax pay value
11-111-1111 16505 Vlad 01/04/2013 5 August 13 pre-tax Medical Plan period Type
11-111-1111 Vlad 16505 01/04/2013 5 August 13 pre-tax Plan PPO medical Plan
11-111-1111 Vlad 16505 01/04/2013 5 August 13 Vision Plan amount 5.94 pre-tax
But I need the result to be
Amount of SSN ID name item level Option PayValue period Type
11-111-1111 Vlad 16505 01/04/2013 null null high of 5 August 13 pre-tax Dental Plan 29,65 EE + SP
11-111-1111 Vlad 16505 01/04/2013 null null MED 5 August 13 149 medical plan pre-tax EE + SP
11-111-1111 Vlad 16505 01/04/2013 Vision Plan before taxes of 5 August 13
Select distinct
' 11-111-1111 "as ssn,
WOMEN'S WEAR. Employee_number,
"Vlad" as EMPLOYEE_FULL_NAME,
TO_CHAR (papf.start_date, "MM/DD/YYYY") as Date_Of_Hire
a.effective_start_date,
PETF.element_name,
pivf. Name,
peevf.screen_entry_valueOf
PER_all_PEOPLE_F women's wear
per_assignments_f A
pay_element_types_f petf
pay_element_links_f pelf
PAY_ELEMENT_ENTRIES_F penf
PAY_ELEMENT_ENTRY_VALUES_F peevf
pay_input_values_f pivf
WHERE
PETF.element_name ('Dental Plan before taxes', 'Medical Plan before taxes', "Vision Plan before taxes")
and petf. ELEMENT_TYPE_ID = pelf. ELEMENT_TYPE_ID
and (trunc (sysdate) BETWEEN pelf. EFFECTIVE_START_DATE AND pelf. EFFECTIVE_END_DATE)
and (pelf. ELEMENT_LINK_ID = penf. ELEMENT_LINK_ID and a.assignment_id = penf. ASSIGNMENT_ID)
and (trunc (sysdate) BETWEEN penf. EFFECTIVE_START_DATE AND penf. EFFECTIVE_END_DATE)
and penf. ELEMENT_ENTRY_ID = peevf. ELEMENT_ENTRY_ID
and peevf. INPUT_VALUE_ID = pivf. INPUT_VALUE_ID
AND papf.employee_number IS NOT NULL
AND A.assignment_type = 'E '.
AND A.person_id = papf.person_id
and papf.effective_end_date > sysdate
and a.effective_end_date > sysdate
and (trunc (sysdate) BETWEEN women's wear. EFFECTIVE_START_DATE AND women's wear. EFFECTIVE_END_DATE)
and a.effective_start_date = (select MAX (effective_start_date) from PER_ASSIGNMENTS_f where assignment_id = a.assignment_id)
and a.assignment_id = 42643
and a.assignment_status_type_id = '1'
order of petf.element_name;Change with your query
SELECT * FROM (select distinct)
' 11-111-1111 "as ssn,
WOMEN'S WEAR. Employee_number,
"Vlad" as employee_full_name,
TO_CHAR (papf.start_date, "MM/DD/YYYY") as date_of_hire
a.effective_start_date,
PETF.element_name,
pivf. Name,
peevf.screen_entry_value
Of
PER_all_PEOPLE_F women's wear
per_assignments_f A
pay_element_types_f petf
pay_element_links_f pelf
PAY_ELEMENT_ENTRIES_F penf
PAY_ELEMENT_ENTRY_VALUES_F peevf
pay_input_values_f pivf
WHERE
PETF.element_name ('Dental Plan before taxes', 'Medical Plan before taxes', "Vision Plan before taxes")
and petf. ELEMENT_TYPE_ID = pelf. ELEMENT_TYPE_ID
and (trunc (sysdate) BETWEEN pelf. EFFECTIVE_START_DATE AND pelf. EFFECTIVE_END_DATE)
and (pelf. ELEMENT_LINK_ID = penf. ELEMENT_LINK_ID and a.assignment_id = penf. ASSIGNMENT_ID)
and (trunc (sysdate) BETWEEN penf. EFFECTIVE_START_DATE AND penf. EFFECTIVE_END_DATE)
and penf. ELEMENT_ENTRY_ID = peevf. ELEMENT_ENTRY_ID
and peevf. INPUT_VALUE_ID = pivf. INPUT_VALUE_ID
AND papf.employee_number IS NOT NULL
AND A.assignment_type = 'E '.
AND A.person_id = papf.person_id
and papf.effective_end_date > sysdate
and a.effective_end_date > sysdate
and (trunc (sysdate) BETWEEN women's wear. EFFECTIVE_START_DATE AND women's wear. EFFECTIVE_END_DATE)
and a.effective_start_date = (select MAX (effective_start_date) from PER_ASSIGNMENTS_f where assignment_id = a.assignment_id)
and a.assignment_id = 42643
and a.assignment_status_type_id = '1')
PIVOT (MAX (screen_entry_value) FOR (name) TO ("Amount" AS 'Amount', 'level' AS 'level', 'Option High' AS 'High Option', 'Pay the value' AS 'Value to pay', 'Period of Type' AS 'Type period'))
order by element_name;
(GOLD)
SELECT ssn,
Employee_number,
employee_full_name,
date_of_hire,
effective_start_date,
element_name,
Max (decode (Name, 'Amount', screen_entry_value)) 'amount. "
Max (decode (Name, 'Level', screen_entry_value)) 'level ',.
MAX (DECODE (name, "High Option", screen_entry_value)) "High Option",
MAX (DECODE (name, 'Value of pay', screen_entry_value)) 'value of pay. "
MAX (DECODE (name, 'Period Type', screen_entry_value)) 'period of Type '.
FROM (select distinct)
' 11-111-1111 "as ssn,
WOMEN'S WEAR. Employee_number,
"Vlad" as employee_full_name,
TO_CHAR (papf.start_date, "MM/DD/YYYY") as date_of_hire
a.effective_start_date,
PETF.element_name,
pivf. Name,
peevf.screen_entry_value
Of
PER_all_PEOPLE_F women's wear
per_assignments_f A
pay_element_types_f petf
pay_element_links_f pelf
PAY_ELEMENT_ENTRIES_F penf
PAY_ELEMENT_ENTRY_VALUES_F peevf
pay_input_values_f pivf
WHERE
PETF.element_name ('Dental Plan before taxes', 'Medical Plan before taxes', "Vision Plan before taxes")
and petf. ELEMENT_TYPE_ID = pelf. ELEMENT_TYPE_ID
and (trunc (sysdate) BETWEEN pelf. EFFECTIVE_START_DATE AND pelf. EFFECTIVE_END_DATE)
and (pelf. ELEMENT_LINK_ID = penf. ELEMENT_LINK_ID and a.assignment_id = penf. ASSIGNMENT_ID)
and (trunc (sysdate) BETWEEN penf. EFFECTIVE_START_DATE AND penf. EFFECTIVE_END_DATE)
and penf. ELEMENT_ENTRY_ID = peevf. ELEMENT_ENTRY_ID
and peevf. INPUT_VALUE_ID = pivf. INPUT_VALUE_ID
AND papf.employee_number IS NOT NULL
AND A.assignment_type = 'E '.
AND A.person_id = papf.person_id
and papf.effective_end_date > sysdate
and a.effective_end_date > sysdate
and (trunc (sysdate) BETWEEN women's wear. EFFECTIVE_START_DATE AND women's wear. EFFECTIVE_END_DATE)
and a.effective_start_date = (select MAX (effective_start_date) from PER_ASSIGNMENTS_f where assignment_id = a.assignment_id)
and a.assignment_id = 42643
and a.assignment_status_type_id = '1')
GROUP BY ssn, employee_number, employee_full_name, date_of_hire, effective_start_date, NOM_ELEMENT
order by element_name;
-
Please help improve the query with the analytic function
The mentioned below query takes about 10 hours to complete (10.2.0.4).
There are 3 tables (table t has a relationship 1: n with table e and k table also has a relationship 1: n with table e).
Table a contains 200,000 lines. (this table is truncated and inserted several times a week)
E table contains rows of 1Mio.
K table contains rows of 170Mio.
I need a query that takes the latest 3 posts for a given t_id:drop table t; create table t ( t_id number, constraint t_pk primary key (t_id) ); drop table e; create table e ( e_id number, e_doc nvarchar2(16), e_date date, constraint e_pk primary key (e_id) ); drop table k; create table k ( t_id number, e_id number ); create unique index k_i1 on k(t_id, e_id); exec dbms_stats.gather_table_stats(user, 'T'); exec dbms_stats.gather_table_stats(user, 'K'); exec dbms_stats.gather_table_stats(user, 'E'); -- Sample data: insert into t(t_id) values (100); insert into t(t_id) values (101); insert into t(t_id) values (102); insert into t(t_id) values (103); insert into e(e_id, e_doc, e_date) values (200, 'doc 200', to_date('01.01.2010', 'DD.MM.YYYY')); insert into e(e_id, e_doc, e_date) values (201, 'doc 201', to_date('02.01.2010', 'DD.MM.YYYY')); insert into e(e_id, e_doc, e_date) values (202, 'doc 202', to_date('03.01.2010', 'DD.MM.YYYY')); insert into e(e_id, e_doc, e_date) values (203, 'doc 203', to_date('04.01.2010', 'DD.MM.YYYY')); insert into e(e_id, e_doc, e_date) values (204, 'doc 204', to_date('05.01.2010', 'DD.MM.YYYY')); insert into e(e_id, e_doc, e_date) values (205, 'doc 205', to_date('06.01.2010', 'DD.MM.YYYY')); insert into e(e_id, e_doc, e_date) values (206, 'doc 206', to_date('07.01.2010', 'DD.MM.YYYY')); insert into e(e_id, e_doc, e_date) values (207, 'doc 207', to_date('08.01.2010', 'DD.MM.YYYY')); insert into k(t_id, e_id) values (100, 200); insert into k(t_id, e_id) values (100, 201); insert into k(t_id, e_id) values (100, 202); insert into k(t_id, e_id) values (100, 203); insert into k(t_id, e_id) values (101, 203); insert into k(t_id, e_id) values (101, 204); select k.t_id, e.e_date, e.e_id, e.e_doc from e, k, t where k.e_id = e.e_id and k.t_id = t.t_id order by k.t_id, e.e_date desc; T_ID E_DATE E_ID E_DOC ---------- -------- ---------- ---------------- 100 04.01.10 203 doc 203 100 03.01.10 202 doc 202 100 02.01.10 201 doc 201 100 01.01.10 200 doc 200 101 05.01.10 204 doc 204 101 04.01.10 203 doc 203
The example query takes several hours in production.T_ID E_DOC_LIST ---------- ----------------------- 100 doc 200/doc 201/doc 202 101 doc 203/doc 204 Sample query: select t_id, e_doc_list from ( select k.t_id, row_number() over(partition by k.t_id order by k.t_id, e.e_date desc) r_num, rtrim( lag(e.e_doc, 0) over(partition by k.t_id order by k.t_id, e.e_date) || '/' || lag(e.e_doc, 1) over(partition by k.t_id order by k.t_id, e.e_date) || '/' || lag(e.e_doc, 2) over(partition by k.t_id order by k.t_id, e.e_date), '/') e_doc_list from e, k, t where k.e_id = e.e_id and k.t_id = t.t_id order by k.t_id, e.e_date desc ) where r_num = 1 ; T_ID E_DOC_LIST ---------- -------------------------------------------------- 100 doc 203/doc 202/doc 201 101 doc 204/doc 203
The r_num = 1 filter is applied quite late. Is there another way to generate the query or even review the tables.
For the sample query: ----------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ----------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 6 | 468 | 6 (50)| 00:00:01 | |* 1 | VIEW | | 6 | 468 | 6 (50)| 00:00:01 | |* 2 | WINDOW SORT PUSHED RANK | | 6 | 216 | 6 (50)| 00:00:01 | | 3 | WINDOW SORT | | 6 | 216 | 6 (50)| 00:00:01 | | 4 | NESTED LOOPS | | 6 | 216 | 4 (25)| 00:00:01 | | 5 | MERGE JOIN | | 6 | 198 | 4 (25)| 00:00:01 | | 6 | TABLE ACCESS BY INDEX ROWID| E | 8 | 208 | 2 (0)| 00:00:01 | | 7 | INDEX FULL SCAN | E_PK | 8 | | 1 (0)| 00:00:01 | |* 8 | SORT JOIN | | 6 | 42 | 2 (50)| 00:00:01 | | 9 | INDEX FULL SCAN | K_I1 | 6 | 42 | 1 (0)| 00:00:01 | |* 10 | INDEX UNIQUE SCAN | T_PK | 1 | 3 | 0 (0)| 00:00:01 | ----------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter("R_NUM"=1) 2 - filter(ROW_NUMBER() OVER ( PARTITION BY "K"."T_ID" ORDER BY "K"."T_ID",INTERNAL_FUNCTION("E"."E_DATE") DESC )<=1) 8 - access("K"."E_ID"="E"."E_ID") filter("K"."E_ID"="E"."E_ID") 10 - access("K"."T_ID"="T"."T_ID") and for query in production --------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| --------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 3118K| 425M| | 160K (1)| | 1 | VIEW | | 3118K| 425M| | 160K (1)| | 2 | SORT ORDER BY | | 3118K| 163M| 383M| 160K (1)| | 3 | WINDOW SORT PUSHED RANK| | 3118K| 163M| 383M| 160K (1)| | 4 | WINDOW SORT | | 3118K| 163M| 383M| 160K (1)| | 5 | HASH JOIN | | 3118K| 163M| 40M| 33991 (1)| | 6 | TABLE ACCESS FULL | E | 1053K| 28M| | 4244 (1)| | 7 | NESTED LOOPS | | 3118K| 80M| | 21918 (1)| | 8 | TABLE ACCESS FULL | T | 144K| 1829K| | 282 (2)| | 9 | INDEX RANGE SCAN | K_I1 | 22 | 308 | | 1 (0)| ---------------------------------------------------------------------------------------
TimWong765 wrote:
...
Table a contains 200,000 lines. (* this table is truncated and inserted several times a week *)You could be in one of the rare cases where the index should be rebuild, take a look in the following thread:
http://asktom.Oracle.com/pls/asktom/f?p=100:11:0:P11_QUESTION_ID:6601312252730 #69571308712887 (search for 'index of Sweeper')
Make sure that you have checked if you are in this case before going for an expensive index rebuild.Nicolas.
-
Need help with the query string manipulation
Hello
With the help of 10.1.0.4.2
Given a table called PREFIX_CODES that contains a column called PREFIX_CODE
with the following data:
PREFIX_CODE - VARCHAR2 (20)
-------------------
AAA
BENAMER
CARTER
and another table called SUBSCRIBERS with a column called SUBSCRIBER_ID
with the following data:
SUBSCRIBER_ID - VARCHAR2 (30)
---------------------
BBBB-123456
AAA-444444
DD-2222222
EEEE-888888
Is there a query that will pull all the SUBSCRIBER_ID that begin with
each of the PREFIX_CODES? The following query is not valid, but it
This will give you an idea of what I'm trying to do:
SELECT SUBSCRIBER_ID
SUBSCRIBERS
WHERE AS SUBSCRIBER_ID (SELECT PREFIX_CODE |) » %'
OF PREFIX_CODES)
Using the data from above, I would like that the query to return:
BBBB-123456
AAA-444444
Thanks for your help!Hello
Welcome to the forum!
You were on the right track. To find if a given subscriber corresponds to any prefix_code, you can do an EXISTS subquery:
SELECT subscriber_id FROM subsribers s WHERE EXISTS ( SELECT NULL FROM prefix_codes WHERE s.subscriber_id LIKE prefix_code || '%' ) ;
This will tell you if at least a prefix code. It won't tell you exactly how many, or what they were.
Your message, I'm guessing that there may be more than one, and you can deduct the subscriber_id itself, then the above query should work for you. -
Need help with the query to get the County
Hello
Oracle 10 g 2 10.2.0.3 - 64 bit
I want back the number of accounts with two different types of funds (say A and B). Some accounts hold only one of the two funds, and some support both. I want to get the counts like this:
- account held funds - has only
- accounts holding funds-B only
- accounts holding the Fund-A and B funds
Here is what I started with but need assistance to meet the requirement above:
select count(distinct acct.bkoff_acct_no ) accounts_holding_fund_A from xe_account acct, xec_tal_investment_mandate iman, xec_tal_asset_allocation alloc, xe_benchmark bmark, xe_benchmark_usage bu, xe_object_description xod, xec_asset_class cls where iman.mandate_status_cd='A' and cls.asset_class_cd = alloc.asset_class_cd and iman.mandate_id = alloc.mandate_id and acct.account_id = iman.object_id and iman.object_type_cd = 'ACCT' and iman.mandate_id = xod.object_id and xod.field_nm='XEC_TAL_INVESTMENT_MANDATE.COMMENT_TXT' and xod.language_cd = 'E' and acct.acct_status_cd = 'O' and bu.object_type_cd(+) = 'TMAA' and bu.object_id(+) = alloc.asset_allocation_id and bmark.benchmark_id(+) = bu.benchmark_id and alloc.resp_txt like '%fund-A%'
And suppose that the Fund-B has resp_txt like ' % of Fund-B»
Please suggest.
Concerning
Hello
Here is another way, it is easier to adapt to different jobs and different numbers of jobs:
WITH got_distinct_jobs AS
(
SELECT DISTINCT deptno, job
FROM scott.emp
WHERE job IN ("ANALYST", "CLERKS") - or what
)
got_job_list AS
(
SELECT LISTAGG (job, ",") THE Group (ORDER BY work) AS job_list
OF got_distinct_jobs
GROUP BY deptno
)
SELECT job_list
COUNT (*) AS num_departments
OF got_job_list
GROUP BY job_list
;
This shows all the combinations of the jobs listed in the WHERE clause of got_distinct_jobs. You don't need to change anything else in the query. There may be any number of jobs.
Output:
JOB_LIST NUM_DEPARTMENTS
------------------------------ ---------------
CLERK 2
ANALYST, CLERK 1
-
Need help in the query.
with t as
(
Select 'AAA', 'AAA01' wers_feature_code of all family_code the double union
Select 'AAA', 'AAA02' from dual union all
Select "BBB-", 'BBB01' from dual union all
Select "BBB-", 'BBB02' from dual
)
Select t1.wers_feature_code. T t1, t t2 T2.wers_feature_code
Result:
T1. WERS_FE
--------------------------------------------------------------------------------
AAA01AAA01
AAA01AAA02
AAA01BBB01
AAA01BBB02
AAA02AAA01
AAA02AAA02
AAA02BBB01
AAA02BBB02
BBB01AAA01
BBB01AAA02
BBB01BBB01
T1. WERS_FE
--------------------------------------------------------------------------------
BBB01BBB02
BBB02AAA01
BBB02AAA02
BBB02BBB01
BBB02BBB02
The above query should be filtered. The above documents not family_code even.
For example AAA01AAA01, AAA01AAA02, AAA02AAA01, AAA02AAA02, BBB01BBB01, BBB02BBB01,
BBB02BBB02 should not be visible. The code is not difficult family_code in the query. Can be more than a family_code.
Thank you
VinodhHi, Vinodh,
That's what you asked for:
WITH got_family_cnt AS ( SELECT family_code , wers_feature_code , COUNT (DISTINCT family_code) OVER () AS family_cnt FROM sample_tbl ) SELECT REPLACE ( SYS_CONNECT_BY_PATH (wers_feature_code, '>') , '>' ) AS output FROM got_family_cnt WHERE LEVEL = family_cnt CONNECT BY NOCYCLE family_code != PRIOR family_code ;
It produces 48 lines
(2 choices for family 'AAA'
* 2 for "BBB".
* 2 for "CCC".
* 3 choices for 1 family
* 2 (2nd).This assumes you know some substring that never happens in wers_feature_code. I used ' > ' above, but you can use anything else.
Published by: Frank Kulash, June 15, 2010 10:42
Simplified query. -
Help with the query to return the results of the group in the table?
Hello
Im a total noob, so please be nice...!
I'm looking around a table contains a column of WORD and a WORD_TYPE_ID column.
Words can have the same type_id.
What I'm trying to do is to write a query that will return the longest word for each word_type_id.
I tried for hours to get it and everything seems to get is two error messages or just the longest word in the whole WORD column.
Is the furthest I can get before things break down...
Select Word
table
where
Length (Word) =
(
Select
Max (length (Word))
table
)
Any help on this or if I could be pointed in the right direction it would be greatly appreciated.
Thank youHello
Welcome to the forum!
Here's one way:
SELECT word_type_id , MIN (word) KEEP (DENSE_RANK LAST ORDDER BY LENGTH (word)) AS longest_word FROM table_x GROUP BY word_type_id;
If there be a tie in some word_type_id (i.e. 2 or more words have exactly the same length, which is the longest in this group) the expression above will return the first one alphabetically. (This is what means here MIN.)
Published by: Frank Kulash, 11 August 2009 13:56
You almost had it.
As you have noticed, you get the longest line across the table. This is because your subquery was watching the entire table.
If you to correlate the subquery to the row in the main table, as shown below, you can get the longest word in each group:select word from table m -- m for main where length (word) = ( select max (length(word)) from table where word_type_id = m.word_type_id -- New );
-
Need help with the query (transpose)
Hello experts, please help here - 11 g Oracle, attribute APA APB are fixed values and can be hard coded in query
Need to transpose these data. Max(decode.. perd les données en sortie à cause de la condition de Max.)
Entry:
Name | Attribute | Value
A1 APP 10
A1 PDB 11
A1 APA 20
A1 PDB 21
A2 BPA 13
A2 BPB 14
Expected results:
Name AttVal1 AttVal2
A1 10 11
13 14 A2
20 21 A1
Hello
Here's one way:
WITH relevant_columns AS
(
SELECT name
value
CASE
WHEN the attribute ("APA", "BPA") THEN 1
WHEN the attribute ("PDB", "BPB") THEN 2
END AS c_num
ROW_NUMBER () (PARTITION BY NAME, attirbute
Value of ORDER BY
) AS r_num
FROM table_x
)
SELECT name, attval1, attval2
OF relevant_columns
PIVOT (MAX (value)
FOR c_num IN (1 AS attval1
2 UNDER attval2
)
)
ORDER BY r_num, name
;
If you would care to post CREATE TABLE and INSERT statements for your sample data, and then I could test this.
Why do you want to
NAME ATTVAL1 ATTVAL2
---- ------- -------
A1 10 11
20 21 A1
in the results, rather than
NAME ATTVAL1 ATTVAL2
---- ------- -------
10 21 A1
20 11-A1
? You would be satisfied to one or the other?
Depending on your answer, you may need to modify the analytical ORDER BY clause in the ROW_NUMBER function.
Maybe you are looking for
-
Texts of Mute Group on Apple Watch
THERE MUST BE A WAY OF SETTING-SPECIFIC TEXT STRINGS MUTE GROUP SO MY WATCH DOESN'T BEEP ALL DAY. I need to keep running notifications but the texts of the group are the bane of my existence when you cannot remove yourself from it. If one person in
-
M60 - 135: where can I get the Power Saver utility?
Hello!where can I get the utility for power management of toshiba for my m60-135? in the download category is not one. can I take from one series of other energy saver? Best regardsThomas
-
Hello Im not getting that error at the launch of the "no HDD Recovery area" software is not to delete the recovery partitions I didn't touch... I tried to restart and hold down the 0 key it did not work, I used f8 but toshiba recovery did not work, h
-
"Printer: door open" error on HP Deskjet 3845
The green light on my HP Deskjet 3845 printer continues to blink. Whenever I try to print something, even a test page, the error message says "printer: open door." However, I checked several times and there is no open door! I am on Windows 7 (64-bi
-
Letter Q is put automatically typed
Hello The letter Q gets automatically typed all the time. I tried to restore the system to 30 days back when I couldn't see the problem but no use. There is no problem with the keyboard, and I got the external keyboard to test also. The question also