• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

How to combine queries  RSS feed

 
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!
 
Rancher
Posts: 42975
76
  • 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!
 
Ranch Hand
Posts: 133
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.


 
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • 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

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!