Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to combine queries

 
M Wilson
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have 2 sql's and want to know how to combine.
SQLS simplied:

SQL1 =
select A.number, B.sn
from A, B
where A.id = B.id
and B.typecd =' O'

SQL1 returns for example
A.number = 12345Z
B.sn = 2222

SQL2 =
select B.name, A.dob, A.sn
from A, B
where A.id = B.id
and B.typecd = 'P'
and A.number = '12345Z'
and B.sn = '2222'

How can these 2 SQLs be combined to return results for SQL2?
Currently I run SQL1 and take those results and plug into SQL2. Currently OK but could be better?
Thanks!
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the first query requires " B.typecd =' O' " whereas the second requires " B.typecd = 'P' ", so no records can fulfill that. Or is that just a typo?
 
M Wilson
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'O' stands for the organization, 'P' stands for the person/people of the organization.
I want to retrieve data is that an organization, therefore SQL for typecd 'O'
and from result of typecd 'O' get the person's name. I want the person's name that is related to the organization.

For example SQL1 typecd 'O' returns name 'THE TRUST OF JOHN AND MARY DOE'
SQL2 typecd 'P' return 2 rows:
row1: name 'DOE, JOHN' sn='2222'
row2: name 'DOE, JANE' sn='1111'

I want the person's name with sn='2222' for the organization.

Thanks!
 
rohit chavan
Ranch Hand
Posts: 132
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can use union operation as an option.

Before that if you can, please put here sample rows from table A and B both, and rephrase what result you are looking for.


 
K. Tsang
Bartender
Posts: 3585
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
M Wilson wrote:'O' stands for the organization, 'P' stands for the person/people of the organization.
I want to retrieve data is that an organization, therefore SQL for typecd 'O'
and from result of typecd 'O' get the person's name. I want the person's name that is related to the organization.


Given your sql1 is organization O and sql2 is people P. So if I understand you correctly, you want "select sql2 from sql1".

If so the from clause in sql2 will basically be your sql1. But then your subquery sql1 should have all the columns you need in sql2 too. Or you will be joining 3 things making it look messy.

 
M Wilson
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I was looking for and got it figure out a couple of weeks ago, I thought I post and share (it's not the actual SQL but structured as so and worked for what I was trying to do)
it's sql in a sql SELECT...WHERE IN (SELECT...)

SELECT *
FROM A
WHERE A.NUMBER IN (SELECT A.NUMBER FROM B INNER JOIN A ON B.NUMBER = A.NUMBER AND B.TYPECD = 'O' ) AND B.PARTY_CD = 'P'
FETCH FIRST 1 ROWS ONLY

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic