473,406 Members | 2,336 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Help finding 'pairs' of rows

I have a table with ID, Status and Date. The status is either Open or Close. For each ID there are multiple records. And there can be multiple entries with the same status but different times. I have to calculate the amount of time that the ID is in an open state. In the example below I have to match row 1 with row 2 and
and row 3 with row 5. In other words, I want the min of the open and the max of the closed that is less than the next open.
[code]
CREATE TABLE #Data( ID int, Status char(1), UpdateDate datetime)

INSERT #Data Values(1, '0', '3/1/2008')
INSERT #Data Values(1, 'C', '3/3/2008')
INSERT #Data Values(1, '0', '3/8/2008')
INSERT #Data Values(1, 'C', '3/9/2008')
INSERT #Data Values(1, 'C', '3/10/2008')
[code]

This is a far as I get.
[code]
SELECT r1.ID, r1.status AS StartStatus, r2.status AS EndStatus,
r1.UpdateDate AS StartOfWork,
r2.UpdateDate AS EndOfWork,
DateDiff(day,r1.UpdateDate, r2.UpdateDate) as LengthOfWork
FROM #data r1
JOIN #data r2 ON r1.ID = r2.ID
Where r1.UpdateDate = (Select Min(r3.UpdateDate)
FROM #data r3
WHERE r3.Status= 'O'
AND r3.ID = r1.ID
AND r3.UpdateDate <= r2.UpdateDate
)
AND r2.UpdateDate = (Select Max(r4.UpdateDate)
FROM #data r4
WHERE r4.rStatus ='C'
and r4.ID = r2.ID
AND r4.UpdateDate >= r1.UpdateDate
)
[code]

The results should be
1 O C 3/1/2008 3/3/2008
1 O C 3/8/2008 3/10/2008

I'm only getting one row.

Any help would be greatly appreciated.

Thanks,
Candi
Apr 5 '08 #1
3 3842
mafaisal
142 100+
Hello

I think U Have got Result is Like
1 O C 3/1/2008 3/10/2008

Bcoz U have Getting Min & Max Date is Taken By group By of ID
Here ID is only 1

So Change ur Iserted Data to

Expand|Select|Wrap|Line Numbers
  1. INSERT #Data Values(1, '0', '3/1/2008')
  2. INSERT #Data Values(1, 'C', '3/3/2008')
  3. INSERT #Data Values(2, '0', '3/8/2008')
  4. INSERT #Data Values(2, 'C', '3/9/2008')
  5. INSERT #Data Values(2, 'C', '3/10/2008')
  6.  
Then U Have Got 2 rows of Result

Like

1 O C 3/1/2008 3/3/2008
2 O C 3/8/2008 3/10/2008

Lets Try

Faisal


I have a table with ID, Status and Date. The status is either Open or Close. For each ID there are multiple records. And there can be multiple entries with the same status but different times. I have to calculate the amount of time that the ID is in an open state. In the example below I have to match row 1 with row 2 and
and row 3 with row 5. In other words, I want the min of the open and the max of the closed that is less than the next open.
[code]
CREATE TABLE #Data( ID int, Status char(1), UpdateDate datetime)

INSERT #Data Values(1, '0', '3/1/2008')
INSERT #Data Values(1, 'C', '3/3/2008')
INSERT #Data Values(1, '0', '3/8/2008')
INSERT #Data Values(1, 'C', '3/9/2008')
INSERT #Data Values(1, 'C', '3/10/2008')
[code]

This is a far as I get.
[code]
SELECT r1.ID, r1.status AS StartStatus, r2.status AS EndStatus,
r1.UpdateDate AS StartOfWork,
r2.UpdateDate AS EndOfWork,
DateDiff(day,r1.UpdateDate, r2.UpdateDate) as LengthOfWork
FROM #data r1
JOIN #data r2 ON r1.ID = r2.ID
Where r1.UpdateDate = (Select Min(r3.UpdateDate)
FROM #data r3
WHERE r3.Status= 'O'
AND r3.ID = r1.ID
AND r3.UpdateDate <= r2.UpdateDate
)
AND r2.UpdateDate = (Select Max(r4.UpdateDate)
FROM #data r4
WHERE r4.rStatus ='C'
and r4.ID = r2.ID
AND r4.UpdateDate >= r1.UpdateDate
)
[code]

The results should be
1 O C 3/1/2008 3/3/2008
1 O C 3/8/2008 3/10/2008

I'm only getting one row.

Any help would be greatly appreciated.

Thanks,
Candi
Apr 5 '08 #2
That doesn't work. I only posted a subset of the data - the data for one ID. The real table has over 100,000 rows for 22,000 different IDs. And there may be multiple entries for each status as in the example below where there ar 2 Cs for the second O. I need to transform the original table into a second table.

Original Table looks like
ID Status UpdateDate
1 O 2008-03-01 00:00:00
1 C 2008-03-03 00:00:00
1 O 2008-03-08 00:00:00
1 C 2008-03-09 00:00:00
1 C 2008-03-10 00:00:00

New Table looks like
ID StartDate EndDate
1 2008-03-01 00:00:00 2008-03-03 00:00:00
1 2008-03-08 00:00:00 2008-03-10 00:00:00

The dates are in US format.

I have also simplified the data I'm looking at and what I need to produce
down to the bare essentials for this post. There's a lot more to the problem
I'm really trying to solve, but once I know how to do this part, I think I can figure
out the rest on my own.

This has to run on both SQL 2000 and 2005.

Thanks for your help.

Candi
Apr 5 '08 #3
ck9663
2,878 Expert 2GB
That doesn't work. I only posted a subset of the data - the data for one ID. The real table has over 100,000 rows for 22,000 different IDs. And there may be multiple entries for each status as in the example below where there ar 2 Cs for the second O. I need to transform the original table into a second table.

Original Table looks like
ID Status UpdateDate
1 O 2008-03-01 00:00:00
1 C 2008-03-03 00:00:00
1 O 2008-03-08 00:00:00
1 C 2008-03-09 00:00:00
1 C 2008-03-10 00:00:00

New Table looks like
ID StartDate EndDate
1 2008-03-01 00:00:00 2008-03-03 00:00:00
1 2008-03-08 00:00:00 2008-03-10 00:00:00

The dates are in US format.

I have also simplified the data I'm looking at and what I need to produce
down to the bare essentials for this post. There's a lot more to the problem
I'm really trying to solve, but once I know how to do this part, I think I can figure
out the rest on my own.

This has to run on both SQL 2000 and 2005.

Thanks for your help.

Candi

Your second row says:

1 2008-03-08 00:00:00 2008-03-10 00:00:00

Isn't the close date be 2008-03-09 00:00:00 ? If not, then the 2008-03-09 00:00:00 would be an orphan record. A close record with no paired Open record.

-- CK
Apr 7 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Orion | last post by:
Hi, This is kind of last minute, I have a day and a half left to figure this out. I'm working on a project using ms-sqlserver. We are creating a ticket sales system, as part of the system, I...
5
by: brettr | last post by:
When I reference document.cookie, there is a long string of key=value; pairs listed. I may have 100 hundred cookies on my hard drive. However, most only have one key=value pair. Does the...
4
by: jrett | last post by:
I'm new to ASP.NET and fairly inexperienced with web development in general, but I've been a professional software dev for over 10 years, C++, Unix and windows, C# the past 4 years. I've been...
9
by: TF | last post by:
Hello all, I made a ASP.NET 2.0 site that shows possible "recipes" for paint colors stored in an access dbase. Basically, 1000 colors are stored with specific RGB values in separate columns. A...
4
by: danbuttercup | last post by:
Hi everyone I just recently learnt how to do while loops in my java class and I am completely lost. I have to make programs for the following questions but I have no idea were to start. ...
4
by: Thomas Arthur Seidel | last post by:
Hello to all, I have a small or big problem with a customer data base, where during a change of system we might have created duplicate records. This should be easy to find, you might think, but,...
2
by: yeshello54 | last post by:
so here is my problem...in a contact manager i am trying to complete i have ran into an error..we have lots of code because we have some from class which we can use...anyways i keep getting an error...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.