[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Is there a way to subtract one rowset from another

Posted on 2010-09-14
3
Medium Priority
?
304 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
[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
  • 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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A recent question popped up and the discussion heated up regarding updating a COMMENTS (TXT) field in a table using SSRS. http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/MS-SQL_Reporting/Q_27475269.html?cid=1572#a37227028 (htt…
In this article I will describe the Copy Database Wizard 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.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

656 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