2013-01-11 128 views
0
CREATE TABLE [dbo].[rx](
      [pat_id] [int] NOT NULL, 
      [fill_Date] [date] NOT NULL, 
      [script_End_Date] AS (dateadd(day,[dayssup],[filldate])), 
      [drug_Name] [varchar](50) NULL, 
      [days_Sup] [int] NOT NULL, 
      [quantity] [float] NOT NULL, 
      [drug_Class] [char](3) NOT NULL, 
      CHECK(fill_Date <=script_End_Date 
PRIMARY KEY CLUSTERED 
(
      [clmid] ASC 
) 
CREATE TABLE [dbo].[Calendar](
      [cal_date] [date] NOT NULL, 
      [julian_seq] [int] IDENTITY(1,1) NOT NULL, 
--unsure if the above line is an acceptable way of adding a 'julianized date number', the data in this database ranges from 1-1-2007 to 12-31-2009 
PRIMARY KEY CLUSTERED 
(
      [cal_date] ASC 
) 

我有我的感兴趣的表和上面的结构的日历表。我试图在现场drug_class的特定时间(重叠日期)找出一个人在一定数量的家庭中服用的不同药物的最大数量。按日期范围进行时间数据分组SQL Server

我在社区的帮助下获得了类似问题的成功,但此刻我正在做一些不正确的事情,并导致大量不准确的结果。如果可能的话,我想结果集返回到看起来像

create table DesiredResults 
(pat_id int, min_overlap date, max_overlap date, drug_class char(3),drug_name varchar(50)) 
insert into Desired_Results(patid, minoverlap, maxoverlap, drug_class,drug_name) 
values (1111,'2008-11-28', '2008-12-18','h3a','drug X') 
     ,(1111,'2008-11-28','2008-12-18','h3a','drug Y') 

这将意味着,上述患者111时间帧期间遵医嘱药物x和药品Y。

我查询 -

;with Overlaps (pat_id,cal_date,drug_class) 
as 
(
select 
mdo.pat_id 
,c1.cal_date 
,mdo.drug_class 
from 
(
--this gives a table of all the scripts a person had within the classes restricted in the where rx.drug_class IN clause and their fill_date and script_end_dates 
SELECT DISTINCT 
rx.pat_id 
,rx.drug_class 
,rx.drug_name 
,rx.fill_date 
,rx.script_end_date 
FROM rx 
WHERE rx.drug_class IN('h3a', 'h6h', 'h4b', 'h2f', 'h2s', 'j7c', 'h2e') 
-- 
) as mdo,Calendar as c1 
where c1.cal_date between mdo.fill_date and mdo.script_end_date 
group by mdo.pat_id,c1.cal_date,mdo.drug_class 
having count(*) > 1--overlaps 
) 
, 
Groupings(pat_id,cal_date,drug_class,grp_nbr) 
as 
(
select 
o.pat_id 
,o.cal_date 
,o.drug_class 
,c2.julian_seq 

--julianized date 
-row_number() over(partition by o.pat_id,o.drug_class order by o.cal_date) as grp_nbr 
from Overlaps as o,calendar as c2 
where c2.cal_date = o.cal_date 
) 
,x 

as 
(

--i think this is what's causing the problem 

select pat_id,min(cal_date) as min_overlap,max(cal_date) as max_overlap,drug_class 
from groupings 
group by pat_id,grp_nbr,drug_class 

) 

select 
x.pat_id 
,x.min_overlap 
,x.max_overlap 
,y.drug_class 
,y.drug_name 
from x 
inner join 
(
select distinct 
rx.pat_id 
,rx.drug_name 
,rx.drug_class 
,rx.fill_date 
from rx 
) as y on x.pat_id = y.pat_id and x.drug_class=y.drug_class 
      and y.fill_date between x.min_overlap and x.max_overlap 
order by datediff(day,min_overlap,max_overlap) desc 

我找天的跨度,其中大部分药物在给定的类被规定。不过,现在这给我的日期范围比任何单个的datediff(day,fill_date,script_end_date)都大。

这使得数字人为地膨胀,因为一些重叠范围是长达数年,至多它们应该大约是医生为剧本编写剧本的天数。如果在同一天处方'h3a'类中的五种药物被处方,那么我将捕获该时间段为pat_id,fill_date,end_date, h3a对于该类中的每种药物重复5次。

回答

0

我不确定这是否解决了您的问题。这使其中的药物的最大数量是规定的日期:

select c.cal_date 
from (select c.cal_date, count(*) as NumDrugs, 
      dense_rank() over (order by count(*) desc) as seqnum 
     from Calendar c join 
      rx 
      on c.cal_date between rx.fill_date betwen rx.script_end_date and 
       rx in IN('h3a', 'h6h', 'h4b', 'h2f', 'h2s', 'j7c', 'h2e') 
     group by c.cal_date 
    ) crx 
where seqnum = 1 

它比你的查询简单得多,所以我想知道如果我错过了什么。

如果你需要把它变成句点,那也是可能的,但是SQL有点麻烦。

此外,此SQL未经测试,因此可能有语法错误。