序列号自动生成
Serial number auto generation
自动生成序列号时遇到问题。
如何正确使用 row_number()
或是否有其他方法可以达到预期的效果?
注:我想写查询不使用CASE
。
SELECT
row_number() over (order by COALESCE(legs.sta, legs.eta) ASC) sno,
legs.Flight_ID flightId,
legs.flight,
legs.flightDate
FROM Flt_OperativeFlight_Legs LEG
ORDER BY COALESCE(legs.sta, legs.eta) ASC
当前结果:
+------+--------+-------------------------+
| sno | flight | flightDate |
+------+--------+-------------------------+
| 1 | 3K0722 | 2019-01-08 17:10:00.000 |
| 2 | 3K0722 | 2019-01-08 20:20:00.000 |
| 3 | 3K0723 | 2019-01-09 17:10:00.000 |
| 4 | 3K0724 | 2019-01-10 20:20:00.000 |
+------+--------+-------------------------+
预期结果:
+------+--------+-------------------------+
| sno | flight | flightDate |
+------+--------+-------------------------+
| 1 | 3K0722 | 2019-01-08 17:10:00.000 |
| | | 2019-01-08 20:20:00.000 |
| 2 | 3K0723 | 2019-01-09 17:10:00.000 |
| 3 | 3K0724 | 2019-01-10 20:20:00.000 |
+------+--------+-------------------------+
您可以使用 DENSE_RANK
而不是 ROW_NUMBER
SELECT
DENSE_RANK() over (order by COALESCE(legs.sta, legs.eta, legs.ata) ASC) as "s.no",
legs.Flight_ID flightId
,legs.flight
,legs.flightDate
FROM Flt_OperativeFlight_Legs LEG
ORDER BY COALESCE(legs.sta, legs.eta, legs.ata) ASC
您需要其他 row_number()
:
SELECT DENSE_RANK() OVER (ORDER BY flt_id, flight) AS Sr,
(CASE WHEN Seq = 1
THEN flightId
END) AS Flight_ID,
(CASE WHEN Seq = 1
THEN flight
END) AS flight, flightDate
FROM (SELECT ROW_NUMBER() OVER(PARTITION BY Flight_ID, flight ORDER BY flightDate) AS Seq,
legs.Flight_ID flightId, legs.flight, legs.flightDate
FROM Flt_OperativeFlight_Legs LEG
) l;
您可以尝试在子查询中使用 ROW_NUMBER
window 函数,然后使用 DENSE_RANK
window 函数创建 no
列 CASE WHEN
表达式得到 rn = 1
行。
SELECT (CASE WHEN rn = 1 THEN DENSE_RANK() over(order by flight) END) no,
(CASE WHEN rn = 1 THEN flt_id END) flt_id,
(CASE WHEN rn = 1 THEN flight END) flight,
flightDate
FROM
(
SELECT *,ROW_NUMBER() OVER(PARTITION BY flt_id,flight ORDER BY flightDate) rn
FROM Flt_OperativeFlight_Legs
) t1
请参考以下解决方案,清空重复数据我需要使用CASE表达式
在 CTE 表达式中,我将 Row_Number 和 Dense_Rank 函数与 Partition By clause
结合使用
;with cte as (
select
ROW_NUMBER() over (order by flight, flightDate) as num,
DENSE_RANK() over (order by flight) as [no],
ROW_NUMBER() over (partition by flight order by flightDate) as rn,
flight,
flightDate
from Flt_OperativeFlight_Legs legs
)
select
case when rn > 1 then null else [no] end as [no],
case when rn > 1 then null else flight end as flight,
flightDate
from cte
order by num
输出如下
自动生成序列号时遇到问题。
如何正确使用 row_number()
或是否有其他方法可以达到预期的效果?
注:我想写查询不使用CASE
。
SELECT
row_number() over (order by COALESCE(legs.sta, legs.eta) ASC) sno,
legs.Flight_ID flightId,
legs.flight,
legs.flightDate
FROM Flt_OperativeFlight_Legs LEG
ORDER BY COALESCE(legs.sta, legs.eta) ASC
当前结果:
+------+--------+-------------------------+
| sno | flight | flightDate |
+------+--------+-------------------------+
| 1 | 3K0722 | 2019-01-08 17:10:00.000 |
| 2 | 3K0722 | 2019-01-08 20:20:00.000 |
| 3 | 3K0723 | 2019-01-09 17:10:00.000 |
| 4 | 3K0724 | 2019-01-10 20:20:00.000 |
+------+--------+-------------------------+
预期结果:
+------+--------+-------------------------+
| sno | flight | flightDate |
+------+--------+-------------------------+
| 1 | 3K0722 | 2019-01-08 17:10:00.000 |
| | | 2019-01-08 20:20:00.000 |
| 2 | 3K0723 | 2019-01-09 17:10:00.000 |
| 3 | 3K0724 | 2019-01-10 20:20:00.000 |
+------+--------+-------------------------+
您可以使用 DENSE_RANK
而不是 ROW_NUMBER
SELECT
DENSE_RANK() over (order by COALESCE(legs.sta, legs.eta, legs.ata) ASC) as "s.no",
legs.Flight_ID flightId
,legs.flight
,legs.flightDate
FROM Flt_OperativeFlight_Legs LEG
ORDER BY COALESCE(legs.sta, legs.eta, legs.ata) ASC
您需要其他 row_number()
:
SELECT DENSE_RANK() OVER (ORDER BY flt_id, flight) AS Sr,
(CASE WHEN Seq = 1
THEN flightId
END) AS Flight_ID,
(CASE WHEN Seq = 1
THEN flight
END) AS flight, flightDate
FROM (SELECT ROW_NUMBER() OVER(PARTITION BY Flight_ID, flight ORDER BY flightDate) AS Seq,
legs.Flight_ID flightId, legs.flight, legs.flightDate
FROM Flt_OperativeFlight_Legs LEG
) l;
您可以尝试在子查询中使用 ROW_NUMBER
window 函数,然后使用 DENSE_RANK
window 函数创建 no
列 CASE WHEN
表达式得到 rn = 1
行。
SELECT (CASE WHEN rn = 1 THEN DENSE_RANK() over(order by flight) END) no,
(CASE WHEN rn = 1 THEN flt_id END) flt_id,
(CASE WHEN rn = 1 THEN flight END) flight,
flightDate
FROM
(
SELECT *,ROW_NUMBER() OVER(PARTITION BY flt_id,flight ORDER BY flightDate) rn
FROM Flt_OperativeFlight_Legs
) t1
请参考以下解决方案,清空重复数据我需要使用CASE表达式
在 CTE 表达式中,我将 Row_Number 和 Dense_Rank 函数与 Partition By clause
结合使用;with cte as (
select
ROW_NUMBER() over (order by flight, flightDate) as num,
DENSE_RANK() over (order by flight) as [no],
ROW_NUMBER() over (partition by flight order by flightDate) as rn,
flight,
flightDate
from Flt_OperativeFlight_Legs legs
)
select
case when rn > 1 then null else [no] end as [no],
case when rn > 1 then null else flight end as flight,
flightDate
from cte
order by num
输出如下