【问题标题】:Create table with multiple columns from a 2 columns table (rows to columns)从 2 列表(行到列)创建具有多列的表
【发布时间】:2021-05-02 13:37:24
【问题描述】:

我有以下问题需要您的帮助才能使用资源表中的数据创建结果表

我有一个“资源”表:

Code Value
A 100
B 100
C 220
A 150
C 300
D 120
E 120

使用 SQL 创建 'Result' 表:

Code1 Value1 Code2 Value2 Code3 Value3
A 100 B 100 C 220
A 150 C 300 D 120
E 120

我的想法是创建 3 个表然后加入它们,但我不知道如何获得第一、第二和第三行。有没有人有更优化的方法?

感谢您阅读我的问题!

【问题讨论】:

  • 请解释一下资源表的逻辑。这并不明显。为什么 D --> B?为什么 E 在 A 列?
  • 我已经编辑了表格,抱歉打错了。据我了解,Result 表将在 Resource 中获得 3 行以放入 Code1Value1、Code2Value2、...
  • 请注明您的数据库管理系统的版本。
  • 我在 sqliteonline.com 上运行它
  • 表的行没有固有的顺序。如需具体订货,请注明。否则,结果表将具有任何 [随机] 顺序的行。

标签: sql sqlite


【解决方案1】:

您没有为行指定任何排序,因此我按(code, value) 对它们进行了排序。请记住,表中的行没有任何固有的顺序(例如,在电子表格中)。

你可以这样做:

with
x as (
  select *,
    (row_number() over(order by code, value) - 1) / 3 as line,
    (row_number() over(order by code, value) - 1) % 3 as col
  from t
)
select
  a.code as code1, a.value as value1,
  b.code as code2, b.value as value2,
  c.code as code3, c.value as value3
from x a
left join x b on b.line = a.line and b.col = 1
left join x c on c.line = a.line and c.col = 2
where a.col = 0;

结果:

 code1  value1  code2  value2  code3  value3 
 ------ ------- ------ ------- ------ ------ 
 A      100     A      150     B      100    
 C      220     C      300     D      120    
 E      120     null   null    null   null   

请参阅DB Fiddle 的运行示例。

【讨论】:

    【解决方案2】:

    架构和插入语句:

     create table Resource (Code varchar(10),   Value int);
    
     insert into Resource values('A',   100);
     insert into Resource values('B',   100);
     insert into Resource values('C',   220);
     insert into Resource values('A',   150);
     insert into Resource values('C',   300);
     insert into Resource values('D',   120);
     insert into Resource values('E',   120);
    

    查询#1:

     with cte as 
     (
     select *,row_number()over(order by null) rn from Resource
     )
     select (case when rn%3=1 then code end) as Code1, (case when rn%3=1 then value end) as Value1
     ,(case when rn%3=2 then code end) as Code2, (case when rn%3=2 then value end) as Value2,
     (case when rn%3=0 then code end) as Code3, (case when rn%3=0 then value end) as Value3
      from cte 
    

    输出:

    Code1 Value1 Code2 Value2 Code3 Value3
    A 100 null null null null
    null null B 100 null null
    null null null null C 220
    A 150 null null null null
    null null C 300 null null
    null null null null D 120
    E 120 null null null null

    查询#2

     with cte as 
     (
     select *,row_number()over(order by null) rn from Resource
     )
     ,Code1 as 
      ( 
      select Code Code1, value  Value1
      , row_number()over(order by null) rownumber
      from cte n where rn%3=1
      )
     ,Code2 as
      ( 
      select Code Code2, value  Value2
      , row_number()over(order by null) rownumber
      from cte n where rn%3=2 
      )
     ,Code3 as
      ( 
      select Code Code3, value  Value3
      , row_number()over(order by null) rownumber
      
      from cte n where rn%3=0
      )
      
      select *,code1.rownumber from Code1 left join Code2 on Code1.rownumber=Code2.rownumber
      left join Code3 on Code2.rownumber = Code3.rownumber
      
      
      
    
    Code1 Value1 rownumber Code2 Value2 rownumber Code3 Value3 rownumber rownumber
    A 100 1 B 100 1 C 220 1 1
    A 150 2 C 300 2 D 120 2 2
    E 120 3 null null null null null null 3

    dbhere

    【讨论】:

    • 如果您遇到此问题的更多困难,请告诉我。这是一个很好的问题。
    • 如果这个回答对你有帮助,请采纳
    猜你喜欢
    • 2016-07-24
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-09
    • 2019-12-27
    • 1970-01-01
    相关资源
    最近更新 更多