不同的升序值取决于select中表中的其他属性

时间:2015-12-06 17:24:24

标签: sql oracle select view

我想要下面的表格(如果相关,则来自联接)

 id  | atr  | order | suborder
-------------------------------
  6  | foo  |   3   |   1
  3  | foo  |   4   |   2
  2  | bar  |   1   |   1
  9  | bar  |   2   |   2

成为这个

// Assuming that I have variables like these:
// String uid = "ffffffff-ffff-ffff-ffff-ffffffffffff"; // This is a Minecraft UUID.
// int languageID = 1; // This is a simple Integer
ResultSet set = stm.executeQuery("SELECT language FROM players WHERE UUID = '" + uid + "';");
set.beforeFirst();
while (set.next()){
    // This will run if in the table already contains the UUID = the uid.
    stm.execute("UPDATE players SET language = '" + languageID + "' WHERE UUID = '" + uid +"';");
    return;
}
// This will just run if the resultSet is empty.
stm.execute("INSERT INTO players(UUID, language) values('" + uid + "','" + languageID +"')");

使用SELECT for a VIEW。

我最接近的是使用rownum的子订单栏中的1,2,3,4。但我还没有想出任何将“指数”与另一个属性分开的东西。

怎么可以这样做?

1 个答案:

答案 0 :(得分:1)

使用窗口函数ROW_NUMBER

SELECT id,atr ,"order",
       ROW_NUMBER() OVER(PARTITION BY atr ORDER BY "order") AS suborder
FROM table_name;

LiveDemo

order是关键字,您需要引用它。