简体   繁体   English

SUBSTRING在SQL分区中的使用

[英]Uses of SUBSTRING in SQL Partitioning

I have created a table and partitioned it(the code is here , Please see the Example of horizontal partitioning with creating a new partitioned table part) I have use the following Partition function : 我已经创建了一个表并对其进行了分区(代码在这里 ,请参见创建新分区表部分的水平分区示例 )我已经使用了以下分区功能

CREATE PARTITION FUNCTION [PartitioningByMonth] (datetime)
AS RANGE RIGHT FOR VALUES ('20140201', '20140301', '20140401',
               '20140501', '20140601', '20140701', '20140801', 
               '20140901', '20141001', '20141101', '20141201'); 

And my SCHEMA is : 我的模式是

CREATE PARTITION SCHEME PartitionBymonth
AS PARTITION PartitioningBymonth
TO (January, February, March, 
    April, May, June, July, 
    Avgust, September, October, 
    November, December);

And My table create Query is : 我的表创建查询是

CREATE TABLE Reports
(ReportDate datetime PRIMARY KEY,
MonthlyReport varchar(max))
ON PartitionBymonth (ReportDate);
GO

if i insert values which are in the year of 2014 (i mean 20140105 , 20140205 , ...... ), it works fine : 如果我插入这在当年2014 (我的意思是2014010520140205 ,......),它工作正常:

INSERT INTO Reports (ReportDate,MonthlyReport)
SELECT '20140105', 'ReportJanuary' UNION ALL
SELECT '20140205', 'ReportFebryary' UNION ALL
SELECT '20140308', 'ReportMarch' UNION ALL

But problem occurs when i insert any value of 2015 , 2016 , 2017 .....: 但是当我插入201520162017任何值时都会出现问题..

   INSERT INTO Reports (ReportDate,MonthlyReport)
    SELECT '20150105', 'ReportJanuary' UNION ALL
    SELECT '20150206', 'ReportFebryary' UNION ALL
    SELECT '20150309', 'ReportMarch' UNION ALL

as it push it only the last partition ( 20141201 i mean December partition). 因为它只推最后一个分区( 20141201我的意思是December分区)。 But i want them to go January , February , March partition. 但是我希望他们去JanuaryFebruaryMarch分区。 I know my problem is in PARTITION FUNCTION because i wrote ('20140201', '20140301',......) only for 2014 year. 我知道我的问题出在“ PARTITION FUNCTION因为我只为2014年编写('20140201','20140301',......)。 But i want to count the Month only .Is there any way so that it only see the month only 2015 03 01 rather than year or day.(can i use SUBSTRING in any part ) 但是我只想计算月份 。有什么办法让它只看到月份2015 03 01而不是年份或日期。(我可以在任何部分使用SUBSTRING)

you can add a computed column to your table as 您可以将计算列添加到表中

ReportDateP as MONTH(ReportDate) PERSISTED

and create the table with partition on this computed column 并在此计算列上创建具有分区的表

Then create the partition function as follows 然后如下创建分区函数

CREATE PARTITION FUNCTION [NewPartitioning] (int)
AS RANGE RIGHT FOR VALUES (2,3,4,5,6,7,8,9,10,11,12); 

You can not use SubQuery in Partions system. 您不能在Partions系统中使用SubQuery。 Use Eralper Method for partitioning this portion 使用Eralper方法对该部分进行分区

CREATE PARTITION SCHEME PartitionBymonth
AS PARTITION PartitioningBymonth
TO (January, February, March, 
    April, May, June, July, 
    Avgust, September, October, 
    November, December); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM