0

我发现很难构建一个选择查询。

PFB,对于表格和对应的数据:

ID    DLS     MATCH_STATUS    LAST_UPDATE_TIME        BO      CH    FT
1      0         0              09-07-2013 00:00:00   IT      TE    NA
1      1         1              09-07-2013 00:01:01   IT      TE    NA
2      0         0              09-07-2013 10:00:00   IP      TE    NA
3      0         0              09-07-2013 11:00:00   IT      YT    NA
3      2         2              09-07-2013 11:01:00   IT      YT    NA

这里

Match_Status 0-->Initial Record
             1-->Singel Match
             2-->Multi Match

对于每条记录,都会有一个 match_status 为 0 的初始条目,随后的匹配过程结束其他数字(例如 1,2)将被更新。

我正在尝试通过 BO、CH 和 FT 检索总记录、等待匹配、单匹配和多匹配组等记录

以下是预期的输出:

BO   CH    FT  TOTAL_RECORD AWAITNG_MATCH   SINGLE_MATCH   MULTI_MATCH
IT   TE    NA   1              0               1             0
IP   TE    NA   1              1               0             0
IT   YT    NA   1              0               0             2 

我试过以下查询:

select BO,CH,FT,sum(case when match_status=0 then 1 else 0 end) as TOTAL_RECORD,
sum(case when match_status = 0 then 1 else 0 end) as AWAITING_MATCH,
sum(case when match_status = 1 then 1 else 0 end) as SINGLE_MATCH,
sum(case when match_status = 2 then 1 else 0 end) as MULTI_MATCH from
table1 where last_update_time >= current_timestamp-1
group by BO,CH,FT;

上述查询的问题是,由于 match_status=0,awaiting_match 的填充与总记录相同

select BO,CH,FT,sum(case when match_status=0 then 1 else 0 end) as TOTAL_RECORD,
select (sum(case when t1.ms=0 then 1 else 0 end) from
(select max(match_status) as ms from table1 where last_update_time >=  current_timestamp-1  group by id)t1) )awaiting_match,
sum(case when match_status = 1 then 1 else 0 end) as SINGLE_MATCH,
sum(case when match_status = 2 then 1 else 0 end) as MULTI_MATCH from
table1 where last_update_time >= current_timestamp-1
group by BO,CH,FT;

该方法的问题是 awaiting_match 正在为后续行条目填充相同的值。

请帮助我对所需格式进行合适的查询。

提前非常感谢。

4

1 回答 1

0

看来您想要最后一场比赛状态。我猜这实际上是状态的最大值。如果是这样,以下通过先分组id然后进行分组总结来解决问题:

select BO, CH, FT,
       count(*) as TOTAL_RECORD,
       sum(case when lastms = 0 then 1 else 0 end) as AWAITING_MATCH,
       sum(case when lastms = 1 then 1 else 0 end) as SINGLE_MATCH,
       sum(case when lastms = 2 then 1 else 0 end) as MULTI_MATCH
from (select id, bo, ch, ft, MAX(match_status) as lastms
      from table1
      where last_update_time >= current_timestamp-1
      group by id, bo, ch, ft
     ) t
group by BO, CH, FT;

如果您确实希望最后一次更新为 提供状态id,那么您可以使用row_number()枚举每个 id 的行,按更新时间降序排列,然后选择第一个:

select BO, CH, FT,
       count(*) as TOTAL_RECORD,
       sum(case when lastms = 0 then 1 else 0 end) as AWAITING_MATCH,
       sum(case when lastms = 1 then 1 else 0 end) as SINGLE_MATCH,
       sum(case when lastms = 2 then 1 else 0 end) as MULTI_MATCH
from (select id, bo, ch, ft, match_status,
             ROW_NUMBER() over (partition by id order by last_update_time desc) as seqnum
      from table1
      where last_update_time >= current_timestamp-1
     ) t
where seqnum = 1
group by BO, CH, FT;
于 2013-07-09T18:40:10.710 回答