0

我有一个with rollup输出数据的查询(查询有点忙,但如有必要我可以发布)

range    subCounts    Counts    percent
1-9      3            100       3.0
10-19    13           100       13.0
20-29    30           100       33.0
30-39    74           100       74.0
NULL     100          100       100.0

怎么可能保持一个运行总和percent?假设我需要找到底部的 15 个百分位数,在这种情况下3+13=16,我希望最后一行返回读取

range    subCounts    counts    percent
10-19    13           100       13.0

EDIT1:这里是查询

select '$'+cast(+bin*10000 + ' ' as varchar(10)) + '-' + cast(bin*10000+9999 as varchar(10)) as bins, 
    count(*) as numbers,
    (select count(distinct patient.patientid) from patient 
    inner join tblclaims on patient.patientid = tblclaims.patientid
    and patient.admissiondate = tblclaims.admissiondate
    and patient.dischargedate = tblclaims.dischargedate
    inner join tblhospitals on tblhospitals.hospitalnpi = patient.hospitalnpi
    where (tblhospitals.hospitalname = 'X') 
    ) as Totals
    , round(100*count(*)/cast((select count(distinct patient.patientid) from patient 
    inner join tblclaims on patient.patientid = tblclaims.patientid
    and patient.admissiondate = tblclaims.admissiondate
    and patient.dischargedate = tblclaims.dischargedate
    inner join tblhospitals on tblhospitals.hospitalnpi = patient.hospitalnpi
    where (tblhospitals.hospitalname = 'X')) as float),2) as binsPercent


from
(

select tblclaims.patientid, sum(claimsmedicarepaid) as TotalCosts, 
    cast(sum(claimsmedicarePaid)/10000 as int) as bin
    from tblclaims inner join patient on patient.patientid = tblclaims.patientid
    and patient.admissiondate = tblclaims.admissiondate 
    and patient.dischargedate = tblclaims.dischargedate
    inner join tblhospitals on patient.hospitalnpi = tblhospitals.hospitalnpi
    where tblhospitals.hospitalname = 'X'
    group by tblclaims.patientid
)   as t 
group by bin with rollup
4

2 回答 2

0

This isn't exactly what i was looking for, but it's on the same track: http://blog.tallan.com/2011/12/08/sql-server-2012-windowing-functions-part-1-of-2-running-and-sliding-aggregates/ and http://blog.tallan.com/2011/12/19/sql-server-2012-windowing-functions-part-2-of-2-new-analytic-functions/ - check out PERCENT_RANK CUME_DIST PERCENTILE_CONT PERCENTILE_DISC

Sorry for the lame answer

于 2012-07-23T22:13:45.337 回答
0

好的,所以对于任何可能将此作为参考的人,我都知道我需要做什么。

我添加row_number() over(bin) as rownum到查询中并将所有这些保存为视图。

然后我用

SELECT *,
          SUM(t2.binspercent) AS SUM
FROM t t1
INNER JOIN t t2 ON t1.rownum >= t2.rownum
GROUP BY t1.rownum,
         t1.bins, t1.numbers, t1.uktotal, t1.binspercent
ORDER BY t1.rownum

通过加入 t1.rownum >=t2.rownum 你可以获得滚动计数之类的东西。

于 2012-07-23T18:03:58.170 回答