在 sql 中写一个数学公式

writing a mathematical formula in sql

使用以下 sql 查询:

Select count(*) as Totaladmit From v_dbPatientAdmissions where AdmitDate > '2017-01-01' and AdmitDate <'2017-12-30' and PatientType = 2
Select count(*) as TotalDischarge From v_dbPatientAdmissions where DischargeDate > '2017-01-01' and DischargeDate <'2017-012-30' and PatientType = 2
select count(*) as totalIP from v_dbPatientAdmissions where PatientType=2 and DischargeDate is null 
select count (BedName) as Bedcount from V_Beds

我们得到以下输出:

TotalAdmit
66668

TotalDischarge
6651

TotalIP
91

BedCount
174

我想编写一个数学公式来执行以下操作:

为了创建一个 crystal 报告 returns 只有一个值(速率),也适用于 Totaladmit 和 TotalDischarge 我想应用一个条件,排除共享相同 AdmitDate 的记录和出院日期。

请试试这个-

SELECT
    ( ( 
       SUM(CASE WHEN PatientType=2 and DischargeDate is null THEN 1 ELSE 0 END) OVER()
     + SUM(CASE WHEN AdmitDate > '2017-01-01' and AdmitDate <'2017-12-30' and PatientType = 2 THEN 1 ELSE 0 END) OVER() )
     - (SUM(CASE WHEN DischargeDate > '2017-01-01' and DischargeDate <'2017-012-30' and PatientType = 2 THEN 1 ELSE 0 END) OVER() )
     ) * 100.0 / ( select COUNT(BedName) OVER() from V_Beds )
FROM v_dbPatientAdmissions 

由于您的 select 条件不同,因此使用 WHERE 的普通分组将不起作用。

您可以像下面这样尝试。这将是最简单的解决方案。

SELECT ( ( TotalIp + Totaladmit - TotalDischarge ) / BedCount ) * 100 AS 
       [Output] 
FROM   (SELECT (SELECT Count(*) 
                From   v_dbPatientAdmissions 
                where  AdmitDate > '2017-01-01' 
                       and AdmitDate < '2017-12-30' 
                       and PatientType = 2)       as Totaladmit, 
               (SELECT Count(*) 
                From   v_dbPatientAdmissions 
                where  DischargeDate > '2017-01-01' 
                       and DischargeDate < '2017-12-30' 
                       and PatientType = 2)       as TotalDischarge, 
               (SELECT Count(*) 
                from   v_dbPatientAdmissions 
                where  PatientType = 2 
                       and DischargeDate is null) as TotalIp, 
               (SELECT Count (BedName) 
                from   V_Beds)                    AS Bedcount) T 

除此之外,您还可以使用 CROSS APPLY 来做同样的事情。

我会通过将它们包括在计算中来绕过当天出院的并发症 例如

drop table t
go
create table t(patientid int, admitdate date, dischargedate date)
create table b(bedname varchar(1))
go
truncate table t
insert into t values (1,'2017-01-01','2017-01-01'),(1,'2017-02-01',null),(2,'2017-01-01','2017-02-01'),
(3,'2016-01-01','2017-01-01'),(4,'2016-01-01',null)

insert into b values ('a'),('b')

select (admittednull + (admitted - discharged - sameday)) / beds * 100 as calulatedratio
from
(
select sum(case when year(admitdate) = 2017 then 1 else 0 end) as admitted,
        sum(case when year(dischargedate) = 2017 then 1 else 0 end) as discharged,
        sum(case when year(admitdate) <> 2017 and dischargedate is null then 1 else 0 end) as admittednull,
        (select count(*) from b) beds,
        sum(case when year(admitdate) = 2017 and year(dischargedate) = 2017 and admitdate = dischargedate then 1 else 0 end) sameday
from t
) s

碰巧这个returns值为0。