Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Help with COUNT() on last day of month

Author  Topic 

GregDDDD
Posting Yak Master

120 Posts

Posted - 2012-06-04 : 11:44:29
I need some assitance with a query. The goal is to determine who is enrolled in which health plan on a given date. The date would always be the last day of the month and the date would be supplied by the user. (e.g. give me enrollment by health plan on 9/30/2011)

Most members come in to our system asigned to a particular health plan and never change, but others may move between plans. When a patient is first put in to the system we put their initial enrollment date in as 1/1/1980 because we may need to put in historical medical information and the front-end will not allow any data to be entered which pre-dates enrollment.

In the query below, the output coulmns are patient_id, item_text (name of insurance plan), encounter_date (the date enrollment in a particular plan started), and date_created (the date the rows was created in the table). We some times get enrollment information months after it actually changed.

The goal is to count the number of patient_id in a health plan on a particular date, using encounter_date as the enrollment date.

SELECT tbl_text.patient_id, tbl_text.item_text, tbl_encounters.encounter_date, tbl_text.date_created
FROM tbl_text INNER JOIN
tbl_encounters ON tbl_encounters.encounter_id = tbl_text.encounter_id AND tbl_text.patient_id = tbl_encounters.patient_id
WHERE (tbl_text.item_id = 129) AND (tbl_encounters.record_active = 1)
ORDER BY tbl_text.patient_id

In the sample output below, you can see that 10076 and 10078 each have one row so their enrollment on 9/30/2011, or any month, for that matter, would be Cal PERS PPO. 10077 should also be recorded as Cal PERS PPO on 9/30/2011 even though they are currently enrolled as PPO Not Billable. Their enrollment changed on 3/16/2012.

patient_id item_text encounter_date date_created
10076 Cal PERS PPO 1/1/1980 8/8/2011
10077 Cal PERS PPO 1/1/1980 8/8/2011
10077 PPO Not Billable 3/16/2012 4/3/2012
10078 Cal PERS PPO 1/1/1980 8/9/2011


I’m not even sure where to start with this. Any help is appreciated.

Greg

GregDDDD
Posting Yak Master

120 Posts

Posted - 2012-06-04 : 12:23:34
This should duplicate the sample data.

Greg
declare @tbl_text TABLE (
[patient_id] [int] NOT NULL,
[encounter_id] [int] NOT NULL,
[item_id] [int] NOT NULL,
[item_text] [varchar](128) NULL,
[date_created] [datetime] NULL
)

declare @tbl_encounters TABLE (
[encounter_id] [int] NOT NULL,
[patient_id] [int] NOT NULL,
[encounter_date] [datetime] NULL,
[record_active] [bit] NOT NULL DEFAULT ((0))
)


insert into @tbl_text
values
(10076, 1, 129, 'Cal PERS PPO', '8/8/2011')

insert into @tbl_text
values
(10077, 2, 129, 'Cal PERS PPO', '8/8/2011')
insert into @tbl_text
values
(10077, 3, 129, 'PPO Not Billable', '4/3/2012')
insert into @tbl_text
values
(10078, 4, 129, 'Cal PERS PPO', '8/9/2011')

insert into @tbl_encounters
values
(1, 10076, '1/1/1980', 1)
insert into @tbl_encounters
values
(2, 10077, '1/1/1980', 1)
insert into @tbl_encounters
values
(3, 10077, '3/16/2012', 1)
insert into @tbl_encounters
values
(4, 10078, '1/1/1980', 1)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-06-04 : 12:36:20
[code]
SELECT t.[patient_id],t.[item_text],e.[encounter_date],t.[date_created]
FROM @tbl_text t
INNER JOIN @tbl_encounters e
ON e.[encounter_id] = t.[encounter_id]
AND e.[patient_id] = t.[patient_id]
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

GregDDDD
Posting Yak Master

120 Posts

Posted - 2012-06-04 : 12:45:02
Thank you for rounding out the test data with a new query. I should have added that.

Now, if I only had a solution

Greg
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-06-04 : 13:06:14
may be this?

@date is passed on date value by user

SELECT t.[item_text],COUNT(t.[patient_id]) AS PatientCnt
FROM @tbl_text t
INNER JOIN @tbl_encounters e
ON e.[encounter_id] = t.[encounter_id]
AND e.[patient_id] = t.[patient_id]
WHERE e.[encounter_date] <= @date
GROUP BY t.[item_text]


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

GregDDDD
Posting Yak Master

120 Posts

Posted - 2012-06-04 : 14:31:29
Hmmm, that seems too easy or maybe I was over thinking it. If a patient moves twice in the month being checked or has multiple encounter dates/Health Plans prior to the user defined date I think they end up in multiple groups.

I think I need to take the MAX(encounter_date) that is prior to the user defined date and then count.

I'll play around with it.

Greg
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-06-04 : 14:55:25
quote:
Originally posted by GregDDDD

Hmmm, that seems too easy or maybe I was over thinking it. If a patient moves twice in the month being checked or has multiple encounter dates/Health Plans prior to the user defined date I think they end up in multiple groups.

I think I need to take the MAX(encounter_date) that is prior to the user defined date and then count.

I'll play around with it.

Greg


how will they move in twice within same health plan?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

GregDDDD
Posting Yak Master

120 Posts

Posted - 2012-06-04 : 15:59:31
Not twice in the same plan, but twice in the same month to different plans. I'm not sure they would, but technically that is possible, even if it is a data entry error. Not likely, I'll admit. Even if they didn't move twice in the same month they could change multiple times prior to the date supplied by the user, so WHERE e.[encounter_date] <= @date could return multiple rows for a single patient and rows are only added when there is a change.

Greg
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-06-04 : 16:44:31
quote:
Originally posted by GregDDDD

Not twice in the same plan, but twice in the same month to different plans. I'm not sure they would, but technically that is possible, even if it is a data entry error. Not likely, I'll admit. Even if they didn't move twice in the same month they could change multiple times prior to the date supplied by the user, so WHERE e.[encounter_date] <= @date could return multiple rows for a single patient and rows are only added when there is a change.

Greg


but still you're looking at count per health plan right? so are you saying that you dont want double count any patients who moved into another helath plan within same month period?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

GregDDDD
Posting Yak Master

120 Posts

Posted - 2012-06-04 : 17:39:22
No, I'm looking for a point in time. On 9/30/2012 who is where. If they were in a different plan on 8/31/2012 I don't want to count them in the plan at that time unless the user supplies the date of 8/31/2012, so by simply saying WHERE e.[encounter_date] <= @date the patient ended up in both buckets. By taking MAX(encounter_date) WHERE e.[encounter_date] <= @date I only get them in their last plan before the date. They end up in one group.

I think I got it, but it turned out to be even more complicated. When the patient changes plans the current plan has a record_active = 1 and any past plans have a record_active = 0, but because I'm looking at historical data I am querying all records, regardless of the record_active value. I found that if a user selects one plan on a particular encounter date and then edits that encounter I get both values recorded on the same encounter date, one or both can have record_active = 0 or 1. There is an entity column which I can use to get the last edited value for that encounter date. So I get the MAX(entity) for the tbl_text item on the MAX(encounter_date) for the last date the user supplies.

I'm still testing, but I think this works. This may be more verbose than it needs.
[CODE]
SELECT tbl_text.patient_id, tbl_text.item_text, tbl_encounters.encounter_date
FROM tbl_text INNER JOIN
tbl_encounters ON tbl_encounters.encounter_id = tbl_text.encounter_id AND tbl_text.patient_id = tbl_encounters.patient_id INNER JOIN
(SELECT tbl_text_1.patient_id, MAX(tbl_encounters_1.encounter_date) AS LastEnc
FROM tbl_text AS tbl_text_1 INNER JOIN
tbl_encounters AS tbl_encounters_1 ON tbl_encounters_1.encounter_id = tbl_text_1.encounter_id AND
tbl_text_1.patient_id = tbl_encounters_1.patient_id INNER JOIN
exp_NewPatient2 ON tbl_text_1.patient_id = exp_NewPatient2.PatientID
WHERE (tbl_text_1.item_id = 129) AND (tbl_encounters_1.record_active = 1) AND (tbl_encounters_1.encounter_date <= '7/31/2012')
GROUP BY tbl_text_1.patient_id) AS LastEncounter ON tbl_encounters.patient_id = LastEncounter.patient_id AND
tbl_encounters.encounter_date = LastEncounter.LastEnc INNER JOIN
(SELECT patient_id, encounter_id, MAX(keyfield) AS MaxKey
FROM tbl_text AS tbl_text_2
WHERE (item_id = 129)
GROUP BY patient_id, encounter_id) AS LastKey ON tbl_text.keyfield = LastKey.MaxKey AND tbl_text.patient_id = LastKey.patient_id AND
tbl_text.encounter_id = LastKey.encounter_id
WHERE (tbl_text.item_id = 129) AND (tbl_encounters.record_active = 1)
ORDER BY tbl_text.patient_id[/CODE]
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-06-05 : 00:04:36
wat about this?

SELECT t.[item_text],COUNT(t.[patient_id]) AS PatientCnt
FROM @tbl_text t
CROSS APPLY (SELECT TOP 1 *
FROM @tbl_encounters
WHERE [patient_id] = t.[patient_id]
AND [encounter_date] <= @date
ORDER BY [encounter_date] DESC
)e
GROUP BY t.[item_text]


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -