Tricky SQL
Hi,
I have a table X that contains data and another table Y that contains user preferences data.
I am trying, without much success so far, to write an sql query which returns all data in table X if there are no preferences in table Y for a specific user and returns only matching data from table X if there are preferences in table Y for a specfic user.
select * from X
where exists (select 1 from Y
where Y.userid = 'aa'
and Y.colA = X.colB)
and X.colD between to_date('20-01-2010', 'dd-mm-yyyy')
and to_date('24-01-2010', 'dd-mm-yyyy');
if the user 'aa' has preferences in table Y then the query works and returns matching data from X but if the user 'aa' has no preferences data in table Y then no data from X is returned at all. I would like to have all the data from X returned in that case.
Any pointers on what i am doing wrong? Pls help as it's urgent.
Thanks.
something like this?
select * from X
where exists (select 1 from Y
where Y.userid = 'aa'
and Y.colA = X.colB)
and X.colD between to_date('20-01-2010', 'dd-mm-yyyy')
and to_date('24-01-2010', 'dd-mm-yyyy')
UNION ALL
select * from X
where not exists (select 1 from Y
where Y.userid = 'aa')
Similar Messages
-
Tricky sql queries for interview preparation
Hi,
I need tricky sql queries as preparation for interview.
pls let me know if anybody has the such queries website address.
or
if you have materials pls mail me on
[email protected]
thnxNKU wrote:
Great Work Kamran....:-)
Regards,
Navneet;)
Kamran Agayev A. (10g OCP)
http://kamranagayev.wordpress.com
[Step by Step install Oracle on Linux and Automate the installation using Shell Script |http://kamranagayev.wordpress.com/2009/05/01/step-by-step-installing-oracle-database-10g-release-2-on-linux-centos-and-automate-the-installation-using-linux-shell-script/] -
I'm trying to do two fairly tricky SQL statements for a class, and been trying for a while and have gotten stuck.
The first one is, "Find Courses where more than half the students got an A"
I've basically been trying to use not exist statements to set it up and then compared the count(sid)/2 > count(sid) query of all A's and am just not getting decent results from it.
The REALLY tricky one is "Find the pairs of students who took exactly the same set of courses"
I can get a cross product from all students with something like this
SELECT s1.sname, s2.sname
FROM lab6_student s1, lab6_student s2
WHERE s1.sname > s2.sname
but after that I can't seem to get it to work properly at all.
Thanks for any pointers/advice!1.
SQL> WITH T
2 AS (SELECT 'mathematics' course_name, 'john' student_name, 'A' grade FROM DUAL
3 UNION ALL
4 SELECT 'mathematics' course_name, 'john1' student_name, 'A' grade FROM DUAL
5 UNION ALL
6 SELECT 'mathematics' course_name, 'john2' student_name, 'A' grade FROM DUAL
7 UNION ALL
8 SELECT 'mathematics' course_name, 'john3' student_name, 'B' grade FROM DUAL
9 UNION ALL
10 SELECT 'physics' course_name, 'john' student_name, 'A' grade FROM DUAL
11 UNION ALL
12 SELECT 'physics' course_name, 'john1' student_name, 'B' grade FROM DUAL
13 UNION ALL
14 SELECT 'physics' course_name, 'john2' student_name, 'B' grade FROM DUAL
15 UNION ALL
16 SELECT 'physics' course_name, 'john3' student_name, 'B' grade FROM DUAL)
17 SELECT *
18 FROM T;
COURSE_NAME STUDE G
mathematics john A
mathematics john1 A
mathematics john2 A
mathematics john3 B
physics john A
physics john1 B
physics john2 B
physics john3 B
8 rows selected.
SQL> WITH T
2 AS (SELECT 'mathematics' course_name, 'john' student_name, 'A' grade FROM DUAL
3 UNION ALL
4 SELECT 'mathematics' course_name, 'john1' student_name, 'A' grade FROM DUAL
5 UNION ALL
6 SELECT 'mathematics' course_name, 'john2' student_name, 'A' grade FROM DUAL
7 UNION ALL
8 SELECT 'mathematics' course_name, 'john3' student_name, 'B' grade FROM DUAL
9 UNION ALL
10 SELECT 'physics' course_name, 'john' student_name, 'A' grade FROM DUAL
11 UNION ALL
12 SELECT 'physics' course_name, 'john1' student_name, 'B' grade FROM DUAL
13 UNION ALL
14 SELECT 'physics' course_name, 'john2' student_name, 'B' grade FROM DUAL
15 UNION ALL
16 SELECT 'physics' course_name, 'john3' student_name, 'B' grade FROM DUAL)
17 SELECT *
18 FROM (SELECT course_name, COUNT (*) total_cnt, SUM (CASE WHEN grade = 'A' THEN 1 ELSE 0 END) grade_cnt
19 FROM T
20 GROUP BY course_name)
21 WHERE grade_cnt > total_cnt / 2;
COURSE_NAME TOTAL_CNT GRADE_CNT
mathematics 4 3
SQL>
2.
SQL> WITH T
2 AS (SELECT '1' course_id, 'john' student_name FROM DUAL
3 UNION ALL
4 SELECT '2' course_id, 'john' student_name FROM DUAL
5 UNION ALL
6 SELECT '1' course_id, 'peter' student_name FROM DUAL
7 UNION ALL
8 SELECT '2' course_id, 'peter' student_name FROM DUAL
9 UNION ALL
10 SELECT '3' course_id, 'king' student_name FROM DUAL
11 UNION ALL
12 SELECT '4' course_id, 'king' student_name FROM DUAL)
13 SELECT *
14 FROM t;
C STUDE
1 john
2 john
1 peter
2 peter
3 king
4 king
6 rows selected.
SQL> WITH T
2 AS (SELECT '1' course_id, 'john' student_name FROM DUAL
3 UNION ALL
4 SELECT '2' course_id, 'john' student_name FROM DUAL
5 UNION ALL
6 SELECT '1' course_id, 'peter' student_name FROM DUAL
7 UNION ALL
8 SELECT '2' course_id, 'peter' student_name FROM DUAL
9 UNION ALL
10 SELECT '3' course_id, 'king' student_name FROM DUAL
11 UNION ALL
12 SELECT '4' course_id, 'king' student_name FROM DUAL),
13 r
14 AS (SELECT student_name, SUM (POWER ( 2, course_id)) val
15 FROM T
16 GROUP BY student_name)
17 SELECT DISTINCT student_name NAME
18 FROM r
19 WHERE EXISTS
20 (SELECT 'x'
21 FROM r r1
22 WHERE r1.val = r.val
23 GROUP BY val
24 HAVING COUNT (*) > 1);
NAME
john
peterEdited by: G. on Feb 24, 2011 2:09 PM -
Tricky SQL query to DUMP to flat file
Hi All,
I have used UTL file utility before to dump table data into a comma seperated file but this time I have a tricky problem.
I have a table called customers with the following fields
work_order_no, customer_no, contract_start_date, title, Name, first_name, street, street_no, bank_code, name, liason_name
and have a products table with following fields
work_order_number, material_type, material_number, qty
1. I need to write the customer details + product details of the customer into a comma seperated file as below
A
1001,
1234,
12.01.2004,
Mr. Dr.,
Alphs,
Thomas,
my street,
12,
1112,
Barclays,
Mr. Dr. Alphs Thomas,
<EOH> - is a fixed
1001,
M,
12,
1,
<EOL> is fixed
1001,
M,
12,
1,
<EOL> is fixed
1001,
M,
12,
1,
<EOF> is fixed
4 - total number of records
1 - total number of A type of records(in the first position
How do i write a Pl/Sql code which can be called from command prompt.
Thanks in advanceYou can try this out and see if it works :
CURSOR to fetch the customers data
Loop
- Use a string to concatenate the cursor variables
- Use chr(10) to print the columns in seperate lines
i.e. lResult := x.work_order_no || ',' || chr(10)
|| x.customer_no || ',' || chr(10)
|| x.contract_start_date || ',' || chr(10)
|| x.liason_name || ',' || chr(10)
- Use Utl_File to write the formatted string to
the output file
- Use Utl_File to write constant '<EOH>' to the
output file
- Get all product details
- For each product details
Loop
lResult := x1.material_type || ',' || chr(10)
|| x1.material_number || ',' || chr(10)
|| x1.Qty || ',' || chr(10)
- Use Utl_File to write the formatted
string to the output file
- Use Utl_File to write constant '<EOL>' to
the output file
End Loop
End Loop
- Use Utl_File to write constant '<EOF>' to the output
file
- Use Utl_File to write counters to the output file
You can write the code in a PL/SQL script file called a.sql and save it on the server directory 'c:\a.sql'.
To run from the command prompt type:
c:\> sqlplus userid/pwd@connectstring @c:\a.sql
Hope this helps.
-- Shailender Mehta -- -
Need help in writing a tricky sql/PL/Sql Procedure
Hi,
I have a Branch table with just two fields in it node1 and Node2, The table looks like this
node1 | node2
-------|---------
1 | 2
3 | 2
3 | 4
4 | 5
1 | 5
1 | 6
5 | 6
in the above data if you draw lines between node_1 and node_2 you will see that there are 3 different paths to get from 1 to 5, as follows
/--------6-------\
1----------------------5
\2------3-------4/
Now I need to Find all the nodes between 1 and 5 or all the possible paths from node 1 to node 5 in this case I have 3 possible paths they are as follows
1-2-3-4-5
1-3-5
1-5
Now I know if this data was stored in a hierarchical manner I can get the results i want by using CONNECT BY and SYS_CONNECT_BY_PATH.
Is there a way to transform this data into a hierarchcal manner so that I can use the CONNECT BY or can somebody give me an Idea as to how to write a PL/SQL procedure to get the result I want.
Any help will be greatly appreciatedHi!
1.Create another table Branch1 to hold 'transformed data'.
2.Use the following procedure to insert data into branch1 table.
declare
cursor n is select * from branch;
begin
for nrec in n loop
if nrec.node1 = 5 then
insert into branch1(node1,node2) values(nrec.node2,nrec.node1);
elsif nrec.node2= 5 then
insert into branch1(node1,node2) values(nrec.node1,nrec.node2);
else
if nrec.node2> 5 then
insert into branch1(node1,node2) values(nrec.node1,nrec.node2);
elsif nrec.node1> 5 then
insert into branch1(node1,node2) values(nrec.node2,nrec.node1);
elsif (nrec.node1 < nrec.node2 ) then
insert into branch1(node1,node2) values(nrec.node1,nrec.node2);
else
insert into branch1(node1,node2) values(nrec.node2,nrec.node1);
end if;
end if;
end loop;
commit;
end;
3.Then execute the following connect by query:
SQL> select lpad(' ',level*2,' ')||node1 from branch1
2 start with node1=1
3 connect by prior node2=node1;
Hope this is satisfactory.
Thanks! -
Tricky SQL query... how to get all data in a single query?
create table employee_definition (def_id number, def_name varchar(50));
insert into employee_definition values (100, 'EMAIL');
insert into employee_definition values (200, 'MOBILE_PHONE');
insert into employee_definition values (300, 'HOME_PHONE');
SQL> select * from employee_definition;
DEF_ID DEF_NAME
100 EMAIL
200 MOBILE_PHONE
300 HOME_PHONE
create table employee_data (def_id number, def_value varchar(20), emp_id number);
insert into employee_data values (100, '[email protected]', 123);
insert into employee_data values (200, '01232222', 123);
insert into employee_data values (300, '5555', 123);
insert into employee_data values (100, '[email protected]', 666);
insert into employee_data values (200, '888', 666);
insert into employee_data values (300, '999', 666);
insert into employee_data values (300, '444', 777);
SQL> select * from employee_data;
DEF_ID DEF_VALUE EMP_ID
100 [email protected] 123
200 01232222 123
300 5555 123
100 [email protected] 666
200 888 666
300 999 666
300 999 777
7 rows selected.I'm supposed to create a SQL that will return me the email, mobile_phone, and home_phone for a set of employees. The result will be something like this:
EMPLOYEE ID | HOME_PHONE | MOBILE_PHONE | EMAIL
123 | 5555 | 01232222 | [email protected]
666 | 999 | 888 | [email protected]
777 | 444 | null | nullThe thing I'm finding difficulty here is that the same column is used to store different values, based on the value in employee_definition table (something like a key/value pair). If I do:
SQL> select emp_id, def_value as email from employee_data, employee_definition
2 where employee_data.def_id = employee_definition.def_id
3 and employee_definition.def_name = 'EMAIL';
EMP_ID EMAIL
123 [email protected]
666 [email protected]'s partially ok.. I'm just getting the definition for 'EMAIL'. But how can I get all the values in a single query, knowing that the column stores different values based on def_name?Oh no, not again.
Entity attribute models always seem like a great idea to people who have been in the profession for five minutes and lack any kind of fundamental knowledge.
It staggers me that someone with 2,345 posts still believes "you need a 'detail table' for [storing multiple telephone numbers]"
"A person can have multiple telephone numbers" is not an excuse to build a tired person_attribute table. Niether is the bizarre proposal by someone with over 4k posts who should know better in an earlier post that EAV models are necessary to support temporal fidelity.
Taken to it's logical conclusion, EAV modelling leads to just two application tables. THINGS and THING_ATTRIBUTES. And when you consider that a THING_ATTRIBUTE is also a THING, why not roll those two tables up into one also? Hmmm, what does THINGS and THING_ATTRIBUTES look like? I know, TABLES and COLUMNS. Who would've guessed? SQL already provides the completely flexible extensible attribute model the advocates of EAV proscribe. But it also has data types, physical data independence, constraints and an efficient query language which EAV does not.
EAV modelling errodes the semantics of the attributes which are bundled into the "attribute" table.
There is no point in storing 12 different phone numbers with implied functional dependency to unconstrained and often repeating notional attributes like "MOBILE", "LANDLINE", "WORK", err, "WORK2", err, "MOBILE2", err, ... when this phone type attribute has no semantic value. When you want to call someone, you invariably want to retrive the prefered_phone_number which may depend on a time of day, or a call context.
These things need to be modelled properly (i.e normalised to BCNF) within the context of the database. -
Hi all,
Please see the example and help me out is there any solution to get below Required output.
with t as (select '61527' as cp_Code,'RELEASE SWITCH' as Cp_Des,'88967095' as Part_No,'1800' as List_Price from dual union all
select '61527' as cp_Code,'RELEASE SWITCH' as Cp_Des,'88967095' as Part_No,'909' as List_Price from dual union all
select '61527' as cp_Code,'RELEASE SWITCH' as Cp_Des,'89018180' as Part_No,'4071.53' as List_Price from dual union all
select '61527' as cp_Code,'RELEASE SWITCH' as Cp_Des,'89018180' as Part_No,'5755.25' as List_Price from dual union all
select '61527' as cp_Code,'RELEASE SWITCH' as Cp_Des,'TEST1' as Part_No,'100' as List_Price from dual)
select * from t
Output of the above query
cp_Code Cp_Des Part_No List_Price
61527 RELEASE SWITCH 88967095 1800
61527 RELEASE SWITCH 88967095 909
61527 RELEASE SWITCH 89018180 4071.53
61527 RELEASE SWITCH 89018180 5755.25
61527 RELEASE SWITCH TEST1 100
Required output like this
cp_Code Cp_Des Part_No List_Price
61527 RELEASE SWITCH 88967095 1800
909
89018180 4071.53
5755.25
TEST1 100
Thanks in advance .Hi,
As Karthick said, the Presentation Layer is the best place to handle formatting like this.
For example, in SQL*Plus you can use the BREAK command:
BREAK ON cp_code NODUPLICATES ON cp_des NODUPLICATES ON part_no NODUPLICATES
SELECT *
FROM t
ORDER BY cp_code
, cp_des
, part_no
, list_price
;Output:
CP_CO CP_DES PART_NO LIST_PR
61527 RELEASE SWITCH 88967095 1800
909
89018180 4071.53
5755.25
TEST1 100If you want the rows in the order in which you posted them, just change the ORDER BY clause.
This site notmally won't display multiple spaces (or tabs) together.
When posting formatted text (like your output), type these 6 characters:
(all small letters, inside curly brackets) before and after formatted sections. -
Tricky SQL - is it possible using just SQL and no PL/SQL
Hello everyone,
Here is my data
create table experience (exp_id number(10), exp_name varchar2(20), root_exp_id number(10));
insert into experience values (642, 'Test', 5172);
create table experience_node (exp_node_id number(10), exp_node_type_id number(2));
insert into experience_node values (5172, 1);
insert into experience_node values (5173, 2);
insert into experience_node values (5174, 2);
create table experience_connector (exp_conn_id number(10), exp_conn_type_id number(2), exp_node_id number(10), parent_exp_node number(10) );
insert into experience_connector values (4287, 1, 5173, 5172);
insert into experience_connector values (4288, 2, 5174, 5173);
insert into experience_connector values (4289, 2, 5175, 5174);
When we run these queries:
select parent_experience_node_id
from experience_connector
where experience_node_id = 5175;
select parent_experience_node_id
from experience_connector
where experience_node_id = 5174;
select parent_experience_node_id
from experience_connector
where experience_node_id = 5173;
select parent_experience_node_id
from experience_connector
where experience_node_id = 5172;
gives 5174, 5173, and 5172 and null. exp_node_type_id = 1 and exp_node_conn_type_id = 1 indicate that it is at the root level. Hope my explanation is helpful. What I am trying to get is - to get great great grant parent for 5175 which is 5172. Is this possible to do this with SQL?
Thank you so much for you help.
Best,
LakshmiWe can use "model clause" ;-)
create table T (id number(10), parentID number(10) );
insert into T values (5173, 5172);
insert into T values (5174, 5173);
insert into T values (5175, 5174);
commit;
select parentID
from T
model RETURN UPDATED ROWS
dimension by(id)
measures(parentID)
rules(
parentID[9999] = parentID[5175],
parentID[9999] = parentID[parentID[9999]],
parentID[9999] = parentID[parentID[9999]]);
PARENTID
5172
or
select parentID
from T
model RETURN UPDATED ROWS
dimension by(id)
measures(parentID)
rules(
parentID[9999] = parentID[parentID[5175]],
parentID[9999] = parentID[parentID[9999]]); -
Help me with this tricky sql....
i need a sql querry that displays ename,salary,max(salary) such that max(salary) should be in every column.
it should be like this...
Ename Salary max(Salary)
Smith 12000 18000
robert 8000 18000
frank 14000 18000
walter 18000 18000
stacy 5000 18000
thanks
sankar..Hi, Sankar,
That's exactly what analytic functions do:
SELECT ename
, salary
, MAX (salary) OVER () AS max_salary
FROM table_x;"OVER (<analytic_clause>)" indicates that this is the analytic MAX function, not the aggregate MAX function.
You don't need an analytic clause in this case, but syntax still requires the parentheses in "OVER ()". -
Hi,
I've got 3 columns X,Y & Z.
Each column can have the values 'A', 'B' or NULL.
If 2 out of 3 have an 'A' I want to get 'TEXT1', if 2 out of 3 have a 'B' I want to get 'TEXT2' in all other cases I want to 'TEXT3'.
I'm using 8.1.7.0.0 and only direct SQL is allowed (so no own functions or procedure's).
Can anybody help me please?create table test(x varchar2(1), y varchar2(1), z varchar2(1));
delete from test ;
insert into test values('A', 'A', 'A');
insert into test values('A', 'A', 'B');
insert into test values('A', 'A', null);
insert into test values('A', 'B', 'A');
insert into test values('A', 'B', 'B');
insert into test values('A', 'B', null);
select
min(decode (zcount,2,decode(zcol,'A','TEXT1','B','TEXT2','TEXT3') ,'TEXT3'))
from
(select zcol, count(*) zcount, zrowid
from
(select x zcol , rowid zrowid from test
union all
select y zcol , rowid zrowid from test
union all
select z zcol , rowid zrowid from test
group by zcol, zrowid
) AAA
group by zrowid
TEXT3
TEXT1
TEXT1
TEXT1
TEXT2
TEXT3 -
** Consider the following table; we'll name this 'entities' for
the following discussion.
ENTITY FORM DEPTH EQUIV SOURCE
100012802307W502 DVNU 1458 14399 GOVT
100012802307W502 DVNU 1458 14399 IPL
100012802307W502 PLSR 1455 14399 GOVT
100012802307W502 PLSR 4238 14399 wayrig
100012802307W502 PLSR 4450.4 14399 GOVT
100012802307W502 PLSR 4788.3 14399 GOVT
101070403227W300 BGVL 800 14399 GOVT
101070403227W300 DVNU 855.3 14390 IPL
101070403227W300 DVNU 855.6 14399 GOVT
** The goal
For each unique ID (only 2 in the above example table: 100012802307W502
and 101070403227W300)
We need to get a single 'formation pick'.
Without going into details, 'formation pick' is an industry term and
in our example, a formation pick is identified by the FORM attribute.
** The Problem
Given the above, the question which I need to answer is
for each unique ENTITY, I want to get the PLSR formation pick.
If for any ENTITY, there is not a PLSR pick, but there is
an EQUIVALENT pick then for that ENTITY, we want that equivalent pick
(in our example above, all FORM which have the same EQUIV value are
considered equivalent; thus for 100012802307W502, there are PLSR picks
while for 101070403227W300, there are not but there are equivalent
picks (DVNU and BGVL)
The picks are to be selected based on a priority
top priority is given to SOURCE wayrig
If a given pick does not have a wayrig, SOURCE, then accept IPL
Finally, if neither the wayrig, or IPL SOURCE are available,
accept the GOVT pick
Thus, for 100012802307W502, where there are multiple PLSR picks, we
want the one with the highest available priority
(the 100012802307W502 PLSR 4238 14399 wayrig)
row
For 101070403227W300, which does not have a PLSR pick, we need to consider
the qualifying equivalent picks and the highest applicable priority for
these picks is where SOURCE = GOVT -> rows
101070403227W300 BGVL 800 14399 GOVT
101070403227W300 DVNU 855.6 14399 GOVT
Thus, the final condition that
should there be 2 or more picks whose priority is the same, then we
want the one with the shallowest DEPTH
row
101070403227W300 BGVL 800 14399 GOVT
Thus the desired final results
ENTITY FORM DEPTH EQUIV SOURCE
100012802307W502 PLSR 4238 14399 wayrig
101070403227W300 BGVL 800 14399 GOVT
** Constraints
It is most desireable to achieve this without having to create
physical tables and/or views as the query(ies) must run against
a public database where users do not have create table privileges.
If anyone has solutions that include physical tables, please submit
these anyways as they may help us devise the ultimate approach that
we are seeking.
More info (in case this is helpful)
I have been working on this and variations of this problem for
a little while.
Here is a query that was submitted to me via this discussion group
sometime ago on a similar problem
select ENTITY, FORM, MIN(DECODE ( SOURCE,'wayrig',1,'IPL',2, 3)) PICK_PRIORITY
from entities
where EQUIV = '14399'
GROUP BY ENTITY, FORM
ENTITY FORM PICK_PRIORITY
100012802307W502 DVNU 2
100012802307W502 PLSR 1
101070403227W300 BGVL 3
101070403227W300 DVNU 3
as you can see, the above query gets me partially where I need to go.
Unfortunately, I have not been able to adapt the above query in such a
way as to get the rest of the information related to the selected ENTITY;
that is, the DEPTH, EQUIV, and SOURCE values are not present in the
selected data.
And of course, the above is only a piece of the puzzle as it does not
get me a single row per selected ENTITY.
Thanks for your consideration
null"bounoe",
I am a little bit confused, because what you are asking for and the sample result seem to be in slight conflict. What you ask for does not require that equiv = '14399', but your results seem to.
So, if equiv must equal '14399', then
SELECT DISTINCT entity, form, depth, equiv, source
FROM entities
WHERE equiv = '14399'
AND (entity,
DECODE(form,'PLSR',1,2),
depth,
DECODE(source,'wayrig',1,'IPL',2,'GOVT',3,4)) IN
(SELECT entity,
DECODE(form,'PLSR',1,2),
MIN(depth),
DECODE(source,'wayrig',1,'IPL',2,'GOVT',3,4)
FROM entities
WHERE equiv = '14399'
AND (entity,
DECODE(form,'PLSR',1,2),
DECODE(source,'wayrig',1,'IPL',2,'GOVT',3,4)) IN
(SELECT entity,
DECODE(form,'PLSR',1,2),
MIN(DECODE(source,'wayrig',1,'IPL',2,'GOVT',3,4))
FROM entities
WHERE equiv = '14399'
AND (entity,DECODE(form,'PLSR',1,2)) IN
(SELECT entity,
MIN(DECODE(form,'PLSR',1,2))
FROM entities
WHERE equiv = '14399'
GROUP BY entity)
GROUP BY entity,DECODE(form,'PLSR',1,2))
GROUP BY entity,
DECODE(form,'PLSR',1,2),
DECODE(source,'wayrig',1,'IPL',2,'GOVT',3,4));
ENTITY FORM DEPTH EQUIV SOURCE
100012802307W502 PLSR 4238 14399 wayrig
101070403227W300 BGVL 800 14399 GOVT
If equiv need not equal '14399':
SELECT DISTINCT entity, form, depth, equiv, source
FROM entities
WHERE (entity,
DECODE(form,'PLSR',1,2),
depth,
DECODE(source,'wayrig',1,'IPL',2,'GOVT',3,4)) IN
(SELECT entity,
DECODE(form,'PLSR',1,2),
MIN(depth),
DECODE(source,'wayrig',1,'IPL',2,'GOVT',3,4)
FROM entities
WHERE (entity,
DECODE(form,'PLSR',1,2),
DECODE(source,'wayrig',1,'IPL',2,'GOVT',3,4)) IN
(SELECT entity,
DECODE(form,'PLSR',1,2),
MIN(DECODE(source,'wayrig',1,'IPL',2,'GOVT',3,4))
FROM entities
WHERE (entity,DECODE(form,'PLSR',1,2)) IN
(SELECT entity,
MIN(DECODE(form,'PLSR',1,2))
FROM entities
GROUP BY entity)
GROUP BY entity,DECODE(form,'PLSR',1,2))
GROUP BY entity,
DECODE(form,'PLSR',1,2),
DECODE(source,'wayrig',1,'IPL',2,'GOVT',3,4));
ENTITY FORM DEPTH EQUIV SOURCE
100012802307W502 PLSR 4238 14399 wayrig
101070403227W300 DVNU 855 14390 IPL
If neither of these is exactly what you need, please let me know.
Barbara
null -
Does anyone have any suggestions on how to load a text file with the following layout:
1SYSTEM: TMS1 STATE OF DISMAY DATE: 10/23/06
REPORT: B155A761 DEPARTMENT OF CONFUSION TIME: 15:05
EMPL ID: AAF*** ACTUAL FTE'S/TOTAL COSTS BY EMPL NAME PAGE: 1
SPR IDX: F**** FOR PERIOD 07/06 THRU 08/06
JOB/TASK: F****/F****
0PROGRAM (F00) DEPARTMENT OF CONFUSION ACTUALS THRU 08/06
0SUPER EMPL ---- MONTHS 07/06 THRU 08/06 ------ ---------- BIENNIUM TO DATE -----------
INDEX NAME ID JOB TASK REG OT STAFF MO COST REG OT STAFF MO COST
0F1150 XYZ, KELLY J. 123454 FJO1A FTO5A 284.0 1.63 6,688.22 735.0 4.23 17,296.52
FJO1A FTO5D 38.0 .22 893.91 90.0 .52 2,128.73
FJO1A FTW5T 6.0 .03 135.07 6.0 .03 135.07
0 * XYZ, KELLY J. 123456 328.0 1.88 7,717.20 831.0 4.78 19,560.32
0 ABC, ALICE M. 234567 FJB1A FTB5A 3.0 .02 107.83 3.0 .02 107.83
FJB1A FTB5B 21.5 .14 881.81
FJB1A FTB5D 5.5 .03 194.37
FJB1A FTB5G 5.5 .03 192.11 22.0 .11 790.06
FJB1A FTW5U 1.0 .01 41.20
FJG1N FTG5C 17.0 .11 700.26
FJG1N FTG5E 15.5 .09 557.19 15.5 .09 557.19
FJG1N FTW5A 1.0 35.95 1.0 35.95
FJG1N FTW5G 1.5 .01 61.79
FJG1N FTW5H 1.0 .01 41.20
FJG1N FTW5T 1.0 35.95 3.0 .01 118.34
FJG1N FTW5U 5.0 .03 205.96
FJG1Q FTG5C 2.0 .01 70.69
FJG1V FTG5E 64.0 .33 2,140.75 64.0 .33 2,140.75
FJG2A FTG5C 2.0 .01 70.69
FJG2A FTW5E 1.0 .01 41.20
FJG2A FTW5J 9.0 .05 370.75
FJG2A FTW5T 5.5 .03 197.72 5.5 .03 197.72
FJO1A FTO5D 219.0 1.14 7,587.85 432.0 2.34 15,578.73
FJO1A FTW5E 1.0 .01 41.20
FJO1A FTW5G 1.0 35.95 1.0 35.95
FJO1A FTW5T 65.5 .37 2,507.55
FJO1A FTW5U 3.0 .02 106.00
FJO1A FTW5V 34.5 .19 1,203.74 84.5 .49 3,103.17
FJO1A FTW5W 2.0 .01 66.30 6.0 .04 219.51
0 * ABC, ALICE M. 234567 352.0 1.84 12,161.34 773.5 4.28 28,219.87
0 CDE, GEORGE 345678 FJB1A FTB5B 145.0 .82 5,390.09 536.0 3.05 19,574.79
0 EFG, MARGARET 456789 FJB1A FTB5B 138.0 .86 4,259.44
FJG1N FTG5E 7.0 .04 191.76
FJG1N FTW5G 1.0 27.38
FJG1N FTW5V 1.0 27.38
FJG1Q FTG5E 2.0 .01 54.78
FJG1Q FTG5F 4.0 .02 109.56
FJG1Q FTW5B 1.0 .01 31.48
1SYSTEM: TMS1 STATE OF DISMAY DATE: 10/23/06
REPORT: B155A761 DEPARTMENT OF CONFUSION TIME: 15:05
EMPL ID: AAF*** ACTUAL FTE'S/TOTAL COSTS BY EMPL NAME PAGE: 2
SPR IDX: F**** FOR PERIOD 07/06 THRU 08/06
JOB/TASK: F****/F****
0PROGRAM (F00) DEPARTMENT OF CONFUSION ACTUALS THRU 08/06
0SUPER EMPL ---- MONTHS 07/06 THRU 08/06 ------ ---------- BIENNIUM TO DATE -----------
INDEX NAME ID JOB TASK REG OT STAFF MO COST REG OT STAFF MO COST
0F1150 EFG, MARGARET 456789 FJG1Q FTW5G 9.0 .05 279.29
FJG1Q FTW5V 6.0 .03 180.76
0 * EFG, MARGARET 456789 169.0 1.02 5,161.83
0 HIJ, KEN H. 567890 FJG1B FTW5F 120.0 .71 4,226.34 324.0 1.86 10,868.58As you can guess, the first 5 lines need to be skipped everytime they appear. I am sure there is a fairly simple approach to this, but it is escaping me at this time.
I thought about loading the table into a single column table and then trying to write a query that ignores these lines and then parse out the remaining text into columns. But that seems a little convoluted and prone to inconsistencies.Yeah, I figured I would just use this technique to fill in the data. I knew that part of the challenge ahead of time. Apparently this file comes from a mainframe and getting the output changed isn't an option.
I thought about the skip=n option, but as you stated, that doesn't help me with the subsequent headers.
I was hoping to avoid scripting an additional parser, but it is looking more and more unlikely that I will be able to skip that part. -
Tricky SQL helpDates between dates
hi,
I need to return all months that fall between a date range. So for example,
Date range = 01-JAN-1999 to 01-JUN-1999
Should return JAN,FEB,MAR,APR,MAY,JUN
Any ideas on this one folks ?
Yhanks.
NYou can use BETWEEN
where col_date between to_date('01-JAN-1999','DD-MON-YYYY') and to_date('30-JUN-1999','DD-MON-YYYY')
hi,
I need to return all months that fall between a date range. So for example,
Date range = 01-JAN-1999 to 01-JUN-1999
Should return JAN,FEB,MAR,APR,MAY,JUN
Any ideas on this one folks ?
Yhanks.
N -
Tricky SQL Query.. help
Hi all,
I am new with this forum, will be here often. i have a question about sq query that i need to run here.
OK. the table
customer
CID_ID CREATE_DT
0000000029003242 10-JUN-12
0000000029003074 10-JUN-12
0000000029003191 10-JUN-12
0000000029002097 09-JUN-12
0000000029004443 12-JUN-12
0000000028975367 10-JUN-12
0000000029004178 11-JUN-12
0000000028998641 07-JUN-12
0000000029003191 10-JUN-12
0000000028998641 07-JUN-12
10 rows selected
the question :
ok, for the result i wanted 2 column which is [cid_id, count(cid_id)] and it must be ordered by create_dt. i know it need group by and order by. i'm ok with using group by or order by them self, but i couldn't get these running at the same time beautifully.
anybody please help me, i have workied on this for 3 days already still i cannot see how i can do this./* Formatted on 8/16/2012 2:51:53 PM (QP5 v5.139.911.3011) */
WITH tmp
AS (SELECT '0000000029003242' cid_id, '10-JUN-12' create_dt FROM DUAL
UNION ALL
SELECT '0000000029003074', '10-JUN-12' FROM DUAL
UNION ALL
SELECT '0000000029003191', '10-JUN-12' FROM DUAL
UNION ALL
SELECT '0000000029002097', '09-JUN-12' FROM DUAL
UNION ALL
SELECT '0000000029004443', '12-JUN-12' FROM DUAL
UNION ALL
SELECT '0000000028975367', '10-JUN-12' FROM DUAL
UNION ALL
SELECT '0000000029004178', '11-JUN-12' FROM DUAL
UNION ALL
SELECT '0000000028998641', '07-JUN-12' FROM DUAL
UNION ALL
SELECT '0000000029003191', '10-JUN-12' FROM DUAL
UNION ALL
SELECT '0000000028998641', '07-JUN-12' FROM DUAL)
SELECT cid_id, create_dt, cnt
FROM (SELECT cid_id,
create_dt,
COUNT (
cid_id)
OVER (PARTITION BY cid_id
ORDER BY TO_DATE (create_dt, 'dd-Mon-yy'))
cnt,
ROW_NUMBER ()
OVER (PARTITION BY cid_id
ORDER BY TO_DATE (create_dt, 'dd-Mon-yy'))
rn
FROM tmp)
WHERE rn = 10000000028975367 10-JUN-12 1
0000000028998641 07-JUN-12 2
0000000029002097 09-JUN-12 1
0000000029003074 10-JUN-12 1
0000000029003191 10-JUN-12 2
0000000029003242 10-JUN-12 1
0000000029004178 11-JUN-12 1
0000000029004443 12-JUN-12 1
Edited by: Indra Budiantho on Aug 16, 2012 12:53 AM -
Hi all,
I have a tricky SQL statement below:
SELECT field_x FROM TABLE B WHERE field_y=0;
will generate result = ABC. ABC is a field IN TABLE A
However WHEN I try TO USE the below query:
SELECT (SELECT field_x FROM TABLE B WHERE field_y=0) FROM TABLE A;
it gives me a different result (All records is ABC), compare to the below query result that I want:
SELECT (ABC) FROM TABLE A;
Please advise.
Thanks.> I appreciate if you please give the some dos and
donts which i have to keep in mind ( except from the
business requirement) while designing database
structure for an application.
My 2c's.
Relational design works. It is mature. It is well documented. It works. Thus if you step outside the bounds of relational design, you must question WHY and make sure that answers are sound and logical. (obviously I do not refer to OLAP designs here, before someone jumps on my case ;-) )
That was the 1st cent. Logical design.
The 2nd cent says that know Oracle. Physical implementation of the logical design. Know Oracle concepts and fundamentals and features. E.g. a partitioned table can make the difference between a system that still works next year because it was able to scale with volumes.
It should be this simple.
Maybe you are looking for
-
What's wrong with the Zen Nano pl
I've been looking around for the MP3 player that will suit my needs. The key features I need are... . Above average sound quality. 2. Flash based. 3. Small. Can accompany me anywhere with ease. 4. User replaceable battery. (after few years, built-in
-
OSB11g - Business Services - Calling WS throwing reader() error.
I have an OSB11g Proxy and business services. my requirement is to get a WS SOAP request from a BPEL application on to my Proxy services. this proxy service will identify the URI to invoke and make a call on to BES via this business service with the
-
AP - Late Payments - Interest Due Report
Is there a report that will provide you with a list of Invoices that are expected to pay interest over the next 10/15/20 days, etc., Note: like an Aging Report but will report interest that may be coming up due.
-
How to transform complex types
I am trying to use the tranformation tool to map a internal 'SalesOrder' object to an external return object that has a slightly different structure. My SalesOrder object has a property of type Set which holds one-to-many SaleItem objects. In the tra
-
Error message processing transaction in F.63
Hello, I have a program which runs transaction F.63, thus upload of mass data which end up as parked document and thereafter posted to FI. On a screen, I get this error message "Only GL accounts can be entered on the fast entry screen". Even though t