Need SQL For Finding Gaps Comparing Two Sets Of Periods
Feb 22, 2008
Hi all,
I have two tables - Planning and Appointments:
Planning - contains a list of planned items. Used to define boundaries for a work day and defines based on type what can be done for each item.
Id,
TypeId - the type of the planned items
BeginTime DateTime - begin date and time of the planned item
EndTime DateTime - end date and time for the planned item
In the Planning table we can have as many records per day as we need:
1, First Meeting, 1 Jan 2008 09:00, 1 Jan 2008 11:00
2, First Meeting, 1 Jan 2008 11:00, 1 Jan 2008 12:00
3, First Meeting, 1 Jan 2008 13:00, 1 Jan 2008 15:00
4, First Meeting, 1 Jan 2008 15:00, 1 Jan 2008 18:00
Appointments - contanis a list with appointments
Id,
BeginTime DateTime
EndTime DateTime
1, 1 Jan 2008 09:00, 1 Jan 2008 09:30
2, 1 Jan 2008 10:00, 1 Jan 2008 11:00
3, 1 Jan 2008 11:00, 1 Jan 2008 11:30
4, 1 Jan 2008 14:00, 1 Jan 2008 15:30
What is needed?
What I need is to a find a way to compare the planned items with the appointments and to return all the periods for which a planned time exists:
Free planned time:
1, 1 Jan 2008 09:30, 1 Jan 2008 10:002, 1 Jan 2008 11:30, 1 Jan 2008 12:00
3, 1 Jan 2008 13:00, 1 Jan 2008 14:00
4, 1 Jan 2008 15:30, 1 Jan 2008 18:00
So, having two multitudes of periods,where the one specifies the planning templates and the other real used time, I need to find all the periods which can be used for another appointments.
I've tried several aproaches, but I always faced performance problems.
Thanks in advance.
View 1 Replies
ADVERTISEMENT
Feb 27, 2008
Hi all,
I have two tables - Planning and Appointments:
Planning - contains a list of planned items. Used to define boundaries for a work day and defines based on type what can be done for each item.
Id,
TypeId - the type of the planned items
BeginTime DateTime - begin date and time of the planned item
EndTime DateTime - end date and time for the planned item
In the Planning table we can have as many records per day as we need:
1, First Meeting, 1 Jan 2008 09:00, 1 Jan 2008 11:00
2, First Meeting, 1 Jan 2008 11:00, 1 Jan 2008 12:00
3, First Meeting, 1 Jan 2008 13:00, 1 Jan 2008 15:00
4, First Meeting, 1 Jan 2008 15:00, 1 Jan 2008 18:00
Appointments - contanis a list with appointments
Id,
BeginTime DateTime
EndTime DateTime
1, 1 Jan 2008 09:00, 1 Jan 2008 09:30
2, 1 Jan 2008 10:00, 1 Jan 2008 11:00
3, 1 Jan 2008 11:00, 1 Jan 2008 11:30
4, 1 Jan 2008 14:00, 1 Jan 2008 15:30
What is needed?
What I need is to a find a way to compare the planned items with the appointments and to return all the periods for which a planned time exists:
Free planned time:
1, 1 Jan 2008 09:30, 1 Jan 2008 10:00
2, 1 Jan 2008 11:30, 1 Jan 2008 12:00
3, 1 Jan 2008 13:00, 1 Jan 2008 14:00
4, 1 Jan 2008 15:30, 1 Jan 2008 18:00
So, having two multitudes of periods,where the one specifies the planning templates and the other real used time, I need to find all the periods which can be used for another appointments.
I've tried several aproaches, but I always faced performance problems.
Thanks in advance.
View 1 Replies
View Related
Nov 9, 2006
hi guys,
i have a booking table which has the following columns...
booking
-------------------------------------------
dCheckin (format 11/9/2006 12:00:00 AM)
dCheckout (format 11/11/2006 12:00:00 AM)
when a new booking is entered, we want to make sure that the period entered does not conflict with an existing record.
not sure how to go about building the query required. any help would be greatly appreciated.
mike
View 4 Replies
View Related
Mar 25, 2014
I'm trying to find gaps in times in a table of sessions where the session endings aren't sequential. That is, session 1 can start at 10:00 and finish at 10:30, while session 2 started at 10:05 and finished at 10:45, and session 3 started at 10:06 and finished at 10:20. Only the starting times are in order; the ending times can be anywhere after that.Here's a bunch of sample data:
CREATE TABLE #SessionTest(SessionId int,Logindatetime datetime, Logoutdatetime datetime)
INSERT INTO #SessionTest
SELECT '1073675','Mar 3 2014 1:53PM','Mar 3 2014 1:53PM' UNION ALL
SELECT '1073676','Mar 3 2014 2:26PM','Mar 3 2014 3:51PM' UNION ALL
SELECT '1073677','Mar 3 2014 2:29PM','Mar 3 2014 3:54PM' UNION ALL
SELECT '1073678','Mar 3 2014 2:29PM','Mar 3 2014 5:47PM' UNION ALL
SELECT '1073679','Mar 3 2014 2:30PM','Mar 3 2014 3:37PM' UNION ALL
[code]....
View 6 Replies
View Related
Sep 13, 2013
I have a group of date ranges and wanted to identify all of the date gaps within the ranges, outputting the dates as another date range dataset.
Example dataset SQL below:
CREATE TABLE #test (daterow int identity, obj_id int, datestart DATETIME, dateend DATETIME)
INSERT INTO #test
SELECT 1, '20130428', '20130523'
UNION
SELECT 1, '20130526', '20130823'
[Code] ....
I would expect a dataset to be returned consisting of:
1, 24/05/2013, 25/05/2013
1, 24/08/2013, 25/08/2013
2, 16/05/2013, 24/05/2013
I have found a lot of examples of problems where I have just a single date column, and then I find the gaps in between that, but I'm having difficulty finding examples where it works with start and end date columns...
View 9 Replies
View Related
Aug 26, 2015
currently I am facing a complex escenario related with gaps and sequences, but I was trying with diferent cases but I did not get the correct results, I am sure about the use of windows functions. I have a table with the information grouped by PublicationId, Provider, MetricId and Amount by Date, one row by each month, but in some cases these data don't have a sequencial values, for example I have the data for the next sequence:
I need to get the sequence by each month, in this case I need to project the month from February to May (with the last previous value, for this case of January) , this is:
The data for testing are:
DECLARE @PublicationsByUser AS TABLE
(
Id INT,
PublicationId INT,
MetricId INT,
ProviderId INT,
DateCreated DATE,
Amount FLOAT
[code]....
View 14 Replies
View Related
Jul 23, 2005
I have the following situation. One set of data has 274 rows (set2)and anther has 264 (set1). Both data sets are similar in structure aswell as values for both of them were extracts from the same parenttable. Hope the info would substitute DDL. I need to find the "gap"rows between these two sets.Attempted to run a query likeselect count(*)from set2where not exists(select *from set1)did not yield what I desired. What else to try?TIA.
View 12 Replies
View Related
Jul 20, 2005
What is the best way to compare two entries in a single table wherethe two fields are "almost" the same?For example, I would like to write a query that would compare thefirst two words in a "company" field. If they are the same, I wouldlike to output them.For example, "20th Century" and "20th Century Fox" in the companyfield would be the same.How do I do this? Do I need to use a cursor? Is it as simple as using"Like?"
View 2 Replies
View Related
Nov 20, 2015
I have this 40,000,000 rows table... I am trying to clean this 'Contacts' table since I know there are a lot of duplicates.
At first, I wanted to get a count of how many there are.
I need to compare records where these fields are matched:
MATCHED: (email, firstname) but not MATCH: (lastname, phone, mobile).
MATCHED: (email, firstname, mobile)
But not MATCH: (lastname, phone)
MATCHED: (email, firstname, lastname)
But not MATCH: (phone, mobile)
View 9 Replies
View Related
Mar 10, 2008
Hi
I am trying to compare two flat files and extract new entry into new file.But in my case there is no key column in both flat files. is any way to find the new entry by checksum with out Key matching?.
Thanks
Guru
View 3 Replies
View Related
Jun 20, 2007
Hi, this is my first post here, i'm working on a view wich I have to display the results by period.
1st period - Current month
2nd period - Last month
3rd period - Month before last
and so on.
How can I achieve this? of course I have a column wich has the date I want to use, I was thinking on semthing like:
case when DATEDIFF(M, TRXDATE, getdate()) <= 0 then ammount END as Period1
(TRXDATE and ammount are columns on my table)
Any ideas? would this work?
Thanks for your replies
View 2 Replies
View Related
May 4, 2006
I have to do alot of date calculations. For some calculations, I can use the datediff or dateadd function to get a Time Period between 2 dates.
Now for some dates I need to calculate the time between 2 dates BUT:
a week = 5 days starting from Monday to Friday
a day starts at 8AM and ends at 6PM (so a day is 10 hours)
You can probably calculate this manually but what about summer to winter hour and the month Februari when it has 29 days etc.
So I was thinking ... is it possible to calculate the Time Period for weeks with 24h a day / 7 days AND then transform that Time Period to a time period for weeks with 10h a day / 5 days ?
If anyone has an idea to solve this, either with functions or an other way, please let me know! Thanks
View 3 Replies
View Related
Oct 10, 2005
Hi,
I need to compare 2 periods (start date / end date) in order to find out if the first period overlaps or in included in the second period.
any idea ???
thanks
View 14 Replies
View Related
Jan 10, 2008
There are three tables Loans, CDs and Account.
I want to create a report based on above three tables
It looks like as follows;
Program Summary
Yesterday/Current
MTD
Previous Month
YTD
Amount
Number
Amount
Number
Amount
Number
Amount
Number
Portfolio
Loans Commenced
$
#
$
#
$
#
$
#
Loans Closed
$
#
$
#
$
#
$
#
CDs Commenced
$
#
$
#
$
#
$
#
CDs Closed
$
#
$
#
$
#
$
#
Loan table - Amount, State
CD - Amount, State
Account - CommencedOnDate(date time), closeondate(date time)
Yesterday - Loans/CDs started yesterday(calander date..not last 24 hours)
MTD - " " First of this month to yesterday
Previous Month - " " during previous month
YTD - " " Loans/CDs started from 1 of this year to yesterday
Loan and cd states are Commenced and Closed
Based on this I have to create a report. But this time format seems to be really complex.
Can anyone help me to write the query pls?
View 1 Replies
View Related
Aug 30, 2007
Greetings,
I am new to SQL Server. I've created a database with a key ID field that is set to automatically increment. Well, after adding records I've got some gaps in my numbering and want to renumber from 1 to eof.
What is the best way to do this in SQL Server 2005?
Thank you.
View 4 Replies
View Related
Feb 10, 2006
Guys
Date brain killer - I have a startdate and enddate and need, for a given period length in months, to identify the number
of periods betwen the startdate and enddate
so for example
DECLARE @Periods TABLE
(
StartDate datetime,
Enddate datetime,
NumberOfMonthsInPeriod INT ,
TotPeriodsINT
)
-- Initial data
insert @Periods
(
StartDate ,
Enddate ,
NumberOfMonthsInPeriod ,
TotPeriods-- for this illustration, initialised to 0 but need to be UPDATEd as per detail below
)
select
'30-Sep-2005',
'10-Apr-2009',
1 -- 1 month period
0
union all
select
'30-Sep-2005',
'10-Apr-2009',
3 -- 3 month period
0
union all
select
'30-Sep-2005',
'10-Apr-2009',
6 -- 6 month period
0
The following rules regarding periods apply
Each 1 (NumberOfMonthsInPeriod) month period is as would be expected ie
01-Jan - 31 Jan
01-Feb - 28-Feb (ie 1st March - 1 day which would deal with leap years - dateadd(...)
and so on to December
Each 3 (NumberOfMonthsInPeriod) month period is one of the following 'bands' per year
01-Jan - 31-Mar
01-Apr - 30-Jun
01-Jul - 30-Sep
01-Oct - 31-Dec
Each 6 (NumberOfMonthsInPeriod) month period is one of the following
01-Jan - 30-Jun
01-Jul - 31-Dec
I need to derive TotPeriods as follows:
For the row where NumberOfMonthsInPeriod = 1, the first period ie the one the start date falls within is 01-Sep-2005 to 30-Sep-2005, second is 01-Oct-2005 - 31-Oct-2005 and so on until
last period ie the one the end datye falls within is 01-Apr-2009 - 30-Apr-2009, a TotPeriods value of 44
For the row where NumberOfMonthsInPeriod = 3, first period is 01-Jul-2005 to 30-Sep-2005, second is 01-Oct-2005 - 31-Dec-2005 and so on until
last period is 01-Apr-2009 - 30-Jun-2009, a TotPeriods value of 16
For the row where NumberOfMonthsInPeriod = 6, first period is 01-Jul-2005 to 31-Dec-2005, second is 01-Jan-2006 - 30-Jun-2006 and so on until
last period is 01-Jan-2009 - 30-Jun-2009, a TotPeriods value of 8
Hope this is clear and thanks in advance
View 3 Replies
View Related
Mar 28, 2014
I have a Column in my data that gives a financial period value in the YYYYMM format. i.e. an asset was re-valued in a particular period for example 201301. I need to find out the number(count) of periods(months) between another given period for example current period (201403) and the period provided in the table i.e. 201301.
Is this possible in the SQL Database?
View 2 Replies
View Related
Apr 11, 2008
Your help is greatly appreciated. How do you query each one of the following:
1) SELECT FieldValue WHERE DateTime is from 8:30AM - 12:00PM and 6:00PM - 9:30PM on Mondays thru Fridays (i.e. exclude Sat & Sun) for the whole month of January 2008.
2) SELECT FieldValue WHERE DateTime is from 9:30PM - 8:30AM on Mondays thru Fridays, AND ALL-DAY on Saturdays & Sundays for the whole month of January 2008.
Thank you in advance.
View 8 Replies
View Related
Aug 17, 2007
Hi there.
I am working on a set of reports where I am summing/averaging data elements based on what period they are in. For example, the report output should look something like this:
Period
Sum
May '07
41
April '07
14
Q2 '07
55
March '07
36
February '07
28
January '07
22
Q1 '07
86
June '07
N/A
YTD '07
141
December '06
33
November '06
27
October '06
42
Q4 '06
102
September '06
58
August '06
84
July '06
52
Q3 '06
194
June '06
40
May '06
41
April '06
14
Q2 '06
95
March '06
67
February '06
38
January '06
N/A
Q1 '06
105
YTD '06
496
For each of the items I am summing, all I have is a datetime of when the event happened. This is a relational database (not a cube), so I am struggling with how to create the 'buckets' based on period. I think the best way is to dynamically create the buckets based on a given date. Is there a way in RS that it can do this bucketing for you?
Thanks, Mike
View 4 Replies
View Related
Dec 13, 2004
hello, i have quite a challenge on my hands here and would appreciate any help. :confused:
I have a table variable that stores integer ranges representing times of the day:
select * from @reservations
room date | starttime | endtime
1 2004-12-11 0 1440 (represents an entire day in minutes)
2 2004-12-12 420 1020
3 2004-12-14 200 600
4 2004-12-15 0 200
4 2004-12-15 500 1000
I need to be able to return the minutes that are open for each room. The @reservations table shows me the times that are blocked.
I'd like to analyze each row and return an integer range representing gaps in the day, where 0-1440 represents an entire day.
Based on the @reservations table above, I'd like to write something that returns:
room date starttime endtime
2 2004-12-12 0 420
2 2004-12-12 1020 1440
3 2004-12-14 0 200
3 2004-12-14 600 1440
4 2004-12-15 200 500
4 2004-12-15 1000 1440
This result represents the times in minutes that are available.
I have no clue how to do this without using a numbers table and checking each minute in each day for each row in the table. Id like to not do that because of sheer performance reasons. There is a possiblity that I will have hundreds of rows in the @reservations table.
I was hoping someone could provide some insight as to how to approach this. Thank you ahead of time! :)
View 3 Replies
View Related
Apr 6, 2008
Hi,
I have a table with an identity column..How will the identity gaps be adjusted if i delete few records in the table..ie..the sequence should automatically adjusted..Is there any way for this ?
ID Name City
1 abc xyz
2 mexm mcel
3 olekc kcome
Suppose i delete the record where ID=2..still the sequence should be auto adjusted..ie.the record of ID=3 should become ID=2 automatically..there shouldn't be any gaps.
View 8 Replies
View Related
Jul 20, 2007
I have a row in my report that has 6 columns. I want to outline the row with a border, but not the column lines in between. So, I went into BorderColor, changed the Default to white and my Top and Bottom colors as Black. The problem is that where the "white" column lines are, they are displaying 1pt gaps in my outline. I tried changing the BorderStyle to Zero, but it wouldn't take it.
See sample of the report here.
Any ideas?
Thanks,
James H.
View 6 Replies
View Related
Jan 14, 2008
Hi developers and architects,
I would like to know if it's possible to do "a gap to the left" in transact sql...
I have a table with:
ID Level1 Level2 Level3 Level4
1 NULL NULL 1 2
2 NULL 1 2 3
My results should be:
ID Level1 Level2 Level3 Level4
1 1 2 2 2
2 1 2 3 3
Currently I'm using a cursor to do that but I haven't good performance.
Do you have ideas?
Thanks a lot,
Jérémie
View 3 Replies
View Related
Jan 31, 2008
When I try to export my new report to PDF, I get extra blank lines in my list(s). My report has a list, with another list inside of it (among other textboxes). When I generate the report, or print it to a printer, it prints as expected. If I export to PDF, it adds in a bunch of blank lines into some of the inner lists. It doesn't seem to reflect any issue with the data, but it is consistent where it puts the spaces. Anyone know of any settings I might be missing?
View 3 Replies
View Related
Mar 24, 2006
Hi,I'm working on a simple performance-program, where I need to extractinformation from the 2 newest periods for every performance-indicator- And from there calculate a trend between these results.The problem is, that I can't find a simple way to extract the 2 latestresults.The Table (Table1) looks like this:kpiIDperiodIDActualAcceleration23Acceleration54Speed1100Speed4200Speed7220Speed9180Weight122Weight332Weight721Weight1033If I want to extract the newest I use something like this (made it inMS Access, so the syntax might differ slightly from SQLServer):SELECT table1.kpiID, table1.periodID, table1.ActualFROM table1 WHERE table1.periodID = (SELECT max(t.periodID) fromtable1 as t WHERE t.kpiID=table1.kpiID);BUT - how how do I get the second-newest period as well?Preferably I would like the final result to be a View with thefollowing fields:kpiID, periodID_newest, Actual_newest, periodID_sec_newest,Actual_sec_newestAlternatively a View with 2 posts for each performace-indicator.Thanks in advanceRyan
View 10 Replies
View Related
Jan 10, 2008
There are three tables Loans, CDs and Account.
I want to create a report based on above three tables
It looks like as follows;
Program Summary
Yesterday/Current
MTD
Previous Month
YTD
Amount
Number
Amount
Number
Amount
Number
Amount
Number
Portfolio
Loans Commenced
$
#
$
#
$
#
$
#
Loans Closed
$
#
$
#
$
#
$
#
CDs Commenced
$
#
$
#
$
#
$
#
CDs Closed
$
#
$
#
$
#
$
#
Loan table - Amount, State
CD - Amount, State
Account - CommencedOnDate(date time), closeondate(date time)
Yesterday - Loans/CDs started yesterday(calander date..not last 24 hours)
MTD - " " First of this month to yesterday
Previous Month - " " during previous month
YTD - " " Loans/CDs started from 1 of this year to yesterday
Loan and cd states are Commenced and Closed
Based on this I have to create a report. But this time format seems to be really complex.
Can anyone help me to write the query pls?
View 7 Replies
View Related
Dec 13, 2006
Is there an elegant way to fill the empty space leading a textbox in a table with periods, similar to a table of contents effect?
When I do not allow the textbox to grow and just append a long string of periods it looks fine in my report preview, but after I deploy everything appended after the primary field in that text box is missing?
My report has a lot of data to the right but it is collapsible so the fields to the left are a good distance away. This is why I am trying to include light visual aids that assist lining up data values. I am not stuck on the leading period idea but it seems the least cluttered.
Aaron
View 2 Replies
View Related
Oct 7, 2014
How can I compute the biweekly periods when a date is passed in as a parameter, for example if the user enters 9/12/2014 I should get:
Friday /2nd half9/12/2014
Saturday9/13/2014
Sunday9/14/2014
Monday9/15/2014
Tuesday9/16/2014
Wednesday9/17/2014
Thursday9/18/2014
Friday/1st half 9/19/2014
Friday/2nd half9/19/2014
Saturday9/20/2014
Sunday9/21/2014
Monday9/22/2014
Tuesday9/23/2014
Wednesday 9/24/2014
Thursday9/25/2014
Friday/1st half 9/26/2014
View 2 Replies
View Related
Jan 21, 2014
I'm trying to create a cte to return a list of lots and every period between the first and last transaction date for each lot. I've gotten this far:
SELECTIntLotKey
,DATEPART(YYYY, StartDate)StartYear
,DATEPART(MM, StartDate)StartPeriod
,DATEPART(YYYY, EndDate)EndYear
,DATEPART(MM, EndDate)EndPeriod
[Code] ....
This gives me the following results:
IntLotKeyStartYearStartPeriodEndYearEndPeriod
271532013120135
28468201312201312
2846920131201312
2847020131201312
28472201312201312
593022013120131
593032013120131
Now what I need is something that looks like this:
LotKeyYearPeriod
2715320131
2715320132
2715320133
2715320134
2715320135
[Code] .....
Some lots may not have any transactions for some of the periods between the start and end dates but I need to report every period between the start and end period for each lot. I have a period table that I thought I could use but haven't come up with a way to get the results I'm after.
View 9 Replies
View Related
Aug 20, 2015
Question: How to determine if a date value was between one of the date periods that appear in multiple rows?
Background: We have a table of "license valid" periods, wherein each license can have one or more rows. (As you know, a driver's license can be started, expired, renewed, suspended, reinstated, revoked, etc.) Instead of of having a license activity table--from which valid license periods could be extrapolated--we store just the periods for which a license was valid.
My task is to take a list of licenses and specific dates and determine if each license was valid as of that date, returning either true or false. What is the best way to accomplish this?
DECLARE @ValidityInQuestion TABLE (
LicenseID int
, DateValidityInQuestion date);
DECLARE @LicenseValidPeriods TABLE (
LicenseID int
, BeginDate date
, EndDate date);
[Code] ...
How then do I query both tables in order to get the same result that results from the following query?
SELECT
12345 AS LicenseID
, '2015-01-15' AS DateValidityInQuestion
, 1 AS LicenseActive
UNION
SELECT
67890
, '2015-02-04'
, 0;
I assume I need to join on the LicenseID columns, but my understanding stops there.
View 4 Replies
View Related
Aug 12, 2015
I have a [Fin Period] column which outputs a number between 1 and 12 depending on the financial period. For numbers 0-9 this is output as a single digit. How can I format this so it places a 0 in front of these numbers but not in front of periods 10-12?
View 2 Replies
View Related
Jan 30, 2005
I need to write a procedure to check if I have any gaps in my dates...
Start Date............End Date
10/6/2004............10/6/2005
10/6/2003............10/6/2004
7/10/2003..........10/6/2003
7/10/2002.............7/10/2003
What I need to do is test the End Date to the next row under the Start Date Column. (in bold to clarify) (moving upward from bottom)
sSQL011$ = "SELECT * FROM TableName " & _
"WHERE ID = '" & ID & "'"
Set rs011 = DB.OpenRecordset(sSQL011$, dbOpenDynaset)
Set Data1.Recordset = rs011
rs011.FindFirst "ID = " & ID
With rs011
do while .eof = False
if .eof then
exit sub
end if
if rs011("StartDate") 'from one record, if it is not equal to the Start
Date in the next record then
MsgBox "Gap in Dates for: " + ID
loop
end with
pseudo...
if the end date in one consecutive row is not the same as the start date in the next row then there is a date gap
thanks...
View 3 Replies
View Related
Jun 19, 2012
Create a table with an Identity column, insert data / restart the server / insert more data / restart the server/ insert some more data.
My data looks like this :
Identity column
1
2
3
1002
1003
1004
1005
2002
2004
It looks like the indentity value gain +~1000 after most server restart (sometimes identity stay the same). This can be very dangerous for some datatype! The only thing google told me was this : URL...but microsoft did not comment on it yet!
View 9 Replies
View Related