Calculating between dates fields from a previous record - sql

Patient Code Unique Visit Code AdmitDate Discharge Date
91260 10146440 7/20/2013 9/16/2013
91260 10217043 9/21/2013 11/2/2013
This is a single patient with 2 different visits to the physician, I need to take the Discharge date of the earliest visit (9/16/2013) and the AdmitDate of the most recent visit (9/21/2013) and determine if it's within a 30 day period. How do I accomplish this in SQL 2008

Try
SELECT PATIENT_CODE,
CASE WHEN DATEDIFF(DAY, MIN(DISCHARGE_DATE), MAX(ADMIT_DATE)) < 30 THEN 1 ELSE 0 END
FROM TABLE1
GROUP BY PATIENT_CODE
The code
CASE WHEN DATEDIFF(DAY, MIN(DISCHARGE_DATE), MAX(ADMIT_DATE)) < 30 THEN 1 ELSE 0 END
will be 1 for those visits which occurs in period less than 30 days.
SQL Fiddle

Related

How to find count of records by month given start and end date SQL?

I have a table:
1 180101 180228
2 180301 180831
3 180901 999999
4 180801 999999
5 180401 181031
6 181101 999999
7 180101 999999
The columns are: userid, start date, end date.
Dates are in text format, YYMMDD with 999999 meaning that there is no end date.
How do I get the number of customers for each month? Ex. March would include all customers that start on or before March, and end on or after March. But would not include customers that started in January, but ended in February.
I'm trying to do it like this, and then write out all the logic:
SELECT SUBSTRING(start_date, 3, 2) AS start, SUBSTRING(end_date, 3, 2) AS end, count(*)
FROM data
GROUP BY start, end
ORDER BY start
Was just wondering if there was any better way to do it?
If you had starts in every month, you can use correlated subqueries to get the count on the first day of the month:
select ym.yymm,
(select count(*)
from t
where t.startdate <= ym.yymm and
t.enddate >= ym.yymm
) as month_count
from (select distinct left(startdate, 4) as yymm
from t
) ym;
Some databases might spell left() as substr().

How to calculate days count between 2 date in sql server

I'm creating a system to calculate user working days.
Suppose user 1 has been at work from 2020-02-01 to 2020-02-20.
And user 2 has been at work from 2020-02-10 to 2020-02-15
In Sql Server
I have something similar in the table below
Now i want to calculte count of user working days between 2 date
For Example
Select Sum(DateDiff(Day,StartDate,EndDate)) From Table1 Where StartDate >= '2020-02-08' And EndDate <= '2020-02-12'
Above query returns 0 . But in this date user 1 has 5 working days and user 2 has 3 working days.
How can i calculate this?
I want a similar answer below:
You can use datediff() -- after testing for the limits of the period that you want:
select t.*,
datediff(day,
case when startdate < '2020-02-08' then '2020-02-08' else startdate end),
case when enddate > '2020-02-12' then '2020-02-12' else startdate end)
) + 1
from t;
The + 1 is because you are including the last day in the calculation.
Note: This only works correctly when there is an overlap. You want a filter:
(enddate >= '2020-02-08' or startdate <= '2020-02-12')
Based on the question, I'm not sure if this should be in a WHERE clause or a CASE expression. That is, do you want to filter out non-overlaps or do you want them to appear as 0/NULL?

Assign a Y/N flag based last 12 month activity

I'm working with a list of hospital patients and would like to flag each patient account with a "Y" if they were seen in the hospital nine or more times over the past 12 months.
I've come up with this, which would work fine if the patient list were static and only included a 12 month period:
SELECT
ENC.HSP_ACCOUNT_ID,
ENC.PAT_MRN_ID,
ENC.ADT_ARRIVAL_DTTM,
case when count(distinct txn.hsp_account_id) over(partition by PAT.PAT_MRN_ID) >= 9 then 'Y' else 'N' end as familiar_face_yn
FROM CLARITY.F_ED_ENCOUNTERS ENC
WHERE ENC.SERVICE_DATE BETWEEN '1-JUL-17' AND '31-OCT-18'
But I'd like to query the prior two years worth of data but only use the 12 months prior to the arrival date (ENC.ADT_ARRIVAL_DTTM) in calculating the Y or N.
The problem I'm running in to with the above query is that it's going back and counting all visits by a particular patient between 7/1/17 and 10/31/18.
What I'd like is that if the arrival date for a record is 8/1/18, it should count all visits between 8/1/17 and 8/1/18, ignoring anything with an arrival date earlier than 8/1/17 or later than 8/1/18.
Is this sort of "rolling" calculation possible? Many thanks!
You can use a windowing clause:
SELECT ENC.HSP_ACCOUNT_ID, ENC.PAT_MRN_ID, ENC.ADT_ARRIVAL_DTTM,
(CASE WHEN COUNT(DISTINCT txn.hsp_account_id) OVER
(PARTITION BY PAT.PAT_MRN_ID
ORDER BY ENC.SERVICE_DATE
RANGE BETWEEN 365 PRECEDING AND CURRENT ROW
) >= 9
THEN 'Y' ELSE 'N'
END) as familiar_face_yn
FROM CLARITY.F_ED_ENCOUNTERS ENC
WHERE ENC.SERVICE_DATE BETWEEN DATE '2017-07-01' AND DATE '2018-10-31'
with cte as
(
SELECT
ENC.HSP_ACCOUNT_ID,
ENC.PAT_MRN_ID,
ENC.ADT_ARRIVAL_DTTM,
-- find the most recent visit
max(ENC.ADT_ARRIVAL_DTTM) over(partition by PAT.PAT_MRN_ID) as last_date
FROM CLARITY.F_ED_ENCOUNTERS ENC
WHERE ENC.SERVICE_DATE BETWEEN '1-JUL-17' AND '31-OCT-18'
)
select ...
-- count all rows with within a 12 month range before the most recent visit
case when count(distinct case when ADT_ARRIVAL_DTTM >= add_months(last_date, -12) then txn.hsp_account_id end)
over (partition by PAT.PAT_MRN_ID) >= 9
then 'Y'
else 'N'
end as familiar_face_yn
from cte
I don't know if you really need the DISTINCT count...

SQL Server aggregation the latest 12 months

Suppose I have the following table:
Id Visitors Date
------------------------------
1 100 '2017-01-01'
2 200 '2017-01-02'
3 150 '2017-01-03'
I want a query to provide the average of a range of records for the last 12 months.
For one record I know that it would be like :
select avg(Visitors), Date
from Visitors_table
where Date between '2018-01-01' and '2017-01-01'
However, I need to do that for a range of dates and multiple records.
I know that Union will solve it, but if the range is one year for example It is not optimized to use 365 union
Get the dates from 1 year ago to current date:
SELECT
Date,
AVG(Visitors) AS avgvisitors,
FROM Visitors_table
WHERE Date > dateadd(year, -1, getdate())
GROUP BY Date
ORDER BY Date;
Since you need to group by date.

Transact SQL- Calculate leave days in a year

I want to calculate the number of leave days taken for staff in the financial year (1 July 2017 to 30 June 2018).
Some staff apply for leave outside these days and it overlaps for example a staff member may have a leave application of -start date of 30/06/2017 to 03/07/2017, so that is 2 days, but I would need to capture just the 1 day in July 2017.
I just want to be 100% here, so here is my query below
Select * from leave where leave_start < '2018-07-01' and leave_end > '2017-06-30'
The table is
Staffno | leave_type | leave_start | leave_end | days_taken
0001 | AnnualLeave| 30/06/2017 | 03/07/2018 | 2
Obviously once I get the result I will need to work out the actual start and actual end date and then do a networkdays formula in excel for it.
Thanks, your help is much appreciated :)
Use CASE statement to substitute boundary dates if start and end dates are out of interval. In your example you don't count start and end date as a leave so if you need this in the query (here start and end days are counted as a leave) just add "-2" into period_days_taken definition:
Select leave.*,
DATEDIFF(DAY,
CASE WHEN leave_start<'2017-06-30'
THEN '2017-06-30'
ELSE leave_start END,
CASE WHEN leave_end>'2018-07-01'
THEN '2018-07-01'
ELSE leave_end END)
AS period_days_taken
from leave
WHERE leave_start <= '2018-07-01'
AND leave_end >= '2017-06-30'
SQLFiddle demo