Azure / U-SQL - ROW_NUMBER() 中的 ORDER BY
Azure / U-SQL - ORDER BY in ROW_NUMBER()
我正在构建一个 U-SQL 脚本,它有几个中间 tables。我想根据其中一列的顺序分配 ROW_NUMBER(),但不知道该怎么做。
@t = SELECT *
FROM(
VALUES
( 1, "2010-01-01","TagName1", 30 ),
( 2, "2010-01-01","TagName2", 5 ),
( 2, "2010-01-02","TagName2", 7 ),
( 2, "2010-01-02","TagName3", 6 ),
( 3, "2010-01-03","TagName2", 15 ),
( 1, "2010-01-01","TagName2", 2 ),
( 1, "2010-01-01","TagName3", 1),
( 3, "2010-01-04","TagName1", 2 ),
( 3, "2010-01-04","TagName2", 4 )
) AS T(DeviceID, Date, TagName, dv);
@t2 = SELECT DISTINCT(dv) as dv
, ROW_NUMBER() OVER() as LevelNumber
FROM @t
ORDER BY dv;
如果我 运行 上面的代码它告诉我将 ORDER BY
移动到 OUTPUT
语句但是当 table 内容改变时这不起作用在随后的操作中。
我尝试将列名放在 OVER()
部分,但这也不起作用。
我想要这样的东西:
dv LevelNumber
1 1
2 2
4 3
5 4
有什么建议吗?
您可以在 OVER
子句中包含 ORDER BY
and/or PARTITION BY
子句。您可以将 ORDER BY
子句移动到 OUTPUT
语句中,例如
@t = SELECT *
FROM(
VALUES
( 1, "2010-01-01","TagName1", 30 ),
( 2, "2010-01-01","TagName2", 5 ),
( 2, "2010-01-02","TagName2", 7 ),
( 2, "2010-01-02","TagName3", 6 ),
( 3, "2010-01-03","TagName2", 15 ),
( 1, "2010-01-01","TagName2", 2 ),
( 1, "2010-01-01","TagName3", 1),
( 3, "2010-01-04","TagName1", 2 ),
( 3, "2010-01-04","TagName2", 4 )
) AS T(DeviceID, Date, TagName, dv);
//@t2 = SELECT DISTINCT(dv) as dv
// , ROW_NUMBER() OVER() as LevelNumber
// FROM @t
// ORDER BY dv;
@output =
SELECT DeviceID,
Date,
TagName,
dv,
ROW_NUMBER() OVER( ORDER BY dv ) AS LevelNumber
FROM @t;
OUTPUT @output TO "/output/output.csv"
ORDER BY dv
USING Outputters.Csv();
我能得到的最接近你的结果的是这个查询,但你需要的逻辑对我来说并不是 100% 清楚:
@output =
SELECT dv,
ROW_NUMBER() OVER(ORDER BY dv) AS LevelNumber
FROM
(
SELECT DISTINCT dv
FROM @t
) AS x;
我正在构建一个 U-SQL 脚本,它有几个中间 tables。我想根据其中一列的顺序分配 ROW_NUMBER(),但不知道该怎么做。
@t = SELECT *
FROM(
VALUES
( 1, "2010-01-01","TagName1", 30 ),
( 2, "2010-01-01","TagName2", 5 ),
( 2, "2010-01-02","TagName2", 7 ),
( 2, "2010-01-02","TagName3", 6 ),
( 3, "2010-01-03","TagName2", 15 ),
( 1, "2010-01-01","TagName2", 2 ),
( 1, "2010-01-01","TagName3", 1),
( 3, "2010-01-04","TagName1", 2 ),
( 3, "2010-01-04","TagName2", 4 )
) AS T(DeviceID, Date, TagName, dv);
@t2 = SELECT DISTINCT(dv) as dv
, ROW_NUMBER() OVER() as LevelNumber
FROM @t
ORDER BY dv;
如果我 运行 上面的代码它告诉我将 ORDER BY
移动到 OUTPUT
语句但是当 table 内容改变时这不起作用在随后的操作中。
我尝试将列名放在 OVER()
部分,但这也不起作用。
我想要这样的东西:
dv LevelNumber
1 1
2 2
4 3
5 4
有什么建议吗?
您可以在 OVER
子句中包含 ORDER BY
and/or PARTITION BY
子句。您可以将 ORDER BY
子句移动到 OUTPUT
语句中,例如
@t = SELECT *
FROM(
VALUES
( 1, "2010-01-01","TagName1", 30 ),
( 2, "2010-01-01","TagName2", 5 ),
( 2, "2010-01-02","TagName2", 7 ),
( 2, "2010-01-02","TagName3", 6 ),
( 3, "2010-01-03","TagName2", 15 ),
( 1, "2010-01-01","TagName2", 2 ),
( 1, "2010-01-01","TagName3", 1),
( 3, "2010-01-04","TagName1", 2 ),
( 3, "2010-01-04","TagName2", 4 )
) AS T(DeviceID, Date, TagName, dv);
//@t2 = SELECT DISTINCT(dv) as dv
// , ROW_NUMBER() OVER() as LevelNumber
// FROM @t
// ORDER BY dv;
@output =
SELECT DeviceID,
Date,
TagName,
dv,
ROW_NUMBER() OVER( ORDER BY dv ) AS LevelNumber
FROM @t;
OUTPUT @output TO "/output/output.csv"
ORDER BY dv
USING Outputters.Csv();
我能得到的最接近你的结果的是这个查询,但你需要的逻辑对我来说并不是 100% 清楚:
@output =
SELECT dv,
ROW_NUMBER() OVER(ORDER BY dv) AS LevelNumber
FROM
(
SELECT DISTINCT dv
FROM @t
) AS x;