我如何 return 具有相同排名的单个 orderid?
How do I return single orderid having same rank?
我有以下输出:
Orderid
Time
State
Order_rank
1
10.15
mfr
1
1
10.15
delivered
1
2
12.10
picked
1
2
12.10
mfr
1
这里我已经将订单ID排序到最新时间,并将条件放在where子句“where order_rank = 1”中,但我想return一个orderjobid的单个条目,即对于 orderid 1,我想要 return 交付状态条目,对于 orderid 2,我想要 mfr 状态条目等。
这只是一个示例数据,我有1000多个orderids,我如何return单次进入同一排名?
扩展您的排名,不仅包括时间,还包括状态表达,例如。
Order time desc,
case state when 'picked' then 1
When 'mfr' then 2
When 'delivered' then 3
...
End asc
这样我们为每个州分配了排序顺序 1,2,3。
根据@simeon-pilgrim 的评论进行编辑以使其正确(感谢)
假设您的代码如下所示:
SELECT * FROM (
SELECT
order_id,
time,
state,
RANK() over (partition by order_id order by time) as order_rank
FROM VALUES
(1,'10.15','mfr'),
(1,'10.15','delivered'),
(2,'12.10','picked'),
(2,'12.10','mfr')
v(order_id, time, state)
)
WHERE order_rank = 1;
它给出了你的输出。
不是您正在寻找的解决方案,但您可以从 RANK 和 sub-select 和 WHERE 移动到使用 QUALIFY 并隐藏 order_rank 行:
SELECT
order_id,
time,
state
FROM VALUES
(1,'10.15','mfr'),
(1,'10.15','delivered'),
(2,'12.10','picked'),
(2,'12.10','mfr')
v(order_id, time, state)
QUALIFY RANK() over (partition by order_id order by time) = 1;
但是正如 userMT 所说,您想在选择中加入数字排名
喜欢:
case state
when 'delivered' then 1
when 'mfr' then 2
when 'picked' then 3
else 4
end as state_rank,
因此,如果您将它作为一个单独的字段,它会更易于阅读:
SELECT
order_id,
time,
state,
case state
when 'delivered' then 1
when 'mfr' then 2
when 'picked' then 3
else 4
end as state_rank
FROM VALUES
(1,'10.15','mfr'),
(1,'10.15','delivered'),
(2,'12.10','picked'),
(2,'12.10','mfr')
v(order_id, time, state)
QUALIFY RANK() over (partition by order_id order by time,state_rank) = 1;
给予:
ORDER_ID
TIME
STATE
STATE_RANK
1
10.15
delivered
1
2
12.10
mfr
2
但您也可以将案例混入排名中:
SELECT
order_id,
time,
state
FROM VALUES
(1,'10.15','mfr'),
(1,'10.15','delivered'),
(2,'12.10','picked'),
(2,'12.10','mfr')
v(order_id, time, state)
QUALIFY RANK() over (partition by order_id order by time, case state when 'delivered' then 1 when 'mfr' then 2 when 'picked' then 3 else 4 end) = 1;
给出:
ORDER_ID
TIME
STATE
1
10.15
delivered
2
12.10
mfr
你可以试试
row_number() over (partition by Orderid order by State)
select 第一个
我有以下输出:
Orderid | Time | State | Order_rank |
---|---|---|---|
1 | 10.15 | mfr | 1 |
1 | 10.15 | delivered | 1 |
2 | 12.10 | picked | 1 |
2 | 12.10 | mfr | 1 |
这里我已经将订单ID排序到最新时间,并将条件放在where子句“where order_rank = 1”中,但我想return一个orderjobid的单个条目,即对于 orderid 1,我想要 return 交付状态条目,对于 orderid 2,我想要 mfr 状态条目等。
这只是一个示例数据,我有1000多个orderids,我如何return单次进入同一排名?
扩展您的排名,不仅包括时间,还包括状态表达,例如。
Order time desc,
case state when 'picked' then 1
When 'mfr' then 2
When 'delivered' then 3
...
End asc
这样我们为每个州分配了排序顺序 1,2,3。
根据@simeon-pilgrim 的评论进行编辑以使其正确(感谢)
假设您的代码如下所示:
SELECT * FROM (
SELECT
order_id,
time,
state,
RANK() over (partition by order_id order by time) as order_rank
FROM VALUES
(1,'10.15','mfr'),
(1,'10.15','delivered'),
(2,'12.10','picked'),
(2,'12.10','mfr')
v(order_id, time, state)
)
WHERE order_rank = 1;
它给出了你的输出。
不是您正在寻找的解决方案,但您可以从 RANK 和 sub-select 和 WHERE 移动到使用 QUALIFY 并隐藏 order_rank 行:
SELECT
order_id,
time,
state
FROM VALUES
(1,'10.15','mfr'),
(1,'10.15','delivered'),
(2,'12.10','picked'),
(2,'12.10','mfr')
v(order_id, time, state)
QUALIFY RANK() over (partition by order_id order by time) = 1;
但是正如 userMT 所说,您想在选择中加入数字排名
喜欢:
case state
when 'delivered' then 1
when 'mfr' then 2
when 'picked' then 3
else 4
end as state_rank,
因此,如果您将它作为一个单独的字段,它会更易于阅读:
SELECT
order_id,
time,
state,
case state
when 'delivered' then 1
when 'mfr' then 2
when 'picked' then 3
else 4
end as state_rank
FROM VALUES
(1,'10.15','mfr'),
(1,'10.15','delivered'),
(2,'12.10','picked'),
(2,'12.10','mfr')
v(order_id, time, state)
QUALIFY RANK() over (partition by order_id order by time,state_rank) = 1;
给予:
ORDER_ID | TIME | STATE | STATE_RANK |
---|---|---|---|
1 | 10.15 | delivered | 1 |
2 | 12.10 | mfr | 2 |
但您也可以将案例混入排名中:
SELECT
order_id,
time,
state
FROM VALUES
(1,'10.15','mfr'),
(1,'10.15','delivered'),
(2,'12.10','picked'),
(2,'12.10','mfr')
v(order_id, time, state)
QUALIFY RANK() over (partition by order_id order by time, case state when 'delivered' then 1 when 'mfr' then 2 when 'picked' then 3 else 4 end) = 1;
给出:
ORDER_ID | TIME | STATE |
---|---|---|
1 | 10.15 | delivered |
2 | 12.10 | mfr |
你可以试试
row_number() over (partition by Orderid order by State)
select 第一个