2

我有一个如下的招聘表:

------------------------------------------------------
| 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     |
-------------------------------------------------------
4

2 回答 2

2

您可以使用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
于 2017-07-25T10:15:09.680 回答
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
于 2017-07-25T10:15:39.787 回答