Solved

Copy three months records of about 20 million of data from 200 million of records using the date range  and some where condition

Posted on 2011-02-22
3
428 Views
Last Modified: 2012-06-27
Hi I have a query where I have to copy the ~ 2 millions of records to another dummy table from 200 millions of records with some condition  with ID as Clustered Primary key

These are Based on two conditions:

1. Recent 3 months of data needs to be copied based on the date range... datatype is datetime which is in the form of following: '2010-02-11 01:25:00.670'

2. There is Description column which says 'Unknown Code 0'. Copy other than those records.

. How can I define the range of date by not hard coding the values I just want to pass the range of date by passing into variables using @min and @max

Is it easy to acheive using temp table or CTE's or is there any easy method that can copy huge data.

Thanks,


0
Comment
Question by:srionline2k6
3 Comments
 

Author Comment

by:srionline2k6
ID: 34950112
Thing to  add more. How can I define the range of date by not hard coding the values I just want to pass the range of date by passing into variables using @min and @max
0
 
LVL 12

Expert Comment

by:sachitjain
ID: 34950749
Can't you have non-clustered index on your date column? Also if you can add description in include list of that non-clustered index. If so then u could use between construct in ur where clause to filter records for 3 months as per @min and @max along with description. You could temporarily hold these selected records in temp table that is again cluster indexed on ur Id. From there use them the way you desire.
0
 
LVL 9

Accepted Solution

by:
sureshbabukrish earned 125 total points
ID: 34951030
DECLARE @ReportStartDate DATETIME            
DECLARE @ReportEndDate DATETIME            
SET @ReportStartDate = DateAdd(M,-3,DateAdd(D,DateDiff(D,0,Getdate()),0))    
SET @ReportEndDate = DateAdd(MS,-3,DateAdd(D,DateDiff(D,0,Getdate()),0))    

the above date range gets last 3 months , if today date is 22nd feb, then nov22nd to feb 21st  will be pulled.

select * from  table1
where datecol between    @ReportStartDate,@ReportEndDate
and description <> 'Unknown Code 0'

above query will pull last three months data and can be used in any insert query or to create any view.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question