Home » SQL & PL/SQL » SQL & PL/SQL » Help required for SQL query to fetch desired records (oracle 11g/windows)
Help required for SQL query to fetch desired records [message #623497] Wed, 10 September 2014 18:48 Go to next message
gupta27
Messages: 31
Registered: August 2013
Location: Delhi
Member
I have three tables policy_tbl, txn_tbl, price_tbl

structure of these tables are as follows:

create table policy_tbl (
policy_id varchar2(5) not null,
state char(2),  not null );


policy_id is the primary key in policy_tbl and have foriegn key relationship with txn_tbl.

 create table txn_tbl (
Policy_id varchar2(5) not null,
txn_id number(5) not null );


txn_id is primary key in txn_tbl and have foreign key relationship with price_tbl.



 create price_tbl (
txn_id number(5) not null,
price number(3) not null,
state char(2) not null );



From policy_tbl - First I want to fetch policy_ids for which state is not in 'VA' (you can take any state). Then using this policy_id I want to select the max(txn_id) from txn_table (txn_table would have multiple txns corresponding to one policy_id).

Using this txn_id , I want to fetch rows from price_tbl which does not have unique txd_id (multiple records corresponding to one txn_id).

I could write the below query but not sure about how to select only those records from price_tbl which have multiple records corresponding to one txn_id.
select * from price_tbl pr
where pr.txn_id = t.txn_id 
and pr.txn_id =
select max(t.txn_id) from txn_table t 
where t.policy_id =(
select p.policy_id from policy_tbl p
where p.state != 'VA'
and p.policy_id= t.policy_id);


Please help.
Re: Help required for SQL query to fetch desired records [message #623498 is a reply to message #623497] Wed, 10 September 2014 18:51 Go to previous messageGo to next message
manubatham20
Messages: 566
Registered: September 2010
Location: Seattle, WA, USA
Senior Member

use having clause.
Re: Help required for SQL query to fetch desired records [message #623499 is a reply to message #623498] Wed, 10 September 2014 18:55 Go to previous messageGo to next message
gupta27
Messages: 31
Registered: August 2013
Location: Delhi
Member

 
select * from price_tbl pr
where pr.txn_id = t.txn_id 
and pr.txn_id =
select max(t.txn_id) from txn_table t 
where t.policy_id =(
select p.policy_id from policy_tbl p
where p.state != 'VA'
and p.policy_id= t.policy_id)
group by pr.txn_id having count(txn_id) > 10 ;


can you please confirm is this query is correct ?
Re: Help required for SQL query to fetch desired records [message #623500 is a reply to message #623499] Wed, 10 September 2014 18:57 Go to previous messageGo to next message
manubatham20
Messages: 566
Registered: September 2010
Location: Seattle, WA, USA
Senior Member

No, I can't verify, and I highly doubt if this is correct.

Why
count(txn_id) > 10 ;


Multiple means > 1, why > 10?

Manu
Re: Help required for SQL query to fetch desired records [message #623501 is a reply to message #623500] Wed, 10 September 2014 18:59 Go to previous messageGo to next message
gupta27
Messages: 31
Registered: August 2013
Location: Delhi
Member
sorry that was by mistake...wanted to write 1 only...let me write the query again:


select * from price_tbl pr
where pr.txn_id = t.txn_id 
and pr.txn_id =
( select max(t.txn_id) from txn_table t 
where t.policy_id =(
select p.policy_id from policy_tbl p
where p.state != 'VA'
and p.policy_id= t.policy_id))
group by pr.txn_id having count(txn_id) > 10 ;

[Updated on: Wed, 10 September 2014 19:01]

Report message to a moderator

Re: Help required for SQL query to fetch desired records [message #623502 is a reply to message #623501] Wed, 10 September 2014 19:01 Go to previous messageGo to next message
manubatham20
Messages: 566
Registered: September 2010
Location: Seattle, WA, USA
Senior Member

Here we have our limitation.

Either give the data, i.e insert statements for all the tables, covering all the required cases, or you can verify at your end by looking at the results.

Manu
Re: Help required for SQL query to fetch desired records [message #623503 is a reply to message #623502] Wed, 10 September 2014 19:14 Go to previous messageGo to next message
manubatham20
Messages: 566
Registered: September 2010
Location: Seattle, WA, USA
Senior Member

Personally I am not a fan of co-related queries, I try to convert them into join.

You may try below:

CREATE TABLE policy_tbl
(
   policy_id   VARCHAR2 (5) NOT NULL,
   state       CHAR (2) NOT NULL
);


CREATE TABLE txn_tbl
(
   Policy_id   VARCHAR2 (5) NOT NULL,
   txn_id      NUMBER (5) NOT NULL
);


CREATE TABLE price_tbl
(
   txn_id   NUMBER (5) NOT NULL,
   price    NUMBER (3) NOT NULL,
   state    CHAR (2) NOT NULL
);

WITH temp AS (SELECT MAX (t.txn_id) txn_id
                FROM txn_tbl t, policy_tbl p
               WHERE t.policy_id = p.policy_id AND p.state != 'VA')
  SELECT pr.txn_id, COUNT (*)
    FROM price_tbl pr, temp t
   WHERE pr.txn_id = t.txn_id
GROUP BY pr.txn_id
  HAVING COUNT (*) > 1;


Manu

[Updated on: Wed, 10 September 2014 19:15]

Report message to a moderator

Re: Help required for SQL query to fetch desired records [message #623506 is a reply to message #623497] Thu, 11 September 2014 00:13 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
gupta27 wrote on Thu, 11 September 2014 05:18
I have three tables policy_tbl, txn_tbl, price_tbl

structure of these tables are as follows:


OK. Now provide the insert statements and your expected output, without which your test case is incomplete.
Re: Help required for SQL query to fetch desired records [message #623567 is a reply to message #623501] Thu, 11 September 2014 08:58 Go to previous message
joy_division
Messages: 4963
Registered: February 2005
Location: East Coast USA
Senior Member
gupta27 wrote on Wed, 10 September 2014 19:59
sorry that was by mistake...wanted to write 1 only...let me write the query again:


group by pr.txn_id having count(txn_id) > 10 ;


???
Previous Topic: Masking of Customer Data
Next Topic: WHERE name != 'HENRY' excludes name which has NULL
Goto Forum:
  


Current Time: Fri Apr 19 21:01:58 CDT 2024