在没有子查询的情况下选择oracle中的特定行

时间:2015-02-21 17:29:37

标签: sql oracle oracle11g oracle-sqldeveloper

我有一个非常复杂的查询,我需要一些调用日志,每个调用日志都显示在特定的寄售单行中,所以背后的想法是

ConsignmentID OTHERCOLUMN OTHERCOLUMN CALL_LOG1             CALL_LOG2 

现在问题是我可以使用子查询来做到这一点,但我的调用日志列不能接受子查询中的子查询来获取特定的日志#2和#3

我的表格结构

CALL_LOG
CONSIGNMENTID CALL_LOGID REMARKS

当我在CONSIGNMENT.CONSIGNMENT_ID上有where子句时,我的子查询出现问题,该子句无法识别此子查询

SELECT CONSIGNMENT_ID, ( SELECT REMAKS FROM (   SELECT
    ROW_NUMBER() OVER (ORDER BY DayRangeId ASC) AS rownumber  FROM CALL_LOG WHERE CALL_LOG.CONSIGNMENT_ID = CONSIGNMENT.CONSIGNMENT_ID  )  as temptablename WHERE rownumber = 4 ) as CALL_LOG1,( SELECT REMAKS FROM (   SELECT
    ROW_NUMBER() OVER (ORDER BY DayRangeId ASC) AS rownumberFROM CALL_LOG WHERE 
CALL_LOG.CONSIGNMENT_ID = CONSIGNMENT.CONSIGNMENT_ID  )  as temptablename WHERE rownumber = 3 )  FROM CONSIGNMENT 

1 个答案:

答案 0 :(得分:1)

我假设字段DayRangeId在call_log表中。这个查询对我有用:

with remarks as (
  SELECT CONSIGNMENT_ID cid, remarks,
      ROW_NUMBER() OVER (partition by CONSIGNMENT_ID ORDER BY DayRangeId ASC) AS rn  
    FROM CALL_LOG )
SELECT CONSIGNMENT_ID, r4.remarks call_log4, r3.remarks call_log3
  FROM CONSIGNMENT c
    left join remarks r3 on r3.cid = consignment_id and r3.rn=3
    left join remarks r4 on r4.cid = consignment_id and r4.rn=4