我有一个包含父ID和子ID的数据集,其对应的指标
我的输入日期为
* def included = $Test1/Envelope/Body/getPricePlanResponse/pricePlanSummary/includedService
* def fun = function(){ karate.remove('Test1', '/Envelope/Body/getPricePlanResponse/pricePlanSummary/includedService') }
* eval karate.forEach(included, fun)
* print Test1
我需要根据以下两种情况提取所有记录
输出应为
Parent_ID Parent_InHome_Ind Child_ID Child_InHome_Ind
100 Y 500 Y
100 Y 501 N
201 Y 701 N
201 Y 702 N
301 Y 801 N
我尝试了group by并列出了aggr,但是无法构建Oracle SQL查询。有人可以帮忙吗?
答案 0 :(得分:0)
这应该通过以下自联接聚合查询来解决:
select
t1.Parent_ID,
decode(
min(decode( t2.Child_InHome_Ind, 'N', 0, 1)),
0, 'N', t1.Parent_InHome_Ind
) Parent_InHome_Ind,
t1.Child_ID,
t1.Child_InHome_Ind
from
table t1
inner join table t2 on t2.Parent_ID = t1.Parent_ID
having
count(distinct t2.Child_InHome_Ind) = 1
group by
t1.Parent_ID,
t1.Parent_InHome_Ind
t1.Child_ID,
t1.Child_InHome_Ind
注意:
自我INNER JOIN
允许检索共享同一父对象的所有记录
HAVING BY
子句过滤器仅允许所有具有相同Child_InHome_Ind
的子代(同一父代的所有子代只能出现一个不同的值)
在SELECT
子句中,DECODE
语句处理所有子项的Child_InHome_Ind
等于N
的情况,并因此将列设置为{ {1}}(否则将保留原始值)
答案 1 :(得分:0)
假设您的规则正确而不是示例数据正确,则下面的方法应该起作用。
GMB的答案似乎行得通,但我认为这是一个更简单的查询,也行得通
with table_name as
(select 100 as parent_id, 'Y' as Parent_InHome_Ind, 500 as Child_ID, 'Y' as Child_InHome_Ind from dual
union
select 100, 'Y', 501, 'N' from dual
union
select 201, 'Y', 701, 'N' from dual
union
select 201, 'Y', 702, 'N' from dual
union
select 301, 'Y', 801, 'N' from dual)
select parent_id, decode(Child_InHome_Ind, 'N', 'N', 'Y') as Parent_InHome_Ind, child_id, Child_InHome_Ind from table_name tn
where (select count(distinct child_INHome_ind) from Table_name tn1 where tn.parent_id = tn1.parent_id) = 1;
where子句会过滤出具有子项具有冲突值的任何父项,然后从那里parent_inHomeInd必须与子项inHomeInd匹配,这样简化的解码才能起作用。