Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Calculate days


Calculate days

Author
Message
adonetok
adonetok
Old Hand
Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)

Group: General Forum Members
Points: 379 Visits: 751
I need help to code days. As the sample data below, days between [ADMDATE] and [DSCHDATE] need to be count as this way (at different rows):

MEMBID ADMDATE DSCHDATE
06000248 08/02/2010 08/04/2010
06000248 08/10/2010 08/12/2010 (08/10/2010 - 08/04/2010=6)
06000248 08/20/2010 08/22/2010 (08/20/2010 - 08/12/2012=8)

The final result is 6 and 8.
DataAnalyst011
DataAnalyst011
SSC-Enthusiastic
SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)

Group: General Forum Members
Points: 112 Visits: 426
EDITED: Yeah, what Lynn said below. That's why I'm the Grasshopper and he's the coach :-)
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24177 Visits: 37948
Not difficult. One thing you need to do is to look carefully at my code below. You notice I am creating the table, populating it with sample data, and after my query I delete the table used for development. The first 2 items and the last you should provide instead of relying on us to do it for you. So happens, it is a slow day today but don't always count on that.



/*
MEMBID ADMDATE DSCHDATE
06000248 08/02/2010 08/04/2010
06000248 08/10/2010 08/12/2010 (08/10/2010 - 08/04/2010=6)
06000248 08/20/2010 08/22/2010 (08/20/2010 - 08/12/2012=8)
*/

CREATE TABLE dbo.PatientInfo (
MEMBID CHAR(8),
ADMDATE DATE,
DSCHDATE DATE
);
go

INSERT INTO dbo.PatientInfo
VALUES ('06000248','08/02/2010','08/04/2010'),
('06000248','08/10/2010','08/12/2010'),
('06000248','08/20/2010','08/22/2010');
GO

WITH BaseData AS (
SELECT
ROW_NUMBER() OVER (PARTITION BY MEMBID ORDER BY DSCHDATE) rn,
MEMBID,
ADMDATE,
DSCHDATE
FROM
dbo.PatientInfo
)
SELECT
bd1.MEMBID,
bd1.ADMDATE,
bd1.DSCHDATE,
ISNULL(DATEDIFF(dd, bd2.DSCHDATE, bd1.ADMDATE),0) DaysOut
FROM
BaseData bd1
LEFT OUTER JOIN BaseData bd2
ON (bd1.rn = bd2.rn + 1)
;
GO

DROP TABLE dbo.PatientInfo;
GO




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
adonetok
adonetok
Old Hand
Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)

Group: General Forum Members
Points: 379 Visits: 751
Thank you. I almost get there.
But, if I inserted more values, the duplicate result will displays.

How to fix it?

INSERT INTO dbo.PatientInfo
VALUES ('06000248','08/02/2010','08/04/2010'),
('06000248','08/10/2010','08/12/2010'),
('06000248','08/20/2010','08/22/2010'),
('06000348','08/02/2011','08/04/2011'),
('06000348','08/10/2011','08/12/2011'),
('06000348','08/20/2011','08/22/2011');
GO

MEMBID ADMDATE DSCHDATE DaysOut
06000248 2010-08-02 2010-08-04 0
06000248 2010-08-10 2010-08-12 6
06000248 2010-08-10 2010-08-12 -359
06000248 2010-08-20 2010-08-22 8
06000248 2010-08-20 2010-08-22 -357
06000348 2011-08-02 2011-08-04 0
06000348 2011-08-10 2011-08-12 371
06000348 2011-08-10 2011-08-12 6
06000348 2011-08-20 2011-08-22 373
06000348 2011-08-20 2011-08-22 8
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24177 Visits: 37948
adonetok (8/14/2012)
Thank you. I almost get there.
But, if I inserted more values, the duplicate result will displays.

How to fix it?

INSERT INTO dbo.PatientInfo
VALUES ('06000248','08/02/2010','08/04/2010'),
('06000248','08/10/2010','08/12/2010'),
('06000248','08/20/2010','08/22/2010'),
('06000348','08/02/2011','08/04/2011'),
('06000348','08/10/2011','08/12/2011'),
('06000348','08/20/2011','08/22/2011');
GO

MEMBID ADMDATE DSCHDATE DaysOut
06000248 2010-08-02 2010-08-04 0
06000248 2010-08-10 2010-08-12 6
06000248 2010-08-10 2010-08-12 -359
06000248 2010-08-20 2010-08-22 8
06000248 2010-08-20 2010-08-22 -357
06000348 2011-08-02 2011-08-04 0
06000348 2011-08-10 2011-08-12 371
06000348 2011-08-10 2011-08-12 6
06000348 2011-08-20 2011-08-22 373
06000348 2011-08-20 2011-08-22 8


You tell me, are duplicate records allowed? Your original sample data set did not have duplicates. If duplicates can exist, what are your business rules regarding them, can you eliminate them prior to processing or do you have work them and if so what are the rules for dealing with duplicates.

Nevermind the above, small font on a small screen (laptop) didn't realize the account numbers were different. Yes, to the below, we need to add a bit more to the join criteria.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
roryp 96873
roryp 96873
SSC Eights!
SSC Eights! (843 reputation)SSC Eights! (843 reputation)SSC Eights! (843 reputation)SSC Eights! (843 reputation)SSC Eights! (843 reputation)SSC Eights! (843 reputation)SSC Eights! (843 reputation)SSC Eights! (843 reputation)

Group: General Forum Members
Points: 843 Visits: 6597
I think just doing the self join (the left join) on MEMBID and the row number to row number + 1 will solve the problem in the second set of sample data posted.
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24177 Visits: 37948
Here is the updated code:



CREATE TABLE dbo.PatientInfo (
MEMBID CHAR(8),
ADMDATE DATE,
DSCHDATE DATE
);
go

INSERT INTO dbo.PatientInfo
VALUES ('06000248','08/02/2010','08/04/2010'),
('06000248','08/10/2010','08/12/2010'),
('06000248','08/20/2010','08/22/2010'),
('06000348','08/02/2011','08/04/2011'),
('06000348','08/10/2011','08/12/2011'),
('06000348','08/20/2011','08/22/2011');
GO



WITH BaseData AS (
SELECT
ROW_NUMBER() OVER (PARTITION BY MEMBID ORDER BY DSCHDATE) rn,
MEMBID,
ADMDATE,
DSCHDATE
FROM
dbo.PatientInfo
)
SELECT
bd1.MEMBID,
bd1.ADMDATE,
bd1.DSCHDATE,
ISNULL(DATEDIFF(dd, bd2.DSCHDATE, bd1.ADMDATE),0) DaysOut
FROM
BaseData bd1
LEFT OUTER JOIN BaseData bd2
ON (bd1.rn = bd2.rn + 1
AND bd1.MEMBID = bd2.MEMBID)
;
GO

DROP TABLE dbo.PatientInfo;
GO




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
adonetok
adonetok
Old Hand
Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)

Group: General Forum Members
Points: 379 Visits: 751
It works great.
Thank you again
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search