对不起标题,但有点难以在一个单行中解释这个主题..
我有一张这样的表格,我想知道(一年中的每个月)第一次获得奖金的员工人数。
EMPLOYEE_NAME MONTH BONUS_RECEIVED
AAA 1 1
BBB 1 1
CCC 2 1
AAA 2 1
DDD 2 1
AAA 3 1
BBB 3 1
XXX 3 1
所以,结果应该是
MONTH TOTAL_BONUS
1 2
2 2
3 1
第1个月,员工AAA和BBB获得奖金(结果为2)
第2个月,员工CCC和DD获得奖金(全年已收到AAA),结果为2
答案 0 :(得分:2)
双重聚合解决了您的问题:
select month, count(1) as total_bonus
from (
select employee_name, min(month) as month
from table_like_this
where bonus_received = 1
group by employee_name
)
group by month;
首先,对于每位员工,您会发现他/她获得奖金的第一个月。然后,您计算每个“第一个奖金 - 收到的月份”的员工数量。
答案 1 :(得分:1)
你也可以使用RANK()
SELECT MONTH
,COUNT(BONUS) AS BONUS
FROM (
SELECT EMPLOYEE
,MONTH
,BONUS
,RANK() OVER (
PARTITION BY EMPLOYEE ORDER BY MONTH
) AS RN
FROM TBTEST
)
WHERE RN = 1
GROUP BY MONTH
答案 2 :(得分:0)
您可以使用 ROW_NUMBER()分析函数。
例如,
<强>设置强>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>proxy-list</title>
</head>
<body>
<table>
<tr>
<td>192.168.1.10</td>
<td>HTTP1</td>
<td>Vietnam</td>
<td>8080</td>
<td>a</td>
<td>%d1</td>
</tr>
<tr>
<td>10.25.100.10</td>
<td>HTTPS2</td>
<td>Campuchia</td>
<td>3214</td>
<td>b</td>
<td>%d2</td>
</tr>
<tr>
<td>203.25.10.110</td>
<td>HTTP3</td>
<td>ThaiLan</td>
<td>123</td>
<td>c</td>
<td>%d3</td>
</tr>
<tr>
<td>220.155.10.13</td>
<td>HTTP4</td>
<td>Itali</td>
<td >1000</td>
<td>d</td>
<td>%d4</td>
</tr>
<tr>
<td>220.155.10.113</td>
<td>HTTP5</td>
<td>Itali</td>
<td >505</td>
<td>d</td>
<td>%d4</td>
</tr>
<tr>
<td>220.155.10.115</td>
<td>HTTPS6</td>
<td>Itali</td>
<td >321</td>
<td>d</td>
<td>%d4</td>
</tr>
</table>
</body>
</html>
<强>查询强>
CREATE TABLE t
(EMPLOYEE_NAME varchar2(3), MONTH number, BONUS_RECEIVED number);
INSERT ALL
INTO t (EMPLOYEE_NAME, MONTH, BONUS_RECEIVED)
VALUES ('AAA', 1, 1)
INTO t (EMPLOYEE_NAME, MONTH, BONUS_RECEIVED)
VALUES ('BBB', 1, 1)
INTO t (EMPLOYEE_NAME, MONTH, BONUS_RECEIVED)
VALUES ('CCC', 2, 1)
INTO t (EMPLOYEE_NAME, MONTH, BONUS_RECEIVED)
VALUES ('AAA', 2, 1)
INTO t (EMPLOYEE_NAME, MONTH, BONUS_RECEIVED)
VALUES ('DDD', 2, 1)
INTO t (EMPLOYEE_NAME, MONTH, BONUS_RECEIVED)
VALUES ('AAA', 3, 1)
INTO t (EMPLOYEE_NAME, MONTH, BONUS_RECEIVED)
VALUES ('BBB', 3, 1)
INTO t (EMPLOYEE_NAME, MONTH, BONUS_RECEIVED)
VALUES ('XXX', 3, 1)
SELECT * FROM dual;