oracle sql select其他列中值最低的位置

时间:2013-08-01 15:21:32

标签: sql oracle join min

我已经为oracle编写了一个SQL查询,我想选择具有我正在查询的另一个值的每组最低值的记录。我写的东西似乎有用,但我不相信它实际上是正确的。

select cam.visible_id campaign_id, 
  ps.visible_id plate_set_id,
  ps.alias plate_set_name,
  ps.date_created date_created,
  count(distinct(iw.isolate_id)) number_of_clones
from plate_set ps
join campaign cam on cam.id = ps.campaign_id
join plate_plate_set pps on pps.plate_set_id = ps.id
join plate p on p.id = pps.plate_id
join plate_type pt on pt.id = p.plate_type_id
join isolate_well iw on iw.plate_id = p.id
where pt.label='Master Plate'
and ps.date_created = (select min(ps2.date_created) from plate_set ps2 where ps2.campaign_id = cam.id)
group by cam.visible_id, ps.alias, ps.visible_id, ps.date_created
order by cam.visible_id

我不明白我的查询在这里的where子句中是如何工作的:

and ps.date_created = (select min(ps2.date_created) 
from plate_set ps2 
where ps2.campaign_id = cam.id)

在我看来,ps.date_created应该是一个连接,其中ps.date_created =在每个广告系列中创建的第一个广告素材集

类似

join (
select min(plate_set.date_created) as created_date
 from plate_set 
join campaign on plate_set.campaign_id 
group by campaign.id 
) helperTable on helperTable.NotSureHowToJoinLikeThis on ps.date_created

是我实际上正确的,并且sql查询广告系列ID然后过滤了创建日期最短的行,或者有人可以帮助我加入我应该做的更好的写作?

1 个答案:

答案 0 :(得分:0)

我建议你将where子句重写为一个连接的子查询。

...
from plate_set ps
inner join campaign cam on cam.id = ps.campaign_id
inner join plate_plate_set pps on pps.plate_set_id = ps.id
inner join plate p on p.id = pps.plate_id
inner join plate_type pt on pt.id = p.plate_type_id
inner join isolate_well iw on iw.plate_id = p.id
inner join (select min(date_created) mdc, campaign_id from plate_set group by campaign_id) ps2 on  ps2.campaign_id = cam.id
and ps.date_created = ps2.mdc
where pt.label='Master Plate'
...

在where where子句中,将为要比较的每一行执行子查询。作为连接时,查询将对整个数据集运行一次,因此(取决于您的数据和其他因素)这可能会为您提供性能泵