区分一列 return 选定的列并使用 sybase 按日期 desc 排序
Distinct one column return selected columns and order by date desc using sybase
大家好,我有一个关于如何区分单列和 return selected 列的问题
Ac_no ord_status order_no
12334 PL 1
12334 ML 2
12334 CL 3
64543 PL 1
65778 JL 6
83887 CL 4
83887 KL 3
Ac_no ord_statu sorder_no
12334 CL 3
64543 PL 1
65778 JL 6
83887 CL 4
我想看看那个结果
这是我的示例或代码,但不幸的是代码在 sybase 1.2.0.637 中不起作用
SELECT Ac_no, ord_status, order_no
select *, ROW_NUMBER() OVER (PARTITION BY Ac_no order by ord_status)rm
来自 wo_order)x
其中 x = 1
您似乎想为每个 Ac_no
组记录显示具有最低 ord_status
的单个记录。您走在正确的轨道上,但是您需要使用您为行号定义的别名来限制子查询:
SELECT Ac_no, ord_status, order_no
FROM
(
SELECT *, ROW_NUMBER() OVER (PARTITION BY Ac_no ORDER BY ord_status) rn
FROM wo_order
) t
WHERE rn = 1;
这是一个应该 运行 在你的 Sybase 版本上的版本,即使不使用 ROW_NUMBER
:
SELECT w1.Ac_no, w1.ord_status, w1.order_no
FROM wo_order w1
INNER JOIN
(
SELECT Ac_no, MIN(ord_status) AS min_ord_status
FROM wo_order
GROUP BY Ac_no
) w2
ON w1.Ac_no = w2.Ac_no AND
w1.ord_status = w2.min_ord_status;
在没有 window 函数的情况下应该可以工作:
select t1.* from wo_order t1,
(select max(order_no) order_no, ac_no from wo_order group by ac_no) t2
where
t1.ac_no=t2.ac_no
and t1.order_no=t2.order_no
大家好,我有一个关于如何区分单列和 return selected 列的问题
Ac_no ord_status order_no
12334 PL 1
12334 ML 2
12334 CL 3
64543 PL 1
65778 JL 6
83887 CL 4
83887 KL 3
Ac_no ord_statu sorder_no
12334 CL 3
64543 PL 1
65778 JL 6
83887 CL 4
我想看看那个结果
这是我的示例或代码,但不幸的是代码在 sybase 1.2.0.637 中不起作用
SELECT Ac_no, ord_status, order_no select *, ROW_NUMBER() OVER (PARTITION BY Ac_no order by ord_status)rm 来自 wo_order)x 其中 x = 1
您似乎想为每个 Ac_no
组记录显示具有最低 ord_status
的单个记录。您走在正确的轨道上,但是您需要使用您为行号定义的别名来限制子查询:
SELECT Ac_no, ord_status, order_no
FROM
(
SELECT *, ROW_NUMBER() OVER (PARTITION BY Ac_no ORDER BY ord_status) rn
FROM wo_order
) t
WHERE rn = 1;
这是一个应该 运行 在你的 Sybase 版本上的版本,即使不使用 ROW_NUMBER
:
SELECT w1.Ac_no, w1.ord_status, w1.order_no
FROM wo_order w1
INNER JOIN
(
SELECT Ac_no, MIN(ord_status) AS min_ord_status
FROM wo_order
GROUP BY Ac_no
) w2
ON w1.Ac_no = w2.Ac_no AND
w1.ord_status = w2.min_ord_status;
在没有 window 函数的情况下应该可以工作:
select t1.* from wo_order t1,
(select max(order_no) order_no, ac_no from wo_order group by ac_no) t2
where
t1.ac_no=t2.ac_no
and t1.order_no=t2.order_no