【问题标题】:Inserting values to SQL Server Database将值插入 SQL Server 数据库
【发布时间】:2018-07-24 17:47:20
【问题描述】:

我正在使用 python 和 microsoft sql server 并希望将数据值垂直导入我的数据库,但数据库会水平批准这些值。我使用 pd.Excelfile 读取文件。一个例子是:

数据库批准这些垂直值:

A 1 2 3 4 5 6 7 8 9
B 3 4 5 6 7 8 2 6 7
C 8 9 4 6 3 2 5 1 3

但我宁愿横向阅读:

A B C
1 3 8
2 4 9
3 5 4
4 6 6
5 7 3
6 8 2
7 6 5
8 7 1

希望我的解释清楚地描述,感谢答案

【问题讨论】:

  • 听起来像pivot 的排序表是必需的
  • 只是transpose DataFrame?
  • 这里的批准是什么意思:数据库批准这些垂直值

标签: python sql sql-server pandas


【解决方案1】:

你可以使用apply

with t1 as (
     select t.col, tt.cols, row_number() over (partition by t.col order by cols) seq
     from table t cross apply
         ( values (col1), (col2), . . . 
         ) tt (cols)
)
select max(case when col = 'A' then cols end) as [A],
       max(case when col = 'B' then cols end) as [B],
       max(case when col = 'C' then cols end) as [C]
from t1
group by seq;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-02
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多