如何在 SQL Server 2016 中创建宽表?

2022-11-23数据库问题
119

本文介绍了如何在 SQL Server 2016 中创建宽表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

使用此代码(*),在 SQL 中创建宽表让我不断发送:

With this code(*), the creation of a wide table in SQL keeps me sending this:

Msg 1702, Level 16, State 1, Line 11
CREATE TABLE failed because column '2010/12/01' in table 'PriceToBookFinalI' exceeds the maximum of 1024 columns.

使用[样式]去

CREATE TABLE [dbo].[PriceToBookFinalI]
    (DocID int PRIMARY KEY,
    [2006/12/29][Money],
    [2007/01/01][Money],
    ...
    SpecialPurposeColumns XML COLUMN_SET FOR ALL_SPARSE_COLUMNS);  

GO

(2614 columns)

寻找好的提示!

这里是我想导入到宽表的背景数据集

Here is the background set of data I want to import to my wide table

推荐答案

对此的解决方案是规范化您的设计.即使您可以将其放入 1024 限制,您的设计也不是一个好主意.例如,如果您想知道 DocID 每月更改的平均金额怎么办.在这个模型中编写这将是一场噩梦.

The solution for this is to normalize your design. Even if you could fit it into the 1024 limit, your design is not a good idea. For example, what if you wanted to know the average amount a DocID changed per each month. That would be a nightmare to write in this model.

试试这个.

CREATE TABLE dbo.PriceToBookFinalI (
                                     DocID INT PRIMARY KEY,
                                     SpecialPurposeColumns XML COLUMN_SET FOR ALL_SPARSE_COLUMNS
                                   );


CREATE TABLE dbo.PriceToBookFinalMoney (
                                         DocID INT,
                                         DocDate DATE,
                                         DocAmount MONEY,
                                         CONSTRAINT PK_PriceToBookFinalMoney
                                           PRIMARY KEY CLUSTERED
                                           (
                                             DocID,
                                             DocDate
                                           )
                                       );

您可以轻松地将带有 SpecialPurposeColumns 的表连接到带有每个 DocID 的日期和金额的表.如果需要,您仍然可以将日期转换为上面提供的格式.将日期作为列中的值可让您更灵活地使用数据、提高性能并自然地处理更多日期.

You can easily join the table with the SpecialPurposeColumns to the table with the dates and amounts for each DocID. You can still pivot the dates if desired into the format you provided above. Having the date as a value in a column gives you much more flexibility how you use the data, better performance, and naturally handles more dates.

这篇关于如何在 SQL Server 2016 中创建宽表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End
SQLServer

相关推荐

将可变参数列表传递给 SqlServer2008 存储过程的理智/快速方法
Sane/fast method to pass variable parameter lists to SqlServer2008 stored procedure(将可变参数列表传递给 SqlServer2008 存储过程的理智/快速方法)...
2023-10-26 数据库问题
1

为什么SqlServer select语句会选择匹配的行和匹配并带有尾随空格的行
Why would SqlServer select statement select rows which match and rows which match and have trailing spaces(为什么SqlServer select语句会选择匹配的行和匹配并带有尾随空格的行)...
2023-10-08 数据库问题
3

SQLSERVER 中的 ListAGG
ListAGG in SQLSERVER(SQLSERVER 中的 ListAGG)...
2023-07-18 数据库问题
7

相当于 mySQL 中的 SQLServer 函数 SCOPE_IDENTITY()?
The equivalent of SQLServer function SCOPE_IDENTITY() in mySQL?(相当于 mySQL 中的 SQLServer 函数 SCOPE_IDENTITY()?)...
2023-04-28 数据库问题
59

使用 spark sql 在 sqlserver 上执行查询
execute query on sqlserver using spark sql(使用 spark sql 在 sqlserver 上执行查询)...
2023-04-04 数据库问题
8

Debezium 如何使用 Kafka Connect 正确注册 SqlServer 连接器 - 连接被拒绝
Debezium How do I correctly register the SqlServer connector with Kafka Connect - connection refused(Debezium 如何使用 Kafka Connect 正确注册 SqlServer 连接器 - 连接被拒绝)...
2023-04-03 数据库问题
3