我有一个这样的表,我需要将其拆分以更好地分析数据
ID | doc | name | price | pay
1 | doc1| PERSON1 | 1 | 1
2 | doc2| PERSON1 | 10 | 0
3 | doc3| PERSON2 | 12 | 1
4 | doc4| PERSON2 | 1 | 0
5 | doc5| PERSON2 | 15 | 0
6 | doc6| PERSON3 | 2 | 1
7 | doc7| PERSON3 | 23 | 0
8 | doc8| PERSON3 | 3 | 0
9 | doc9| PERSON4 | 8 | 0
,我需要这样的输出,但我不知道该怎么做!
name | price | pay
PERSON1 | |
Doc1 | 1 | 1
Doc2 | 10 | 0
Total payed | 1 |
Total per pay| 10 |
Total | 11 |
PERSON2 | |
Doc3 | 12 | 1
Doc4 | 1 | 0
Doc5 | 15 | 0
Total payed | 12 |
Total per pay| 16 |
Total | 28 |
PERSON3 | |
Doc6 | 2 | 1
Doc7 | 23 | 0
Doc8 | 3 | 0
Total payed | 2 |
Total per pay| 26 |
Total | 28 |
PERSON4 | |
Doc9 | 8 | 0
Total payed | 0 |
Total per pay| 8 |
Total | 8 |
Payed | 15 |
PER PAY | 60 |
Total SUM | 75 |
这可能吗?
答案 0 :(得分:3)
如果第二种格式更好,我无法理解您如何分析数据。您可以这样做,但这很痛苦:
select doc, price, pay
from ((select doc, price, pay, 2 as ord, name
from t
) union all
(select distinct name as doc, null, null, 1, name
from t
group by name
) union all
(select 'total paid', sum(pay * price), null, 3, name
from t
group by name
) union all
(select 'total not paid', sum( (1 - pay) * price), null, 3, name
from t
group by name
) union all
(select 'total', sum(pay), null, 5, name
from t
group by name
)
) t
order by name, ord;
答案 1 :(得分:2)
类似这样的东西:
select t.empid,
coalesce(INTIME, OUTIME) as INTIME,
coalesce(OUTIME, INTIME) as OUTIME
from ( <query here>
) t;
这是一个完整的工作示例,可以为您提供输出。您可以对其进行一些更改以匹配您的真实数据。