如何根据 1-69 之间的数字列表创建 5 个数字排列的列表?
技术问答
161 人阅读
|
0 人回复
|
2023-09-12
|
我有一个 SQL Server DB,包括列 NM1-NM69 及其字段值中的相应数字:
, k. ^. j/ p" Q5 N7 Z5 D) wNM1 | NM2 | NM3 | NM4 | NM5 |...... | NM69------------------------------------------1 | 2 | 3 | 4 | 5 |...... | 69我试图找出这个列表的所有 5 数字排列。我怎么能简单易行(好..这样做更容易)?3 ?! Z* q" H8 U' |8 y
示例输出:
7 n5 X _* b8 z2 bPM NM1 | PM NM2 | PM NM3 | PM NM4 | PM NM5 |--------------------------------------------68 | 2 | 55 | 43 | 52 |编辑:我在用 SQL8 A/ ~$ K, Z: R9 ?2 |5 _' ?9 `% U% H
/ s: G5 l, S4 y5 B
解决方案: * a N6 k, G% K! y
您将数据 UNPIVOT 放入一列,然后 CROSS JOIN 五次。这是 SQL Fiddle 排列 5 选择 33. SQL Fiddle
; |" w8 ^( r: C- b; n, HDECLARE @t TABLE (Field1 INT,Field2 INT,Field3 INT,Field4 INT,Field5 INT)INSERT INTO @t VALUES WITH cte AS SELECT u.FieldValue FROM @t UNPIVOT [FieldValue] FOR [FieldName] IN Field1,Field2,Field3,Field4,Field5 u)SELECT t1.FieldValue AS PermuteField1, t2.FieldValue AS PermuteField2, t3.FieldValue AS PermuteField3FROM cte AS t1 CROSS JOIN cte AS t2 CROSS JOIN cte AS t3WHERE t1.FieldValue t2.FieldValue AND t1.FieldValue t3.FieldValue AND t2.FieldValue t3.FieldValue |
|
|
|
|
|