这是我的输入数据:
MyFragment frag1 = new MyFragment() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, ActivityA.class);
MainActivity.this.startActivity(i);
}
};
MyFragment frag2 = new MyFragment() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, ActivityB.class);
MainActivity.this.startActivity(i);
}
};
我希望在单个查询(无子查询或CTE)中为每个不同的CREATE TEMP TABLE test AS SELECT * FROM (VALUES
(1, 12),
(2, 7),
(3, 8),
(4, 8),
(5, 7)
) AS rows (position, value);
分配唯一的编号。但是,我还希望这些数字根据关联的value
提升 - 即,应根据最低position
分配不同的value
号码。
假设:
position
position
不保证每行唯一value
是1-2-3还是3-8-14 所需的输出是:
distinct_values
我可以使用 position | value | distinct_value
----------+-------+----------------
1 | 12 | 1
2 | 7 | 2
3 | 8 | 3
4 | 8 | 3
5 | 7 | 2
为不同的数字编号:
DENSE_RANK
结果显然忽略了SELECT
position,
value,
DENSE_RANK() OVER (ORDER BY value) AS distinct_value
FROM test ORDER BY position;
:
position
这有更好的窗口功能吗?
答案 0 :(得分:2)
with
t(x,y) as (values
(1, 12),
(2, 7),
(3, 8),
(4, 8),
(5, 7)),
pos(i,y) as (select min(x), y from t group by y),
ind(i,y) as (select row_number() over(order by i), y from pos)
select * from ind join t using(y) order by x;