1

我在一个看起来像这样的表中有数据:

VISIT ID | Surgeon | Surgeon Arrival Before Schedule in Minutes | ...
123      | Dr. A   | -45
156      | Dr. A   | -25
198      | Dr. A   | 32
...      | ...

我想构建一个将 bin 和 count 的查询,这意味着我想要如下输出:

Surgeon | Bin 1 | Bin 2 | Bin 3 | ... | Bin 6
Dr. A   | 2     | 0     | 4     | ... | 0

上面,Bin(s) 定义了Surgeon Arrival Before Schedule in Minutes列落入具有以下逻辑的 bin 的位置:

Bin 1 = Surgeon Arrival Before Schedule in Minutes        <= -30
Bin 2 = -30 < Surgeon Arrival Before Schedule in Minutes  <= -15
Bin 3 = -15 < Surgeon Arrival Before Schedule in Minutes  <= 0
Bin 4 = 0   < Surgeon Arrival Before Schedule in Minutes  <= 15
Bin 5 = 15  < Surgeon Arrival Before Schedule in Minutes  <= 30
Bin 6 = Surgeon Arrival Before Schedule in Minutes        > 30

我想有COUNT多少Encounter ID数字落入每个箱中以产生所需的结果。

我一直在尝试查询,例如:

TRANSFORM Count([or on time log].[ENCOUNTER ID]) AS CountOfID

SELECT [or on time log].[Surgeon]

FROM [or on time log]

GROUP BY [or on time log].[Surgeon]

PIVOT IIf([Surgeon Arrival Before Schedule in Minutes] <= -30, "Bin 1",
    IIf(-30 < [Surgeon Arrival Before Schedule in Minutes] <= -15, "Bin 2",
        IIf(-15 < [Surgeon Arrival Before Schedule in Minutes] <=0, "Bin 3",
            IIf(0 < [Surgeon Arrival Before Schedule in Minutes] <=15, "Bin 4",
                IIf(15 < [Surgeon Arrival Before Schedule in Minutes] <= 30, "Bin 5",
                    IIf(30 < [Surgeon Arrival Before Schedule in Minutes], "Bin 6")
                )
            )
        )
    )
)
;

这样做只给了我不准确的 bin 1 和 3 Surgeon。

4

2 回答 2

2

作为第一步,为每个到达分配箱号。

这是我使用的示例表。

VISIT_ID Surgeon Arrival
     123 Dr. A       -45
     156 Dr. A       -25
     198 Dr. A        32

下面的查询使用 Switch 表达式来分配 bin 编号,给我这个结果集。

VISIT_ID Surgeon Arrival bin_number
     123 Dr. A       -45          1
     156 Dr. A       -25          2
     198 Dr. A        32          6

SELECT
    o.VISIT_ID,
    o.Surgeon,
    o.Arrival,
    Switch
        (
            o.Arrival <= -30, 1,
            o.Arrival <= -15, 2,
            o.Arrival <=   0, 3,
            o.Arrival <=  15, 4,
            o.Arrival <=  30, 5,
            o.Arrival >   30, 6,
        ) AS bin_number
FROM OR_on_time_log AS o;

您可以在此基础上使用 aPIVOT返回每位外科医生的垃圾箱计数。

于 2013-12-23T19:43:00.180 回答
0

我将采用一种不同的方式,如此处链接中所述

代码是这样的:

SELECT [or on time log].Surgeon
, sum(iif([or on time log].[surgeon arrival before schedule in minutes] < -30,1,0)) 
  as Bin1
, sum(iif([or on time log].[surgeon arrival before schedule in minutes] > -30 
    and [or on time log].[surgeon arrival before schedule in minutes] <= -15,1,0)) 
  as Bin2
, sum(iif([or on time log].[surgeon arrival before schedule in minutes] > -15 
    and [or on time log].[surgeon arrival before schedule in minutes] <= -0,1,0)) 
  as Bin3
, ...
FROM [or on time log]
group by [or on time log].surgeon
于 2013-12-23T19:39:11.963 回答