Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Patients who come in within 14 days Expand / Collapse
Author
Message
Posted Wednesday, September 15, 2010 4:27 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, December 04, 2012 11:52 AM
Points: 26, Visits: 80
Dear All,

I'm trying to wriite sql query and i would like to know how would write the following in sql:

anyone with a crn and admdate who has a record within 14 days admdate between a 5 month period.

so far i am here:

SELECT    crn, admission,  admdate, admtime, disdate, admsource, admtype
FROM mfCarlRepos.dbo.admissions as table_name1
WHERE (admdate >= '2010-04-01' AND admdate <= '2010-08-31')

The above shows me all records between april and august with criteria in admdate. My next step is to show that any CRN that has admdate who has come in again within 14 days and display those records.

regards

F
Post #986142
Posted Wednesday, September 15, 2010 4:42 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, May 16, 2013 9:16 AM
Points: 2,236, Visits: 6,486
SELECT    crn, admission,  admdate, admtime, disdate, admsource, admtype
FROM mfCarlRepos.dbo.admissions as table_name1
WHERE admdate BETWEEN '2010-04-01' AND '2010-08-31'

Have you heard of between?

SELECT    crn, admission,  admdate, admtime, disdate, admsource, admtype
FROM mfCarlRepos.dbo.admissions as table_name1
WHERE admdate BETWEEN ( Dateadd(dd, Datediff(dd, 0, Getdate()), 0) ) AND (
Dateadd(dd, -14,
(
Dateadd(dd, Datediff(dd, 0, Getdate()), 0) )) )




Not a DBA, just trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


LinkedIn | Blog coming soon (for sufficiently large values of "soon" )!
Post #986154
Posted Wednesday, September 15, 2010 4:55 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, December 04, 2012 11:52 AM
Points: 26, Visits: 80
thank you for responding....however i don't think i've clearly explained myself.

The CRN is patient identification No. What i'm trying to do is see how many of the same patients (CRN's) have come in with the 14 days.

Here is a scenario:

I'm patient A had an operation on 15th August then i come again on the 20th august due to complications.

e.g

crn admdate

1234 19/05/2010
1234 15/08/2010*
1234 20/08/2010*
3456 01/07/2010
3456 11/08/2010*
3456 14/08/2010*

Display only these records

1234 has come in 4 days
3456 has come in 3 days
Post #986160
Posted Wednesday, September 15, 2010 5:08 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 6:25 AM
Points: 6,861, Visits: 8,045
If this is for homework, please mention the url as a ref


use mfCarlRepos

SELECT crn
, admission
, admdate
, admtime
, disdate
, admsource
, admtype
FROM dbo.admissions as table_name1
WHERE admdate between '2010-04-01' AND '2010-08-31'
and exists ( Select *
FROM dbo.admissions as ALZDBA
WHERE ALZDBA.crn = table_name1.crn
and ALZDBA.admdate > table_name1.admdate
and ALZDBA.admdate <= dateadd(dd, 14, table_name1.admdate)
/* tell your system what you know - this may help filtering your initial indexes and reduce the working set */
and ALZDBA.admdate between '2010-04-01' AND '2010-08-31'
)




Johan

     Jul 13  

Don't drive faster than your guardian angel can fly ...
but keeping both feet on the ground won't get you anywhere

- How to post Performance Problems
- How to post data/code to get the best help


- How to prevent a sore throat after hours of presenting ppt ?


"press F1 for solution", "press shift+F1 for urgent solution"


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me but most of the time this is me
Post #986169
Posted Wednesday, September 15, 2010 6:03 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, December 04, 2012 11:52 AM
Points: 26, Visits: 80
This is perfect,

Can you quickly go over the SQL statement. Its learning curve and learning as i go along.

Nope its not homework i'm afraid, i'm out of practice 5 years ago, helping trainee sql developer. Books only go certain far!

many thanks for your response

F
Post #986190
Posted Wednesday, September 15, 2010 6:15 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, April 24, 2013 9:20 AM
Points: 1,191, Visits: 1,251
This can also be done in a CTE

Setup
declare @t table (CRN int, admdate smalldatetime)
insert into @t
select 1234,'05/19/2010' union all
select 1234 ,'08/15/2010' union all
select 1234 ,'08/20/2010' union all
select 3456 ,'07/01/2010' union all
select 3456 ,'08/11/2010' union all
select 3456 ,'08/14/2010'

Code
;with cte as
(select CRN, ADMDate,
ROW_NUMBER() over (PARTITION by CRN order by CRN, ADMDate) RowNum
from @t)

select a.CRN, a.admdate
from cte a
left outer join cte b
on a.CRN = b.CRN
and a.RowNum = b.RowNum - 1
where DATEDIFF(d, a.admdate, b.admdate) <= 14



For better, quicker answers, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #986198
Posted Wednesday, September 15, 2010 6:20 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 6:25 AM
Points: 6,861, Visits: 8,045
With the extra "exists" clause it checks for existance of _any_ data in the query within the brackets.

Because I'm using a Correlated Subquery (check Books Online (bol) for "Correlated Subqueries")
I had it tied to the original query by using the columns prefixed by the alias 'table_name1' of the object dbo.admissions.

You could imagine this as if it would grab a row from 'table_name1' and use that data to see if it fullfils for existance in the exists-part of your query, based on the column values of that row for the specified columns in the embeded query.


Johan

     Jul 13  

Don't drive faster than your guardian angel can fly ...
but keeping both feet on the ground won't get you anywhere

- How to post Performance Problems
- How to post data/code to get the best help


- How to prevent a sore throat after hours of presenting ppt ?


"press F1 for solution", "press shift+F1 for urgent solution"


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me but most of the time this is me
Post #986201
Posted Wednesday, September 15, 2010 6:52 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, December 04, 2012 11:52 AM
Points: 26, Visits: 80
thank you for both responses.

i have similar query which if i wanted to calculate the 14 days from the disdate from the admdate

e.g

crn admdate DISdate

1234 19/05/2010 24/05/2010*
1234 01/06/2010* 19/06/2010
1234 20/08/2010 19/07/2010
3456 01/07/2010 19/07/2010
3456 20/07/2010 23/07/2010*
3456 24/07/2010* 30/11/2010

Display only these records

1234 has discharge on the 24th may but returned back on the 1st of june so its within 14 days
3456 has discharged on the 23rd July but returned back on the 24th july


ALZDBA, I have made a slight modification to the code


use mfCarlRepos

SELECT crn
, admission
, admdate
, admtime
, disdate
, admsource
, admtype
FROM dbo.admissions as table_name1
WHERE admdate between '2010-04-01' AND '2010-08-31'
and exists ( Select *
FROM dbo.admissions as ALZDBA
WHERE ALZDBA.crn = table_name1.crn
and ALZDBA.admdate > table_name1.admdate
and ALZDBA.admdate <= dateadd(dd, 14, table_name1.DISdate)
/* tell your system what you know - this may help filtering your initial indexes and reduce the working set */
and ALZDBA.admdate between '2010-04-01' AND '2010-08-31'


and ALZDBA.admdate <= dateadd(dd, 14, table_name1.DISdate) would changing admdate to disdate achieve the result i want.

many thanks for you help in this
Post #986224
Posted Wednesday, September 15, 2010 7:03 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 6:25 AM
Points: 6,861, Visits: 8,045
The easiest way to find out is to actually run the query and use the returned results to check for correctness using the subquery.

You'll only get comfortable with sql by using it.

Learn to play, play to learn

Off course, taking a course on SQL may get you there with a head start.


Johan

     Jul 13  

Don't drive faster than your guardian angel can fly ...
but keeping both feet on the ground won't get you anywhere

- How to post Performance Problems
- How to post data/code to get the best help


- How to prevent a sore throat after hours of presenting ppt ?


"press F1 for solution", "press shift+F1 for urgent solution"


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me but most of the time this is me
Post #986231
Posted Wednesday, September 15, 2010 7:03 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 10:37 AM
Points: 32,893, Visits: 26,769
Hmmm... not homework? How do you explain the remarkable similarity here...
http://www.sqlservercentral.com/Forums/Topic986540-391-1.aspx


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #986833
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse