This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

How to combine queries

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic