以下是我在表格中名为TEMP2的列
[ID],[Week1], [Week2],[Week3],[Week4],[Week5],
[Week6],[Week7],[Week8],[Week9], [Week10],
[Week11],[Week12],[Week13], [Week14],[Week15],
[Week16],[Week17], [Week18],[Week19],[Week20],
[Week21], [Week22],[Week23],[Week24],[Week25], [Week26]
我想找到每行的前6条记录并显示。
以下是我尝试使用的查询,但未获取预期结果。我在这里做错了什么?
SELECT
id, [1], [2], [3],[4], [5], [6]
FROM
(SELECT
id, Val, rn
FROM
(SELECT id, Val, Col,
ROW_NUMBER()OVER (PARTITION BY id ORDER BY Val)
AS rn
FROM
TEMP2
UNPIVOT
(Val FOR Col IN ([Week1],
[Week2],[Week3],[Week4],[Week5],[Week6],[Week7],[Week8],[Week9],
[Week10],[Week11],[Week12],[Week13],[Week14],[Week15],[Week16],[Week17],
[Week18],[Week19],[Week20],[Week21],[Week22],[Week23],[Week24],[Week25],
[Week26]))AS unpvt) AS t
WHERE
t.rn <= 6) AS src
PIVOT
(MAX(Val) FOR rn IN ([1], [2], [3],[4], [5], [6])) AS pvt
我正在使用SQL Server 2008。
答案 0 :(得分:2)
正如https://github.com/jcodec/jcodec/blob/master/src/main/java/org/jcodec/api/SequenceEncoder.java在上述评论中所建议的那样,您的PARTITION BY
语句中只需要SELECT
id, [1], [2], [3], [4], [5], [6]
FROM
(SELECT
id, Val, rn
FROM
(SELECT id, Val, Col,
ROW_NUMBER()OVER (PARTITION BY id ORDER BY Val DESC)
AS rn
FROM
TEMP2
UNPIVOT
(Val FOR Col IN ([Week1],
[Week2],[Week3],[Week4],[Week5],[Week6],[Week7],[Week8],[Week9],
[Week10],[Week11],[Week12],[Week13],[Week14],[Week15],[Week16],[Week17],
[Week18],[Week19],[Week20],[Week21],[Week22],[Week23],[Week24],[Week25],
[Week26]))AS unpvt) AS t
WHERE
t.rn <= 6) AS src
PIVOT
(MAX(Val) FOR rn IN ([1], [2], [3], [4], [5], [6])) AS pvt
,就可以先返回最大值。
这对我设置的一些测试数据起了作用:
// myModule.js
import dependency from './dependency';
export default (x) => {
dependency.doSomething(x * 2);
}