0

我已经为 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然后过滤创建日期最低的行,或者有人可以帮助我加入我应该做的更好写吗?

4

1 回答 1

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 子句中,将为您要比较的每一行执行子查询。虽然作为连接,查询将对整个数据集运行一次,因此(取决于您的数据和其他因素)这可能会给您带来性能提升

于 2013-08-01T16:41:42.297 回答