简体   繁体   中英

Create a subquery in a view SQL- ORACLE

I am setting up a database view.

I have a table PERSON and a RELATION_COMPANY related by the person_id. I amb trying to obtain the min date from the relation_company for each person_id.

I need to use a subquery?

This is what I have tried so far.

CREATE OR REPLACE VIEW PEOPLE_ADDRESS
select
    p.id as ADDRESS_CODE,
    case p.type_
        when 1 then 'JURIDICAL'
        when 0 then 'NATURAL'
    end PERSON_TYPE,
    (select min(start_date) from relation_company where relation_company.person_id = p.id) as INITIAL_dATE
    P.ID as PERSON_CODE,
    p.ADDRESS_NAME as ADDRESS,
    p.POSTAL_CODE as POSTAL_CODE,
    case P.COUNTRY_ID
        when 68 then 'NO'
        else 'YES'
    end FOREIGN_INDICATOR

from
    PERSON p join relation_company r on relation_company = p.id ;

That's my person table:

ID TYPE_ ADDRESS_NAME          NAME             OTHER COLUMNS
1   0    AVENUE SMITH, 19      MICHAEL SCOTT     ....
2   1    OXFORD STREET , 119   COMPANY A          ....
3   1    CHESTHAM ROAD, 7      COMPANY B          ....

That's the RELATION_COMPANY table:

ID      PERSON_ID       START_DATE
1           1           2016-12-12 00:00:00
2           1           1981-07-09 00:00:00
3           2           1991-10-10 00:00:00
4           2           1981-03-09 00:00:00
5           3           1984-07-05 00:00:00
6           3           1981-08-11 00:00:00
7           3           1987-07-09 00:00:00
8           3           2011-07-09 00:00:00

select min(START_DATE) from RELATION_COMPANY group by PERSON_ID;

The result should be:

ADDRESS_CODE  PERSON_TYPE    INITIAL_dATE                 other columns
1             NATURAL        1981-07-09 00:00:00             ....
2            JURIDICAL      1981-03-09 00:00:00               ....
3             JURIDICAL     1981-08-11 00:00:00

I want to get the min(start_date) for each person_id and print it to the view to the column INITIAL_dATE.

I am having trouble to create that query.

You Already have the solution, this query are missing the "person_id" column in your SELECT statement. Change this query

select min(START_DATE) from RELATION_COMPANY group by PERSON_ID;

To this.

select min(START_DATE),PERSON_ID from RELATION_COMPANY group by PERSON_ID;

Hope this helps.

You don't need a subquery for your solution. Only add a group-clause to your query.

like this one:

select
p.id as PERSON_ID,
case p.type_
    when '1' then 'JURIDICAL'
    when '0' then 'NATURAL'
end PERSON_TYPE,
min(start_date) as INITIAL_dATE,
p.ADDRESS_NAME as ADDRESS,
p.POSTAL_CODE as POSTAL_CODE,
case P.COUNTRY_ID
    when 68 then 'NO'
    else 'YES'
end FOREIGN_INDICATOR
from PERSON p join relation_company r on r.person_id = p.id 
group by p.id, type_, ADDRESS_NAME, POSTAL_CODE, P.COUNTRY_ID;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM