sql - How to use order by and rownum without subselect? -


I need to create a query with a command and rownum but without using any novel, first line of query It is necessary to obtain.

In other words, I want to select

  * (Select Camp 1, ORDERCAMP ordering Tableline Order from OrderCap) where rownum = 1;   

but use whithout a substrate Is it possible?

I have Oracle 11. You can say that this is my complete query:

  SELECT T1.CAMP_ID, T2.CAMP (SET OT.CAMP FROM other qualified OT WHERE OT.FK_TO_TABLE1 = T1.CAMP_ID ORTER BY OT .ORDERCAMP) TABLE1 T1, TABLE2 T2 WHERE T1.FK_TO_T2 = T2.PK;   

The subkey provides more than one line, and I can use another subquery such as

  SELECT T1.CAMP_ID, T2.CAMP (SELECT * FROM (SELECT OTHERTABLE OT OT.CAMP OT.FK_TO_TABLE1 = T1.CAMP_ID by order OT.ORDERCAMP) Where ROWNUM = 1) Table1 T1, Table 2 T2 Where T1.FK_TO_T2 = T2.PK;   

Because T1.CAMP_ID is an invalid identifier in the third level subkey.

I select CAMP 1, ORDERCAMP

Hope I have explained myself enough.

Your current query (without the invalid ORDER BY ) becomes ORA-01427: Single-line subquery returns more than one line . You can nest subqueries, but you can only mention one level when you join; Then you have done so:

  select T1.CAMP_ID, T2.CAMP, (Select the camp from FROM (OT.CAMP, OTHERTABLE OT, OT.FK_TO_TABLE1 = T1.CAMP_ID command by OT.ORDERCAMP) where ROWNUM = 1) to TABLE1 T1, TABLE2 T2 WHERE T1.FK_TO_T2 = T2.PK;   

... then you will get ORA-00904: "T1". "CAMP_ID": invalid identifier . So your question, maybe.

You can join the third table instead, and use the analytical ROW_NUMBER () function to assign the line number, and then the external thing to do around the whole thing To find the wrapped selection for the record with only the minimum ORDERCAMP :

  select CAMP_ID, camping, OT_CAMP (select T1.CAMP_ID, T2. Connect with RN to TAB2 T2 as CAMP, OT.CAMP AS OT_CAMP, ROW_NUMBER () order (Order by O.O.ORDERCAMP order by T1.CAMP_ID) TA BLE1 T1 ON T1.FK_TO_T2 = T2.PK OT.FK_TO_TABLE1 = O.T. on T1.CAMP_ID) where RN = 1; Can divide on   

ROW_NUMBER () T1.CAMP_ID primary key value, or anything else that is unique.

, the internal query is also included on your own, so you can see the code RN before the implementation of the external code.

Select another approach, / p>

  T1.CAMP_ID, T2.CAMP, MAX (OT.CAMP) (O.D.ORDERCAMP by DENSE_RANK first order) Table 2 T Joining 2 to OT_CAMP form T1.FK_TO_T2 = T2.PK OT.FK_TO_TABLE1 = T1.CAMP_ID GROUP by T1.CAMP_ID on T1.CAMP_ID, T2.CAMP on T1.CAMP Other Object OT Join on;   

Which is a little shorter and does not require any internal query. I'm not sure there is any real advantage of one on the other.



Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -