基于年份和季度的分区表

时间:2017-10-26 11:46:11

标签: sql-server partitioning

我正在尝试在sql server 2016中创建一个表分区。我基本上需要根据年份和季度进行分区。我们在查询中有两个字段来确定它。 periodenddate,它确定决定季度的年份和财政季度,并且是int字段。 我需要的第二件事是应该从2000年到2070年创建分区 我创建了5个文件组。所以不确定如何在文件组中分配分区

请参阅下面我现有的分区查询。目前只在年内分配并考虑到过去5年。

USE CoreReferenceStaging;  
GO  
-- Adds five new filegroups to the CoreReferenceStaging database  
ALTER DATABASE CoreReferenceStaging  
ADD FILEGROUP CT1;  
GO  
ALTER DATABASE CoreReferenceStaging  
ADD FILEGROUP CT2;  
GO  
ALTER DATABASE CoreReferenceStaging  
ADD FILEGROUP CT3;  
GO  
ALTER DATABASE CoreReferenceStaging  
ADD FILEGROUP CT4;
GO  
ALTER DATABASE CoreReferenceStaging  
ADD FILEGROUP CT5;


-- Adds one file for each filegroup.  
ALTER DATABASE CoreReferenceStaging   
ADD FILE   
(  
    NAME = ctdata1,  
    FILENAME = 'M:\Data\MSSQL13.MSSQLSERVER\MSSQL\DATA\ctdata1.ndf',  
    SIZE = 25MB,  
    MAXSIZE = 500MB,  
    FILEGROWTH = 10MB  
)  
TO FILEGROUP CT1;  
ALTER DATABASE CoreReferenceStaging   
ADD FILE   
(  
    NAME = ctdata2,  
    FILENAME = 'M:\Data\MSSQL13.MSSQLSERVER\MSSQL\DATA\ctdata2.ndf',  
    SIZE = 25MB,  
    MAXSIZE = 500MB,  
    FILEGROWTH = 10MB  
)  
TO FILEGROUP CT2;  
GO  
ALTER DATABASE CoreReferenceStaging   
ADD FILE   
(  
    NAME = ctdata3,  
    FILENAME = 'M:\Data\MSSQL13.MSSQLSERVER\MSSQL\DATA\ctdata3.ndf',  
    SIZE = 25MB,  
    MAXSIZE = 500MB,  
    FILEGROWTH = 10MB  
)  
TO FILEGROUP CT3;  
GO  
ALTER DATABASE CoreReferenceStaging   
ADD FILE   
(  
    NAME = ctdata4,  
    FILENAME = 'M:\Data\MSSQL13.MSSQLSERVER\MSSQL\DATA\ctdata4.ndf',  
    SIZE = 25MB,  
    MAXSIZE = 500MB,  
    FILEGROWTH = 10MB  
)  
TO FILEGROUP CT4;  
GO  
ALTER DATABASE CoreReferenceStaging   
ADD FILE   
(  
    NAME = ctdata5,  
    FILENAME = 'M:\Data\MSSQL13.MSSQLSERVER\MSSQL\DATA\ctdata5.ndf',  
    SIZE = 25MB,  
    MAXSIZE = 500MB,  
    FILEGROWTH = 10MB  
)  
TO FILEGROUP CT5;  
GO  






CREATE PARTITION FUNCTION financialStatementPartition (datetime)  
    AS RANGE RIGHT FOR VALUES ();

CREATE PARTITION SCHEME financialStatementRange  
    AS PARTITION financialStatementPartition  
    ALL TO ([PRIMARY]); 
DECLARE @periodenddate datetime = DATEADD(year, -4, DATEADD(year, DATEDIFF(year, '', GETDATE()), ''));
ALTER PARTITION SCHEME financialStatementRange
    NEXT USED CT1;
ALTER PARTITION FUNCTION financialStatementPartition()
    SPLIT RANGE(@periodenddate);
SET @periodenddate = DATEADD(year, 1, @periodenddate);
ALTER PARTITION SCHEME financialStatementRange
    NEXT USED CT2;
ALTER PARTITION FUNCTION financialStatementPartition()
    SPLIT RANGE(@periodenddate);
SET @periodenddate = DATEADD(year, 1, @periodenddate);
ALTER PARTITION SCHEME financialStatementRange
    NEXT USED CT3;
ALTER PARTITION FUNCTION financialStatementPartition()
    SPLIT RANGE(@periodenddate);
SET @periodenddate = DATEADD(year, 1, @periodenddate);
ALTER PARTITION SCHEME financialStatementRange
    NEXT USED CT4;
ALTER PARTITION FUNCTION financialStatementPartition()
    SPLIT RANGE(@periodenddate);
SET @periodenddate = DATEADD(year, 1, @periodenddate);
ALTER PARTITION SCHEME financialStatementRange
    NEXT USED CT5;
ALTER PARTITION FUNCTION financialStatementPartition()
    SPLIT RANGE(@periodenddate);




CREATE TABLE [dbo].[FinancialStatementIds_Partitioned](
    [financialCollectionId] [int] NOT NULL,
    [companyId] [int] NOT NULL,
    [dataItemId] [int] NOT NULL,
    [dataItemName] [varchar](200) NULL,
    [dataItemvalue] [decimal](18, 0) NULL,
    [unittypevalue] [int] NULL,
    [fiscalyear] [int] NULL,
    [fiscalquarter] [int] NULL,
    [periodenddate] [datetime] NULL,
    [filingdate] [datetime] NULL,
    [restatementtypename] [varchar](200) NULL,
    [latestforfinancialperiodflag] [bit] NULL,
    [latestfilingforinstanceflag] [bit] NULL,
    [currencyconversionflag] [int] NULL,
    [currencyname] [varchar](200) NULL,
    [periodtypename] [varchar](200) NULL
) ON financialStatementRange(periodenddate);
GO 

INSERT INTO dbo.FinancialStatementIds_Partitioned
SELECT *
FROM dbo.FinancialStatementIds WITH(TABLOCKX);
GO
DROP TABLE dbo.FinancialStatementIds;
GO
EXEC sp_rename N'dbo.FinancialStatementIds_Partitioned', N'FinancialStatementIds';
GO

从2000年到2070年按年分区

CREATE PARTITION SCHEME financialStatementRange  
    AS PARTITION financialStatementPartition  
    ALL TO ([PRIMARY]); 

DECLARE @StartDate DATE = '20000101'
DECLARE @periodenddate DATE = '20700101'

while( @StartDate < @periodenddate) 
begin
ALTER PARTITION SCHEME financialStatementRange
    NEXT USED [PRIMARY];
ALTER PARTITION FUNCTION financialStatementPartition()
    SPLIT RANGE(@StartDate);
   SET @StartDate = DATEADD(year, 1, @StartDate);

end

1 个答案:

答案 0 :(得分:0)

检查一下,它可能适合你:

CREATE TABLE dbo.Orders
(
    OrderID     integer NOT NULL,
    Name        nvarchar(20) NULL,
    OrderDate   date NOT NULL,

    CONSTRAINT PK__Orders_OrderID
        PRIMARY KEY NONCLUSTERED 
            (OrderID)
        ON [PRIMARY]
)
ON PS (OrderDate);
GO
CREATE CLUSTERED INDEX CX__Orders_OrderDate
ON dbo.Orders (OrderDate)
ON PS (OrderDate)
相关问题