I'm very new to SQL, and I am struggling to see what I've missed in this script.
Each time I try to run it, it takes ages and ends up with an error due to insufficient memory.
select r.CONSTITUENT_ID
, case when r.SEX = '1'then 'Male'
when r.SEX = '2' then 'Female' else 'Unknown'end as Gender
, case when r.KEY_INDICATOR = 'O' then 'Organisation'
when r.KEY_INDICATOR = 'I' then 'Individual'end as record_Classification
, case when cc.CODE = '1924' then 'Bristol Affiliated'
when cc.CODE = '1963' then 'Gloucester Affiliated'
when cc.CODE = '2045' then 'Wiltshire Affiliated'
else 'n/a' end as DW_Affiliation
from RECORDS r, CONSTITUENT_CODES cc
This
from RECORDS r,CONSTITUENT_CODES cc`
is cross join what means show me every row from RECORDS with every row CONSTITUENT_CODES
so it returns [rows number from RECORDS]*[rows number from CONSTITUENT_CODES ] rows. You have to use join
or where
to eliminate cross join
from RECORDS r,CONSTITUENT_CODES cc
where r.column = cc.column
or
from RECORDS r
join CONSTITUENT_CODES cc on r.column = cc.column
I don't know your table structure but maybe this will work
select r.CONSTITUENT_ID
, case when r.SEX = '1'then 'Male'
when r.SEX = '2' then 'Female' else 'Unknown'end as Gender
, case when r.KEY_INDICATOR = 'O' then 'Organisation'
when r.KEY_INDICATOR = 'I' then 'Individual'end as record_Classification
, case when cc.CODE = '1924' then 'Bristol Affiliated'
when cc.CODE = '1963' then 'Gloucester Affiliated'
when cc.CODE = '2045' then 'Wiltshire Affiliated'
else 'n/a' end as DW_Affiliation
from RECORDS r, CONSTITUENT_CODES cc
where r.CONSTITUENT_ID = cc.CONSTITUENT_ID
with recommended join style it looks like
select r.CONSTITUENT_ID
, case when r.SEX = '1'then 'Male'
when r.SEX = '2' then 'Female' else 'Unknown'end as Gender
, case when r.KEY_INDICATOR = 'O' then 'Organisation'
when r.KEY_INDICATOR = 'I' then 'Individual'end as record_Classification
, case when cc.CODE = '1924' then 'Bristol Affiliated'
when cc.CODE = '1963' then 'Gloucester Affiliated'
when cc.CODE = '2045' then 'Wiltshire Affiliated'
else 'n/a' end as DW_Affiliation
from RECORDS r
join CONSTITUENT_CODES cc on r.CONSTITUENT_ID = cc.CONSTITUENT_ID
I see in RECORDS
is CONSTITUENT_ID
this column can be use to join with column id from CONSTITUENT_CODES
- I guess CONSTITUENT_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.