Solved

How do I calculate the datediff between multiple rows in a table when rowcount is unknown?

Posted on 2008-10-10
1
294 Views
Last Modified: 2012-05-05
I am trying to calculate the days that a record has been in a current status.  I have a field that displays the change date when the status changes.  When there are multiple rows for an ID,  I will need to compare the amount of time that passed since the last change date.  When there is only 1 row for the ID, I would just compare the change date to the current date.  As you can see in the snippet, I am trying to also exclude weekends from the count.

Any assistance will be greatly appreciated.
DAYS_IN_STATUS = IF @@ROWCOUNT = 1 (SELECT DATEDIFF(dw,AU_TIME,GETDATE())-(DATEDIFF(wk,AU_TIME,GETDATE())*2) AS DAYS_IN_STATUS 
ELSE ??

Open in new window

0
Comment
Question by:testergirl
1 Comment
 
LVL 15

Accepted Solution

by:
mish33 earned 500 total points
ID: 22688280
Fold multiple rows into one:

select id, min(au_time) from tbl group by id

and then use your formula.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Character matching different date formats for dates between 6 49
SQL Sub-Query Help 22 62
SQL Insert parts by customer 12 36
ServiceCenter IR Query Expressions 1 25
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question