Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-10-10
1
Medium Priority
?
306 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 15

Accepted Solution

by:
mish33 earned 1500 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

688 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