0

我不确定我的标题是否使我提出的问题非常清楚。我正在使用 SQL Developer 来查询 Oracle 数据库。我已经构建了下面的查询,它能够提取一个特定 ID 所需的值。我需要一个查询,该查询将提取相同的值但用于多个 id。我需要每月对一组 20-50 个不同的 id 运行相同的查询。该数据库包含 80,000 多个 ID,因此如果可以避免,我不想提取所有内容。这可能吗?

SELECT b.id, a.final_grade, a.final_score
FROM db.table1 a, db.table2 b
where a.survey_id=b.survey_id
and b.id = '1796'
and a.created_dt = (select max (a.created_dt) from db.table1 a, db.table2 b 
where a.survey_id=b.survey_id and b.cp_id = '1796')

非常感谢你的帮助

4

3 回答 3

1

最简单的方法是使用 ROW_NUMBER() 和 WITH 子句。

with data as 
SELECT 
      b.id, 
      a.final_grade, 
       a.final_score,
       ROW_NUMBER OVER (PARTITION BY b.id, order by a.created_dt desc) rn
FROM 
        db.table1 a
        INNER JOIN  db.table2 b
        ON  a.survey_id=b.survey_id

where
 b.id in (<Comma seperated list of ids>)
)

SELECT * FROM DATA WHERE rn = 1

注意:如果给定 id 的 created_dt 存在关联,则将选择任意行。如果您希望显示领带,请替换ROW_NUMBERRANK()

于 2013-06-27T22:00:29.020 回答
0
  SELECT b.id, a.final_grade, a.final_score
    FROM db.table1 a, db.table2 outerb
    where a.survey_id=outerb.survey_id
    and outerb.id IN (<SELECT query with your list of ids>)
    and a.created_dt = (select max (a.created_dt) from db.table1 a, db.table2 b 
    where a.survey_id=b.survey_id and b.cp_id = outerb.id )

那是你要找的吗?问题不清楚!

于 2013-06-27T21:57:02.630 回答
0

我认为这就是您要寻找的,或者至少这是我从您的解释中得到的:

SELECT b.id, a.final_grade, a.final_score
    FROM db.table1 a, db.table2 b
    where a.survey_id=b.survey_id
    and b.id IN (<SELECT query with your list of ids>)
    and a.created_dt **IN** (select max (a.created_dt) from db.table1 a, db.table2 b 
    where a.survey_id=b.survey_id and b.cp_id = b.id )
于 2013-06-27T22:27:59.240 回答