我有一个如下招聘表:
------------------------------------------------------
| slot_id | hired_days | qty | amt | return |
-----------|-------------|-------|-------|-----------|
| 1 | 30 | 5 | 100 | 0 |
| 1 | 30 | 15 | 300 | 0 |
| 1 | 30 | 12 | 170 | 1 |
| 1 | 25 | 13 | 180 | 0 |
| 1 | 30 | 4 | 180 | 1 |
| 2 | 30 | 15 | 300 | 0 |
------------------------------------------------------
我希望结果按slot_id和hired_days分组,但分组应仅针对0行的返回值进行。结果表需要显示所有分组的行并返回1个数据。有没有办法用SQL做到这一点?
-------------------------------------------------------
| slot_id | hired_days | qty | amt | return |
|----------|-------------|--------|-------|-----------|
| 1 | 30 | 20 | 400 | 0 |
| 1 | 25 | 13 | 180 | 0 |
| 1 | 30 | 12 | 170 | 1 |
| 1 | 30 | 4 | 180 | 1 |
| 2 | 30 | 15 | 300 | 0 |
-------------------------------------------------------
答案 0 :(得分:1)
您可以使用UNION
操作
SELECT slot_id,hired_days,sum(qty),sum(amt),return FROM hiring
WHERE return=0 GROUP BY slot_id,hired_days
UNION ALL
SELECT slot_id,hired_days,qty,amt,return FROM hiring WHERE return =1
答案 1 :(得分:0)
尝试此查询....
select slot_id, hired_days, sum(qty), sum(amt), return from hiring where return = 0 group by slot_id, hired_days, return union select slot_id, hired_days, qty, amt, return from hiring where return = 1