任何人都可以帮助我吗?我想从我的桌子查询,目的是显示哪些员工销售超过100件。我已完成该查询并且有效,
但问题出现在我想显示每位员工的最后一次销售(销售超过100件的商品)
我有表格TBL_SALES,如
no employee_id name pieces sl_time
---|-------------|-------------|--------|----------
1 | 1 | bungdito | 60 | 2012-03-29 22:20
2 | 1 | bungdito | 40 | 2012-03-05 18:00
3 | 1 | bungdito | 50 | 2012-02-18 08:00
4 | 2 | addheat | 120 | 2012-02-12 09:30
5 | 3 | angga | 20 | 2012-01-18 10:45
6 | 4 | dimas | 50 | 2012-01-01 08:30
当我使用像这样的查询时
select * from
(
select EMPLOYEE_ID, NAME, sum(PIECES) PIECES from
(
select EMPLOYEE_ID, NAME, PIECES
from DB_SCHEMA.TBL_SALES
)
group by EMPLOYEE_ID, NAME
) where PIECES > 100
我这个正确的结果
employee_id name pieces
-------------|-------------|--------
1 | bungdito | 150
2 | addheat | 120
上面的结果是正确的,
但我需要 完成每位员工的上次交易时间
我试图让查询但仍然不正确
select * from
(
select EMPLOYEE_ID, NAME, sum(PIECES) PIECES, SL_TIME
(
select EMPLOYEE_ID, NAME, PIECES,(select max(WAKTU) SL_TIME from DB_USER.TR_PELANGGARAN where TB_SALES.EMPLOYEE_ID = EMPLOYEE_ID from TB_SALES) SL_TIME
from DB_SCHEMA.TBL_SALES
)
group by EMPLOYEE_ID, NAME, SL_TIME
) where PIECES > 100
使用该查询我仍然有错误的结果(请参阅sl_time在bungdito和addheat之间相同),如:
employee_id name pieces sl_time
-------------|-------------|--------|----------
1 | bungdito | 150 | 2012-03-29 22:20
2 | addheat | 120 | 2012-03-29 22:20
我需要的是这样的:
employee_id name pieces sl_time
-------------|-------------|--------|----------
1 | bungdito | 150 | 2012-03-29 22:20
2 | addheat | 120 | 2012-02-12 09:30
答案 0 :(得分:1)
select *
from
(
select EMPLOYEE_ID, NAME, sl_time
, sum(PIECES) over(partition by EMPLOYEE_ID) SUM_PIECES
, max(sl_time) over(partition by EMPLOYEE_ID) last_sl_time
from
DB_SCHEMA.TBL_SALES
)
where SUM_PIECES > 100
and last_sl_time = sl_time
;
答案 1 :(得分:1)
试试这个:
select EMPLOYEE_ID, NAME, sum(PIECES) PIECES, MAX(SL_TIME)
from DB_SCHEMA.TBL_SALES
group by EMPLOYEE_ID, NAME
having sum(PIECES) > 100