Oracle SQL Query
Hi
Need some help with a query
I have a table with the following records (examples)
Title Initial Surname Unique_ID AccNo_1 Pay_1 AccNo_2 Pay_2
MR MJ JACKSON 0002283543 4232080448 2400.00 4262784181 6802.84
MR MJ JACKSON 0002283543 4269248501 12000.00 4275330444 0.00
MR TJ HOOKER 0005236988 4275207018 5953.20 4275330710 1936.00
MR TJ HOOKER 0005236988 4279256421 495.00 4272126429 0.00
I need to show it as below in a query (Showing the Unique ID,Account No and Payment alone is fine as well)
Title Initial Surname Unique_ID Account_No Payment
MR MJ JACKSON 0002283543 4232080448 2400.00
MR MJ JACKSON 0002283543 4262784181 6802.84
MR MJ JACKSON 0002283543 4269248501 12000.00
MR MJ JACKSON 0002283543 4275330444 0.00
MR TJ HOOKER 0005236988 4275207018 5953.20
MR TJ HOOKER 0005236988 4275330710 1936.00
MR TJ HOOKER 0005236988 4279256421 495.00
MR TJ HOOKER 0005236988 4272126429 0.00
SQL> SELECT SUBSTR(T_NAME,1,10) T_NAME, SUBSTR(T_ACNO,1,5)ACNO1, T_VAL, SUBSTR(T_ACNO2,1,5) ACNO2, T_VAL2 FROM TMP
2
SQL> /
T_NAME ACNO1 T_VAL ACNO2 T_VAL2
ABC 1234 100 1111 200
ABC 5678 200 2222 300
DEF 1234 100 3333 400
DEF 5678 200 4444 500
SQL> SELECT SUBSTR(T_NAME,1,5) TNAME, SUBSTR(T_ACNO,1,5) TACNO, T_VAL
2 FROM
3 (
4 SELECT T_NAME, T_ACNO, T_VAL
5 FROM TMP
6 UNION ALL
7 SELECT T_NAME, T_ACNO2, T_VAL2
8 FROM TMP
9 )
10 ORDER BY 1
11 /
TNAME TACNO T_VAL
ABC 1234 100
ABC 5678 200
ABC 1111 200
ABC 2222 300
DEF 1234 100
DEF 5678 200
DEF 3333 400
DEF 4444 500
8 rows selected.
SQL>
Similar Messages
-
Clarification on using function in where clause of oracle sql query
I have an issue in regarding function using where clause of sql query..
We are facing performance issue while executing query, so in what ways to improve the performance of the query which i have posted below.
select col ,case when my_function(parameter)
from tab1 a ,tab2 b,tabl3 c
where a.column1=b.column2
and b.column3 =c.column6
and my_function(parameter)>0
Regards
Dinesh
Edited by: wild fire on May 18, 2012 4:15 PMDinesh,
remind that when you use a function in the where clause it normally will get started for each record in the table.
So your answer is two-fold:
1. make the function only start when needed by adding a function based index on the table (this will make inserts and updates slower)
2. make the function faster by adding the DETERMINISTIC clause if possible. this will make Oracle "cache" the result of the function.
Regards,
Richard
blog: http://blog.warp11.nl
twitter: @rhjmartens
If this question is answered, please mark the thread as closed and assign points where earned.. -
Oracle Sql Query issue Running on Different DB Version
Hello All,
I have come into situation where we are pruning sql queries on different DB version of Oracle and have performance issue. Let me tell you in brief and i really appreciate for your prompt response as its very imperative stuff.
I have a query which is running on a DB of version 7.3.4 and it takes around 30 mins where as the same query when run on 8i it takes 15sec., its a huge difference. I have run the statistics to analyze on 7.3 and its comparatively very high. Question here is, the sql query trys to select data from same schema table and 2 tables from another DB using DB link and 2 other tables from another DB using DB link.So,how can we optimize this stuff and achieve this run as same time as 8i DB in 7.3. Hope i am clear about my question, Eagerly waiting for your replies.
Thanks in Advance.
Message was edited by:
Ram8Difficult to be sure without any more detailed information, but I suspect that O7 is in effect copying the remote tables to local temp space, then joining; 8i is factoring out a better query to send to the remote DBs, which does as much work as possible on the remote DB before shipping remaining rows back to local.
You should be able to use EXPLAIN PLAN to identify what SQL is being shipped to the remote DB, If you can't (and it's been quite a while since I tried DB links or O7) then get the remote DBs to yourself, and set SQL_TRACE on for the remote instances. Execute the query and then examine the remote trace files, And don't forget to turn off the tracing when you're done.
Of course it could just be that the CBO got better,,,
HTH - if not, post your query and plans for the local db, and the remote queries.
Regards Nigel -
Oracle SQL Query from EXCEL 2007 with prompt
Hello,
I have many excel reports where I am pulling information from our Oracle 9 db through Excel using the following method:
http://blog.mclaughlinsoftware.com/microsoft-excel/how-to-query-oracle-from-excel-2007/
http://blog.mclaughlinsoftware.com/2009/11/30/sql-query-in-excel-2007/
However, I am having trouble when I try the following query due to the prompt:
SELECT
IM.ITEM_GROUP,
IM.ITEM,
IM.DESCRIPTION
FROM
LAWSON.ITEMMAST IM
WHERE
IM.ITEM = '&ITEM';
Does any one know how I can connect a prompt to an excel cell and then pass the query on to Oracle, or have a PL SQL prompt work from Excel?
Thanks,
BenThe 'prompt' as you call it is a sqlplus feature, so does not belong to the SQL language
You would need to write a stored procedure returning a resultset.
create or replace procedure foo(rc in out sys_refcursor, p_item) as
begin
open rc for
'SELECT
IM.ITEM_GROUP,
IM.ITEM,
IM.DESCRIPTION
FROM
LAWSON.ITEMMAST IM
WHERE
IM.ITEM = '||p_item;
end;
and call that using ODBC or asp.net
Obviously this is profusely documented.
Sybrand Bakker
Senior Oracle DBA -
Oracle SQL query for getting specific special characters from a table
Hi all,
This is my table
Table Name- Table1
S.no Name
1 aaaaaaaa
2 a1234sgjghb
3 a@3$%jkhkjn
4 abcd-dfghjik
5 bbvxzckvbzxcv&^%#
6 ashgweqfg/gfjwgefj////
7 sdsaf$([]:'
8 <-fdsjgbdfsg
9 dfgfdgfd"uodf
10 aaaa bbbbz#$
11 cccc dddd-/mnm
The output has to be
S.no Name
3 a@3$%jkhkjn
5 bbvxzckvbzxcv&^%#
7 sdsaf$([]:'
8 <-fdsjgbdfsg
10 aaaa bbbbz#$
It has to return "Name" column which is having special characters,whereas some special chars like -, / ," and space are acceptable.
The Oracle query has to print columns having special characters excluding -,/," and space
Can anyone help me to get a SQL query for the above.
Thanks in advance.You can achieve it in multiple ways. Here are few.
SQL> with t
2 as
3 (
4 select 1 id, 'aaaaaaaa' name from dual union all
5 select 2 id, 'a1234sgjghb' name from dual union all
6 select 3 id, 'a@3$%jkhkjn' name from dual union all
7 select 4 id, 'abcd-dfghjik' name from dual union all
8 select 5 id, 'bbvxzckvbzxcv&^%#' name from dual union all
9 select 6 id, 'ashgweqfg/gfjwgefj////' name from dual union all
10 select 7 id, 'sdsaf$([]:''' name from dual union all
11 select 8 id, '<-fdsjgbdfsg' name from dual union all
12 select 9 id, 'dfgfdgfd"uodf' name from dual union all
13 select 10 id, 'aaaa bbbbz#$' name from dual union all
14 select 11 id, 'cccc dddd-/mnm' name from dual
15 )
16 select *
17 from t
18 where regexp_like(translate(name,'a-/" ','a'), '[^[:alnum:]]');
ID NAME
3 a@3$%jkhkjn
5 bbvxzckvbzxcv&^%#
7 sdsaf$([]:'
8 <-fdsjgbdfsg
10 aaaa bbbbz#$
SQL> with t
2 as
3 (
4 select 1 id, 'aaaaaaaa' name from dual union all
5 select 2 id, 'a1234sgjghb' name from dual union all
6 select 3 id, 'a@3$%jkhkjn' name from dual union all
7 select 4 id, 'abcd-dfghjik' name from dual union all
8 select 5 id, 'bbvxzckvbzxcv&^%#' name from dual union all
9 select 6 id, 'ashgweqfg/gfjwgefj////' name from dual union all
10 select 7 id, 'sdsaf$([]:''' name from dual union all
11 select 8 id, '<-fdsjgbdfsg' name from dual union all
12 select 9 id, 'dfgfdgfd"uodf' name from dual union all
13 select 10 id, 'aaaa bbbbz#$' name from dual union all
14 select 11 id, 'cccc dddd-/mnm' name from dual
15 )
16 select *
17 from t
18 where translate
19 (
20 lower(translate(name,'a-/" ','a'))
21 , '.0123456789abcdefghijklmnopqrstuvwxyz'
22 , '.'
23 ) is not null;
ID NAME
3 a@3$%jkhkjn
5 bbvxzckvbzxcv&^%#
7 sdsaf$([]:'
8 <-fdsjgbdfsg
10 aaaa bbbbz#$
SQL> -
Why does Oracle SQL query returning a date field without the time component
Hi,
I'm a novice SQL user & hv just installed Oracle SQL developer (Version 3.0.04, Build MAIN-04.34).
I made the same SQL query using "Oracle SQL developer" & "TOAD for Oracle 9.0.1" but I got 2 different format on the same date field:
On TOAD, I get the date field extracted as *04/26/2011 23:12:58*
On Oracle, I get the date field extracted as *26/APR/11*
Why is the Oracle result in a different format & missing the time component?
Is there any option/preference that I need to set in Oracle SQL developer to get the full date/time format displayed?
I've tried to set my the date format to DD/MON/RR HH12:MI:SSXFF AM under the preference -> database NLS but I still get the same format!
Plse help!hokim wrote:
Hi,
I'm a novice SQL user & hv just installed Oracle SQL developer (Version 3.0.04, Build MAIN-04.34).
I made the same SQL query using "Oracle SQL developer" & "TOAD for Oracle 9.0.1" but I got 2 different format on the same date field:
On TOAD, I get the date field extracted as *04/26/2011 23:12:58*
On Oracle, I get the date field extracted as *26/APR/11*
Why is the Oracle result in a different format & missing the time component?
Is there any option/preference that I need to set in Oracle SQL developer to get the full date/time format displayed?
I've tried to set my the date format to DD/MON/RR HH12:MI:SSXFF AM under the preference -> database NLS but I still get the same format!
Plse help!http://edstevensdba.wordpress.com/category/nls_date_format/ -
Convert Oracle SQL Query to SQLite Query
Hi,
I am new to SQLite and have requirement to write quries in SQLite format. As table structure is same in both the Oracle and SQLite database i have created queries in Oracle SQL. Now wanted to know is there any tool or package available which will convert my simple Oracle queries into SQLite format???
Please guide..
Regards,
Priyankawith tree as (
select
level lev
,id
,parent_id
,sys_connect_by_path(id, '-') p
from test_temp
connect by
parent_id = prior id
start with parent_id is null
select
from tree t1
where exists (
select 1
from tree t2
where substr(t2.p, 1, length(t1.p)) = t1.p
and regexp_count(t2.p, '-') = regexp_count(t1.p, '-') + 2
-- if regexp_count is not available in your version or performs not well, below is an alternative
-- and length(t2.p) - length(replace(t2.p, '-')) = length(t1.p) - length(replace(t1.p, '-')) + 2
LEV
ID
PARENT_ID
P
1
1
-1
2
3
1
-1-3
2
5
1
-1-5
1
2
-2
2
6
2
-2-6
3
14
6
-2-6-14 -
Oracle sql query execute in the apex
hi:
my requirement is, in region i have one text area item and query button.
in this text area i am typing the any sql query and when i am pressing the query button it should display the query output report..
pls give solution fast as soon as possible....
Message was edited by:
anbarasanHi anbarasan,
Maybe you can create a report of type "SQL query (PL/SQL Function returning query". Then in the region source, something like:
DECLARE
v_sql VARCHAR2 (4000);
BEGIN
v_sql := :PX_YOUR_ITEM;
RETURN v_sql;
END;
Paulo Vale
http://apex-notes.blogspot.com -
Mysql equivalent oracle sql query is required
Hi all,
please help me convert mysql quyery into sql query.
select u.username, t.name as fullname, p.id as project_id, p.name as project,
(select count(a.resource_id) from pd_resource_task_alloc as a left OUTER JOIN pd_resource_task as b on a.task_id=b.task_id where a.task_id=t.id ) as allocatedTask,
(select count(a.resource_id) from pd_resource_task_alloc as a left OUTER JOIN pd_resource_task as b on a.task_id=b.task_id where a.task_id=t.id and b.task_status_id='2') as completedTask,
(select count(a.resource_id) from pd_resource_task_alloc as a left OUTER JOIN pd_resource_task as b on a.task_id=b.task_id where a.task_id=t.id and b.task_status_id!='2') as pendingTask,
c.name as category, sub.task_id, t.name, sub.bucket_date, sum(sub.alloc_time) as s
from (select rt.task_id, rt.resource_id, rt.project_id, rta.sequence, rta.alloc_time,DATE(th.creation_date) as creation_date,rt.start_date,th.task_status_id,rt.start_date as bucket_date from resource_task rt, resource_task_alloc rta ,task_history th
where rt.task_id = rta.task_id and rt.task_id = th.task_id and th.task_status_id >=2
and rt.resource_id = rta.resource_id and rt.project_id = rta.project_id
and rta.alloc_time > 0 and rt.start_date is not null ) as sub,
pd_tool_user u, pd_task t, pd_project p, pd_category c where sub.resource_id = u.id
and sub.start_date >='2011-11-01' and sub.creation_date <= '2011-11-31'
and sub.task_id = t.id and sub.project_id = p.id and
t.category_id = c.id
and sub.project_id='355'
group by p.project, u.username, p.id , u.name, sub.task_id, t.name, c.name,
sub.bucket_date order by u.username, sub.bucket_date, p.name, c.nameThanks,
P PrakashNot sure as we don't have any tables and data to work with, but firstly...
a) format your code to make it easier to see what's going on
b) don't mix ansi joins with regular joins
c) treat DATEs as DATEs
d) Table alias names don't use the "AS" keyword
First guess (obviously unteseted)...
select u.username
,t.name as fullname
,p.id as project_id
,p.name as project
,(select count(a.resource_id)
from pd_resource_task_alloc a
left OUTER JOIN pd_resource_task b on (a.task_id=b.task_id)
where a.task_id=t.id
) as allocatedTask
,(select count(a.resource_id)
from pd_resource_task_alloc a
left OUTER JOIN pd_resource_task b on (a.task_id=b.task_id)
where a.task_id=t.id
and b.task_status_id='2'
) as completedTask
,(select count(a.resource_id)
from pd_resource_task_alloc a
left OUTER JOIN pd_resource_task b on (a.task_id=b.task_id)
where a.task_id=t.id
and b.task_status_id!='2'
) as pendingTask
,c.name as category
,sub.task_id
,t.name
,sub.bucket_date
,sum(sub.alloc_time) as s
from (select rt.task_id
,rt.resource_id
,rt.project_id
,rta.sequence
,rta.alloc_time
,th.creation_date -- assuming creation_date is a DATE datatype?
,rt.start_date
,th.task_status_id
,rt.start_date as bucket_date
from resource_task rt
join resource_task_alloc rta on (rt.task_id = rta.task_id
and rt.resource_id = rta.resource_id
and rt.project_id = rta.project_id)
join task_history th on (rt.task_id = th.task_id)
where th.task_status_id >=2
and rta.alloc_time > 0
and rt.start_date is not null
) sub
join pd_tool_user u on (sub.resource_id = u.id)
join pd_task t on (sub.task_id = t.id)
join pd_project p on (sub.project_id = p.id)
join pd_category c on (t.category_id = c.id)
where sub.start_date >= to_date('2011-11-01','YYYY-MM-DD') -- assuming start_date is a DATE datatype
and sub.creation_date <= to_date('2011-11-31','YYYY-MM-DD') -- assuming creation_date is a DATE datatype
and sub.project_id='355'
group by p.project
,u.username
,p.id
,u.name
,sub.task_id
,t.name
,c.name
,sub.bucket_date
order by u.username
,sub.bucket_date
,p.name
,c.name -
Handling hierarchy in oracle sql query
I have two tables:
The first contains COMPANY_ID and CATEGORY_ID
The second contains CATEGORY_ID and PARENT_CATEGORY_ID
I need to return a list containing COMPANY_ID and CATEGORY_ID. The entries, however, must come not only from the first table, but also include all the descendants listed in the second table.
So for example (and please read the example carefully) - let’s say in the first table I have one row:
COMPANY_ID | CATEGORY_ID
1000 | 1
In the second table, I have three rows:
CATEGORY_ID | PARENT_CATEGORY_ID
1 | null
2 | 1
3 | 2
4 | null
I want to have the following rows as a result:
COMPANY_ID | CATEGORY_ID
1000 | 1
1000 | 2
1000 | 3
How can I do this in oracle 10 in a single SQL statement ?Hi,
Welcome to the forum!
Whenver you have a question, post your sample data in a form that people can use.
CREATE TABLE and INSERT statements are great:
CREATE TABLE first
( company_id NUMBER (4)
, category_id NUMBER (4)
INSERT INTO first (company_id, category_id) VALUES (1000, 1);CREATE TABLE AS is good, too:
CREATE TABLE second
AS
SELECT 1 AS category_id, NULL AS parent_category_id FROM dual UNION ALL
SELECT 2, 1 FROM dual UNION ALL
SELECT 3, 2 FROM dual UNION ALL
SELECT 4, NULL FROM dual;Without that, people can't test their ideas, and are often unsure about your data.
You should also say what version of Oracle you're using. That's especially important with CONNECT BY queries. Every version since Oracle 7 has had significant improvements in how to do CONNECT BY queries. The query below works in Oracle 10 (and up).
For hierarchies and trees, use CONNECT BY
When you have to join tables and use CONNECT BY, it's usually more efficient to do one in a sub-query, and the other in another query.
In the example below, we're doing the CONNECT BY in sub-query cbq, and the join in the main query.
WITH cbq AS
SELECT category_id
, CONNECT_BY_ROOT category_id AS root_category_id
FROM second
START WITH parent_category_id IS NULL
AND category_id IN ( SELECT category_id -- Maybe; see note below
FROM first
CONNECT BY parent_category_id = PRIOR category_id
SELECT first.company_id
, cbq.category_id
FROM first
JOIN cbq ON first.category_id = cbq.root_category_id
;The 2nd condition in the START WITH clause won't change the results any; any rows that are excluded by that condition would also be excluded by the join condition. There can be a big differenece in performance, however. Try the query both ways, and use the 2nd condition if it helps.
Edited by: Frank Kulash on Feb 3, 2010 3:46 PM -
How to tune a oracle sql query to make it run faster ...
Hi Guys,
I am very new to this tuning of sql queries. It is taking a lot of time to run a query and give the output. I have a tool called TOAD[ Tool for oracle application development ] and i ran a query in that tool. Its showing that the query is taking 7sec to get its output. I donno the exact procedure what to follow in order to tune that query. What are these actually like : explain plan, sqltrace, tkprof and how to use these to tune the query. I want my query to run faster like in msec.
Help Appreciated.
Thankshttp://download-east.oracle.com/docs/cd/B10501_01/server.920/a96533/toc.htm
http://download-east.oracle.com/docs/cd/B10501_01/server.920/a96533/sqltrace.htm#1018 -
Loading an Oracle SQL query into an MSSQL table
I have a select query on an Oracle table. I want to load this data into an MSSQL table.
Problem is that I cannot create an interim table on the Oracle database (lack of privelages)
Is there a way to load an Oracle query into the MSSQL table without an interim table?
Many thanks
ZYes,
1) Create an ODI procedure
2) Create a step inside
3) at source tab put the oracle query
4) at target tab put the sql insert code
Refer to the oracle returned values like "#column_name_from_oracle_query"
Make sense?
Cezar Santos
[www.odiexperts.com] -
Convert Oracle SQL query to single column output
Hello All,
I need to build the query to have multiple columns in a single column with multiple rows.
select a.customer_trx_id,a.previous_customer_trx_id
from ra_customer_trx_all a
where a.customer_trx_id = :customer_trx_id
here, a.customer_trx_id and a.previous_customer_trx_id are in two columns. I need to bring them into a single column.
Say: the above output is
a.customer_trx_id a.previous_customer_trx_id
123456 87654
Need to have single column
As
123456
87654
Please do the needful.
Thanks,
AbdulHi,
Post your question in [SQL and PL/SQL|http://forums.oracle.com/forums/forum.jspa?forumID=75] forum, you would probably get a better/faster response.
Regards,
Hussein -
Can any body tell , how the sql select statement will works ? how it will come across the 3 phases 1.pharse 2.execute and 3. fetch
what is the parameter in oracle 9i and 10g for compress the dumpfile in export and import?Hi,
Welcome to forums.. !!
Can any body tell , how the sql select statement will works ? how it will come across the 3 phases 1.pharse 2.execute and 3. fetch Try to refer to : How the select Query Works
Oracle documentation explains every thing, just spend some time.
what is the parameter in oracle 9i and 10g for compress the dumpfile in export and import?You must check the Oracle documentation. From high level the compress parameter works different for both versions while allocations the extents.
- Pavan Kumar N -
Replacing a set of characters from a string in oracle sql query
I want to replace a set of characters ( ~ ! @ # $ % ^ * / \ + : ; | < > ? _ ,) from a STRING in sql
select 'TESTING ! ABC 123 #'
FROM DUAL;
What is the best way to do it? Please provide examples also.What is your expected output... The query I posted just removes them, it never replaces them with spaces..your string already has space.. if you want to remove space as well .. try this...
SELECT TRANSLATE ('TESTING ! ABC 123 #', '-~!@#$%^*/\+:;|<>?_, ', ' ') FROM DUAL;
Output:
--TESTINGABC123
Else post your expected output..
Cheers,
Manik. -
A Oracle sql query is needed, please help me out
Hi,
I have a table similar to scott.emp table and i should get first three recently joined employees salaries according to their joined date from each deptno
output should like this:
deptno firstempsalary secondemplsal thirdemploysalary
10 xxx xxx xxxx
20 xx xx
30 xx xxx xxxx
40 xx
50 xx xxxx xxx
60 xx xx
70 xx
it means that 70 dept having only one employee and 20 dept having only two employees and 10 dept having more than three employees but we should get only three recently joined employees salaries from 10 dept.
Hope this is clear .. please give me a query to get this info ..
oracle is 10g version
great thanks in advance.select deptno,
max(case jd_rank when 1 then salary end) firstempsal,
max(case jd_rank when 2 then salary end) secondempsal,
max(case jd_rank when 3 then salary end) thirdempsal
from
(select salary, deptno, row_number() over(partition by deptno order by join_date desc) jd_rank from emp)
group by deptno
Problem here is what to do when employees have the same join date? This example will arbitrarily choose their order and if, for example, three employees share the same (most recent) join date then they will arbitrarily be classed as "first", "second" and "third". If four employees join at the same time, one of them will be ignored at random!
Edited by: user10548434 on 03-Dec-2008 06:27
Maybe you are looking for
-
Usb device not recognised by windows 7 on hp pavillion dv5094ea
Im having problems since ive updated my HP dv5094ea from windows xp up to windows 7, Since the upgrade my usb ports arent functioning, i keep getting an error bubble saying "Usb device not recognised" - device has malfunctioned ive tried several trou
-
Epson Perfection 2480 photo scanner
Just got a new iMac running Snow Leopard. I have a problem getting my scanner to work, Scanner is a Epson Perfection 2480 Photo. I have down loaded the latest driver from Epson. If I try to run the scanner from finder I get the following message. The
-
Is it possible to increase/decrease pages based on a number selected from a dropdown?
I need to the end user to fill out information on between 1 and 5 properties. Is it possible to increase/decrease the number of identical forms pages based on a dropdown from page one? For example, if the local property manager decides there are 3
-
Metadata access in Jdeveloper 10g using Oracle 9i
Hello everyone. I´m using Jdev 10 g (release 3) and im trying to create a database connection in the connections tab (new database conection wizard) and after complete all steps an brownsing the "tables" item in the navigator(connection itens treevie
-
I designed an interactive pdf form for a company with a reset button. If someone fills it out then sends it to someone, what's stopping the receipient from hitting the reset button and erasing the info the person sent them.