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找到一个人在特定时间(重叠日期)在一定数量的家庭中服用的不同药物的最大数量。
我在社区的帮助下取得了类似问题的成功,但目前我正在做一些不正确的事情并得到非常不准确的结果。如果可能的话,我希望返回的结果集看起来像
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
重复五次对于该班的每种药物。
答案 0 :(得分: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未经测试,因此可能存在语法错误。