SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Need T-SQL optimization Help


Need T-SQL optimization Help

Author
Message
vignesh.ms
vignesh.ms
Old Hand
Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)

Group: General Forum Members
Points: 366 Visits: 516
Hi There,

Following is the sample data ,

DECLARE @maxval int, @minval int
select @maxval=201301,@minval=201312

SELECT top 100001 CAST(((@maxval + 1) - @minval) *
RAND(CHECKSUM(NEWID())) + @minval AS int)Date ,ceiling (RAND(CHECKSUM(NEWID())) *1000+25692*RAND(CHECKSUM(NEWID())) )*ceiling (RAND(CHECKSUM(NEWID())) *1000+25692*RAND(CHECKSUM(NEWID())) )ID
into #sample from sysobjects , syscolumns



Problem :
* there are 2 columns date & id
* take first month id's , say for example 201302
* need to find whether those id's are available or not, in the next 3 months. so here 201303,201304,201305
* need to repeat this for all months

Here is the my code, It requires more manual intervention and time taking. Please assist.


select distinct date from #sample order by DATE

declare @processingdate varchar(6),@from varchar(6),@to varchar(6)
set @processingdate = '201302'
set @from = '201303'
set @to = '201305'


select distinct id into #Source from #sample where DATE = @processingdate
select distinct id into #lookup from #sample where DATE between @from and @to

select @processingdate,count(*) from #Source where ID not in (select * from #lookup)




Thanks
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)

Group: General Forum Members
Points: 86816 Visits: 41103
To be honest, whomever designed the table with an integer representation of year and month did a great disservice to anyone and everyone using the table and it's going to be a real bugger when the year roles over. Are you allowed to change the datatype of that column?

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
j.miner
j.miner
SSC-Enthusiastic
SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)

Group: General Forum Members
Points: 156 Visits: 358
I agree with Jeff that a date should be stored as a date in the source system (OLTP). I would try to change it. However, I know how hard it is sometimes to get buy in at the Business Level.

I have work on a few data warehouses (OLAP) in my time. One part of a star schema is a date dimension. Usually some number is given to a date. That number is used throughout the tables.

How about a date decode table that translates the 201301 (a number stored as a char(6)) to '2013-01-01' as a date. Thus, adding one more join to the equation allows you to work with dates.

I would check Aaron's article on handling dates.

http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/16/bad-habits-to-kick-mishandling-date-range-queries.aspx

I would keep away from BETWEEN.

Good luck ...

John Miner
Crafty DBA
www.craftydba.com
vignesh.ms
vignesh.ms
Old Hand
Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)

Group: General Forum Members
Points: 366 Visits: 516
Jeff Moden (1/4/2014)
To be honest, whomever designed the table with an integer representation of year and month did a great disservice to anyone and everyone using the table and it's going to be a real bugger when the year roles over. Are you allowed to change the datatype of that column?



Yes I can change the datatype ... Its complex bet one time activity..
so I could ...
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search