我正在进行一项查询以获取学生联系邮件地址,而且我正处于一个有点困难的地步。我已经设法获得了所有学生及其联系人的列表,但现在当我尝试将联系人加入他们的地址时,我不确定如何获得正确的地址。
在地址表中可以容纳多种地址(家庭,邮件,业务,接送,下载),基本上我需要做的只是每个联系人带回一个地址。
通常这是家庭住址,除非有邮寄地址
所以我的问题是我如何编写某种类型的条件语句来仅获取条目WHERE ADDRESS_TYPE_NAME ='Home',除非对于同一个PERSON_ID还有一个条目WHERE ADDRESS_TYPE_NAME ='Mailing'?
由于
答案 0 :(得分:0)
<ion-card *ngFor="let checklist of checklists;let i=index">
<input type='radio' name="check{{checklist.id}}" value='yes_{{checklist.id}}'> Yes
<input type='radio' name="check{{checklist.id}}" value='no_{{checklist.id}}'> Not passed
<ion-item [hidden]="">//hide when Not passed radio is not checked
<ion-label floating>Comment</ion-label>
<ion-input type="text"></ion-input>
</ion-item>
</ion-card>
答案 1 :(得分:0)
您可以使用
确定地址类型的优先级并获得最高优先级类型select Person_id,
case min(case Address_Type_Name
when 'Mailing' then 1
when 'Home' then 2
-- more
end)
when 1 then 'Mailing'
when 2 then 'Home'
-- more
end Best_Address_Type_Name
from Address_Table
group by Person_id;
然后根据需要将结果加入您的数据
答案 2 :(得分:0)
这是一种方法,使用row_number()
分析函数,不需要显式或隐式的任何连接。它还处理各种特殊情况:既没有邮寄地址也没有家庭住址(但仍需要在输出中显示)的学生,以及另一个有两个邮寄地址的学生(在这种情况下,选择一个随机的;如果有标准,从一个到另一个,可以很容易地调整查询以适应那个。)
with
students ( id, name, address_type, address ) as (
select 11, 'Andy', 'home' , '123 X street' from dual union all
select 11, 'Andy', 'office' , 'somewhere else' from dual union all
select 15, 'Eva' , 'mailing', 'post office' from dual union all
select 18, 'Jim' , 'office' , '1 building' from dual union all
select 30, 'Mary', 'mailing', 'mail addr 1' from dual union all
select 30, 'Mary', 'office' , '1 building' from dual union all
select 30, 'Mary', 'home' , 'her home' from dual union all
select 30, 'Mary', 'mailing', 'mail addr 2' from dual
)
-- End of test data (not needed for the SQL query - reference your actual table)
select id, name, address_type,
case when address_type is not null then address end as address
from (
select id, name,
case when address_type in ('home', 'mailing')
then address_type end as address_type,
address,
row_number() over (partition by id
order by case address_type when 'mailing' then 0
when 'home' then 1 end) as rn
from students
)
where rn = 1
;
ID NAME ADDRESS_TYPE ADDRESS
--- ---- ------------ --------------
11 Andy home 123 X street
15 Eva mailing post office
18 Jim
30 Mary mailing mail addr 1
4 rows selected.