SQL query to retrieve data from multiple rows

advertisements

Hi I have the requirement to traverse three different tables of Oracle database and fetch data.

I will pass countyId to get all zipCodes from ZIP table. Based on the number of zipCodes retrieved from ZIP table, I will have pass each zip code to verify it in another table. Based on this zipCode the ACTIVITY table will return the rows of that zipCode.

How could I write the SQL query to achieve this. I heard virtual table creation concept is there but I am non-knowledgeable on that.

Please provide me high level view, How could I proceed...?

I tried the below query, but no results are retrieved - 0 rows

select T_ZIP5.zip,
  T_DTV_CNTY_ELIGIBILITY.DTV_eligible,
  T_USER_ACTIVITY.cuid,
  T_USER_ACTIVITY.actvty_date
from T_ZIP5
join T_DTV_CNTY_ELIGIBILITY
  on T_ZIP5.FIPS_CNTY=T_DTV_CNTY_ELIGIBILITY.CNTY_FIPS
join T_USER_ACTIVITY
  on T_DTV_CNTY_ELIGIBILITY.zip_code=T_USER_ACTIVITY.zip
where T_ZIP5.FIPS_CNTY='53033'

Table Structures:

desc t_zip5

----------------- -------- ------------
ZIP               NOT NULL VARCHAR2(5)
FIPS_CNTY                  VARCHAR2(5)
CITY_NAME         NOT NULL VARCHAR2(50)
STATE             NOT NULL VARCHAR2(2)
RATE_CENTER_NAME           VARCHAR2(10)
RATE_CENTER_STATE          VARCHAR2(2)

desc T_DTV_CNTY_ELIGIBILITY

------------- -------- ------------
CNTY_FIPS     NOT NULL VARCHAR2(5)
ZIP_CODE      NOT NULL VARCHAR2(5)
DTV_ELIGIBLE           CHAR(1)
USER_MODIFIED          VARCHAR2(10)
CREATED_DATE           DATE
MODIFIED_DATE          DATE

desc T_USER_ACTIVITY

----------- ---- -------------
ACTVTY_DATE      DATE
ACTVTY_NAME      VARCHAR2(20)
ACTVTY_DSCR      VARCHAR2(200)
CUID             VARCHAR2(10)
ZIP              VARCHAR2(5)

53033 is countyCode

The Zip for county is present in T_USER_ACTIVITY table. But there are multiple zip entries in T_ZIP5 table for the given county 53033.


You can use the join query for this. Suppose you have three table 1. country (country_id) 2. zip (zip_id,country_id,zipcode) 3. test (test_id,zipcode);

So query like

SELECT zip.zipcode
  FROM country
    JOIN zip on zip.country_id = country.country_id
    JOIN test on test_id.zipcode = zip.test_id
  Where country.country_id = 10

Here you can use left,right join depend on your record need and you zipcode will be one entry other wise you get result as multiple records.