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
3 3842
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 -
INSERT #Data Values(1, '0', '3/1/2008')
-
INSERT #Data Values(1, 'C', '3/3/2008')
-
INSERT #Data Values(2, '0', '3/8/2008')
-
INSERT #Data Values(2, 'C', '3/9/2008')
-
INSERT #Data Values(2, 'C', '3/10/2008')
-
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
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
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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.
...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |