Solved

Is there a way to subtract one rowset from another

Posted on 2010-09-14
3
293 Views
Last Modified: 2012-05-10
Hi - I have a query that returns a reulst set:
AcctNumber tdactdatetimenurpcwork
V04356634      2010-08-02 09:17:00.000
V04356634      2010-08-02 09:32:00.000
V04356634      2010-08-02 09:47:00.000
V04356634      2010-08-02 10:05:00.000
V04356634      2010-08-02 10:20:00.000
V04356634      2010-08-02 10:50:00.000
V04356634      2010-08-02 11:20:00.000
V04356634      2010-08-02 11:50:00.000
V04356634      2010-08-02 12:20:00.000

There are no unique idetifiers in the table other than the difference in time.  I need to know the difference in time between each row.  Any ideas?  I am writing in Visual Studio 2005.
SELECT    acctnumber,tdactdatetimenurpcwork

FROM         Picis.dbo.[Cases by Day] AS p LEFT OUTER JOIN

                          (SELECT     UrnADMPAT, AcctNumber, Location, Name, Status, CASE WHEN servicedate IS NULL 

                                                   THEN admitdate ELSE servicedate END AS AdmitDate

                            FROM          AdmPatientFile 

                            WHERE      (Status LIKE '%sdc%')) AS a ON a.AcctNumber = p.crp_acct_nbr LEFT OUTER JOIN

                          (SELECT     NURPCWORKDocumentedResults.PatientNURPCWORK, NURPCWORKDocumentedResults.IntBaseNURPCWORK, 

                                                   NURPCWORKDocumentedResults.IntUrnNURPCWORK, NURPCWORKDocumentedResults.TdActDateNURPCWORK, 

                                                   NURPCWORKDocumentedResults.TdActTimeNURPCWORK, NURPCWORKDocumentedResults.TdActCtrNURPCWORK, 

                                                   NURPCWORKDocumentedResults.TdQueryNURPCWORK, NURPCWORKDocumentedResults.TdValue, 

                                                   NURPCWORKDocumentedResults.tdactdatetimenurpcwork

                            FROM          NURPCWORKDocumentedResults INNER JOIN

                                                   NURPCWORKIntDocumentedActivity ON 

                                                   NURPCWORKDocumentedResults.PatientNURPCWORK = NURPCWORKIntDocumentedActivity.PatientNURPCWORK

                            WHERE      

                                                   (NURPCWORKDocumentedResults.IntBaseNURPCWORK = '25030')) AS n ON 

                      n.PatientNURPCWORK = a.UrnADMPAT

WHERE     (a.Location = 'ambsurg') AND ACCTNUMBER = 'V04356634'

group by acctnumber,tdactdatetimenurpcwork

ORDER BY TDACTDATETIMENURPCWORK

Open in new window

0
Comment
Question by:palmer9
  • 2
3 Comments
 
LVL 41

Expert Comment

by:ralmada
ID: 33674674
try
;with CTE as (

SELECT    acctnumber,tdactdatetimenurpcwork, row_number() over (order by tdactdatetimenurpcwork) rn

FROM         Picis.dbo.[Cases by Day] AS p LEFT OUTER JOIN

                          (SELECT     UrnADMPAT, AcctNumber, Location, Name, Status, CASE WHEN servicedate IS NULL 

                                                   THEN admitdate ELSE servicedate END AS AdmitDate

                            FROM          AdmPatientFile 

                            WHERE      (Status LIKE '%sdc%')) AS a ON a.AcctNumber = p.crp_acct_nbr LEFT OUTER JOIN

                          (SELECT     NURPCWORKDocumentedResults.PatientNURPCWORK, NURPCWORKDocumentedResults.IntBaseNURPCWORK, 

                                                   NURPCWORKDocumentedResults.IntUrnNURPCWORK, NURPCWORKDocumentedResults.TdActDateNURPCWORK, 

                                                   NURPCWORKDocumentedResults.TdActTimeNURPCWORK, NURPCWORKDocumentedResults.TdActCtrNURPCWORK, 

                                                   NURPCWORKDocumentedResults.TdQueryNURPCWORK, NURPCWORKDocumentedResults.TdValue, 

                                                   NURPCWORKDocumentedResults.tdactdatetimenurpcwork

                            FROM          NURPCWORKDocumentedResults INNER JOIN

                                                   NURPCWORKIntDocumentedActivity ON 

                                                   NURPCWORKDocumentedResults.PatientNURPCWORK = NURPCWORKIntDocumentedActivity.PatientNURPCWORK

                            WHERE      

                                                   (NURPCWORKDocumentedResults.IntBaseNURPCWORK = '25030')) AS n ON 

                      n.PatientNURPCWORK = a.UrnADMPAT

WHERE     (a.Location = 'ambsurg') AND ACCTNUMBER = 'V04356634'

group by acctnumber,tdactdatetimenurpcwork

)

select a.acctnumber,a.tdactdatetimenurpcwork, datediff(minute, a.tdactdatetimenurpcwork, b.tdactdatetimenurpcwork) as difference

from CTE a

left join CTE b on a.rn = b.rn + 1

ORDER BY TDACTDATETIMENURPCWORK

Open in new window

0
 
LVL 41

Accepted Solution

by:
ralmada earned 500 total points
ID: 33674693
sorry like this
;with CTE as (

SELECT    acctnumber,tdactdatetimenurpcwork, row_number() over (order by tdactdatetimenurpcwork) rn

FROM         Picis.dbo.[Cases by Day] AS p LEFT OUTER JOIN

                          (SELECT     UrnADMPAT, AcctNumber, Location, Name, Status, CASE WHEN servicedate IS NULL 

                                                   THEN admitdate ELSE servicedate END AS AdmitDate

                            FROM          AdmPatientFile 

                            WHERE      (Status LIKE '%sdc%')) AS a ON a.AcctNumber = p.crp_acct_nbr LEFT OUTER JOIN

                          (SELECT     NURPCWORKDocumentedResults.PatientNURPCWORK, NURPCWORKDocumentedResults.IntBaseNURPCWORK, 

                                                   NURPCWORKDocumentedResults.IntUrnNURPCWORK, NURPCWORKDocumentedResults.TdActDateNURPCWORK, 

                                                   NURPCWORKDocumentedResults.TdActTimeNURPCWORK, NURPCWORKDocumentedResults.TdActCtrNURPCWORK, 

                                                   NURPCWORKDocumentedResults.TdQueryNURPCWORK, NURPCWORKDocumentedResults.TdValue, 

                                                   NURPCWORKDocumentedResults.tdactdatetimenurpcwork

                            FROM          NURPCWORKDocumentedResults INNER JOIN

                                                   NURPCWORKIntDocumentedActivity ON 

                                                   NURPCWORKDocumentedResults.PatientNURPCWORK = NURPCWORKIntDocumentedActivity.PatientNURPCWORK

                            WHERE      

                                                   (NURPCWORKDocumentedResults.IntBaseNURPCWORK = '25030')) AS n ON 

                      n.PatientNURPCWORK = a.UrnADMPAT

WHERE     (a.Location = 'ambsurg') AND ACCTNUMBER = 'V04356634'

group by acctnumber,tdactdatetimenurpcwork

)

select a.acctnumber,a.tdactdatetimenurpcwork, b.tdactdatetimenurpcwork, datediff(minute, a.tdactdatetimenurpcwork, b.tdactdatetimenurpcwork) as difference

from CTE a

left join CTE b on a.rn + 1 = b.rn

ORDER BY TDACTDATETIMENURPCWORK

Open in new window

0
 

Author Comment

by:palmer9
ID: 33674710
Genius - Thank you SO MUCH!!!
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Introduction In the following article I’ll be discussing and demonstrating several different ways of how images can be put on a report. I’m using SQL Server Reporting Services 2008 R2 CTP, more precisely version 10.50.1352.12, but the methods ex…
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 tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

912 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now