Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Antonio
Posting Yak Master
168 Posts |
Posted - 2006-10-12 : 06:53:31
|
Hi there,I have a simple table consisting of two columns :Cost Centre.....Target_Date1180............200610011180............200610021180............200610031180............200610041180............200610051180............200610061180............200610071180............20061008...etcWhat I want to do is find whether there gaps in the dates for other cost centre number as I only want to select cost centres that have 2 moths worth of consecutive dates.Is there a solution that doesn't use a temporary table with a complete list of dates?Thanks for your help in advance._________________________________________________________________________________________________________________________Inability is a disaster; patience is bravery; abstinence is a treasure, self-restraint is a shield; and the best companion is submission to Divine Will. |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-12 : 07:07:07
|
Are the dates stored as DATETIME/SMALLDATETIME or INT?Peter LarssonHelsingborg, Sweden |
 |
|
Antonio
Posting Yak Master
168 Posts |
Posted - 2006-10-12 : 07:12:01
|
quote: Originally posted by Peso Are the dates stored as DATETIME/SMALLDATETIME or INT?Peter LarssonHelsingborg, Sweden
They are stored as integers._________________________________________________________________________________________________________________________Inability is a disaster; patience is bravery; abstinence is a treasure, self-restraint is a shield; and the best companion is submission to Divine Will. |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-12 : 07:15:49
|
Wrong answer! Wait some minutes...Peter LarssonHelsingborg, Sweden |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-12 : 07:33:31
|
[code]-- Prepare test dataDECLARE @Test TABLE (CostCentre INT, TargetDate INT)INSERT @TestSELECT 1180, 20061001 UNION ALLSELECT 1180, 20061002 UNION ALLSELECT 1180, 20061003 UNION ALLSELECT 1180, 20061005 UNION ALLSELECT 1180, 20061006 UNION ALLSELECT 1180, 20061008 UNION ALLSELECT 1180, 20061009 UNION ALLSELECT 1180, 20061010-- Prepare platformdeclare @Consecutive tinyint select @consecutive = 3-- Do the workselect d1.CostCentre, d1.TargetDatefrom ( SELECT DISTINCT CostCentre, DATEADD(month, TargetDate % 10000 / 100 - 1, DATEADD(day, TargetDate % 100 - 1, DATEADD(year, TargetDate / 10000 - 1900, 0))) TargetDate FROM @Test ) d1inner join ( SELECT DISTINCT CostCentre, DATEADD(month, TargetDate % 10000 / 100 - 1, DATEADD(day, TargetDate % 100 - 1, DATEADD(year, TargetDate / 10000 - 1900, 0))) TargetDate FROM @Test ) d2 on d2.costcentre = d1.costcentre and d2.targetdate between d1.targetdate and dateadd(day, @consecutive - 1, d1.targetdate)group by d1.CostCentre, d1.TargetDatehaving count(*) = @consecutive[/code]Peter LarssonHelsingborg, Sweden |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-12 : 08:03:42
|
Oops! I read your question more closely now, and had to rewrite last select to DISTINCT.-- Prepare test dataDECLARE @Test TABLE (CostCentre INT, TargetDate INT)INSERT @TestSELECT 1180, 20061001 UNION ALLSELECT 1180, 20061002 UNION ALLSELECT 1180, 20061003 UNION ALLSELECT 1180, 20061005 UNION ALLSELECT 1180, 20061006 UNION ALLSELECT 1181, 20061002 UNION ALLSELECT 1181, 20061003 UNION ALLSELECT 1181, 20061005 UNION ALLSELECT 1181, 20061006 UNION ALLSELECT 1181, 20061007 UNION ALLSELECT 1181, 20061008 UNION ALLSELECT 1181, 20061010-- Prepare platformdeclare @Consecutive tinyint select @consecutive = 3-- Do the workselect distinct d1.CostCentrefrom ( SELECT DISTINCT CostCentre, DATEADD(month, TargetDate % 10000 / 100 - 1, DATEADD(day, TargetDate % 100 - 1, DATEADD(year, TargetDate / 10000 - 1900, 0))) TargetDate FROM @Test ) d1inner join ( SELECT DISTINCT CostCentre, DATEADD(month, TargetDate % 10000 / 100 - 1, DATEADD(day, TargetDate % 100 - 1, DATEADD(year, TargetDate / 10000 - 1900, 0))) TargetDate FROM @Test ) d2 on d2.costcentre = d1.costcentre and d2.targetdate between d1.targetdate and dateadd(day, @consecutive - 1, d1.targetdate)group by d1.CostCentre, d1.TargetDatehaving count(*) = @consecutive Peter LarssonHelsingborg, Sweden |
 |
|
Antonio
Posting Yak Master
168 Posts |
Posted - 2006-10-12 : 08:04:05
|
quote: Originally posted by Peso
-- Prepare test dataDECLARE @Test TABLE (CostCentre INT, TargetDate INT)INSERT @TestSELECT 1180, 20061001 UNION ALLSELECT 1180, 20061002 UNION ALLSELECT 1180, 20061003 UNION ALLSELECT 1180, 20061005 UNION ALLSELECT 1180, 20061006 UNION ALLSELECT 1180, 20061008 UNION ALLSELECT 1180, 20061009 UNION ALLSELECT 1180, 20061010-- Prepare platformdeclare @Consecutive tinyint select @consecutive = 3-- Do the workselect d1.CostCentre, d1.TargetDatefrom ( SELECT DISTINCT CostCentre, DATEADD(month, TargetDate % 10000 / 100 - 1, DATEADD(day, TargetDate % 100 - 1, DATEADD(year, TargetDate / 10000 - 1900, 0))) TargetDate FROM @Test ) d1inner join ( SELECT DISTINCT CostCentre, DATEADD(month, TargetDate % 10000 / 100 - 1, DATEADD(day, TargetDate % 100 - 1, DATEADD(year, TargetDate / 10000 - 1900, 0))) TargetDate FROM @Test ) d2 on d2.costcentre = d1.costcentre and d2.targetdate between d1.targetdate and dateadd(day, @consecutive - 1, d1.targetdate)group by d1.CostCentre, d1.TargetDatehaving count(*) = @consecutive Peter LarssonHelsingborg, Sweden
Hi Peso,I have just tried your solution and it seems to work. I am now trying to understand what exactly is going on within the code. Will get back to you once I've understood the query.Thanks for your time and effort. Much appreciated! _________________________________________________________________________________________________________________________Inability is a disaster; patience is bravery; abstinence is a treasure, self-restraint is a shield; and the best companion is submission to Divine Will. |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-12 : 08:16:27
|
Here is a more effective query-- Prepare test dataDECLARE @Test TABLE (CostCentre INT, TargetDate INT)INSERT @TestSELECT 1180, 20061001 UNION ALLSELECT 1180, 20061002 UNION ALLSELECT 1180, 20061003 UNION ALLSELECT 1180, 20061003 UNION ALLSELECT 1180, 20061005 UNION ALLSELECT 1180, 20061006 UNION ALLSELECT 1181, 20061002 UNION ALLSELECT 1181, 20061003 UNION ALLSELECT 1181, 20061005 UNION ALLSELECT 1181, 20061006 UNION ALLSELECT 1181, 20061007 UNION ALLSELECT 1181, 20061008 UNION ALLSELECT 1181, 20061010-- Prepare platformdeclare @Consecutive tinyint select @consecutive = 3-- Do the workselect distinct d1.CostCentrefrom ( SELECT CostCentre, DATEADD(month, TargetDate % 10000 / 100 - 1, DATEADD(day, TargetDate % 100 - 1, DATEADD(year, TargetDate / 10000 - 1900, 0))) TargetDate FROM @Test ) d1inner join ( SELECT CostCentre, DATEADD(month, TargetDate % 10000 / 100 - 1, DATEADD(day, TargetDate % 100 - 1, DATEADD(year, TargetDate / 10000 - 1900, 0))) TargetDate FROM @Test ) d2 on d2.costcentre = d1.costcentre and d2.targetdate between d1.targetdate and dateadd(day, @consecutive - 1, d1.targetdate)group by d1.CostCentre, d1.TargetDatehaving count(distinct d2.targetdate) = @consecutive The reason for having derived tables is that your dates are stored int, and hence I can't check for if 20061101 is consecutive to 20061031 because of the 70 number differences.Peter LarssonHelsingborg, Sweden |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-10-13 : 10:36:25
|
>>They are stored as integers.Use proper Datatype DATETIMEMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|