我有一张表,该表使用分组依据功能汇总不同的作业类型。我希望工作类型(字段)穿过顶部。这样做的简单方法是什么?
OFFICE_CENTER OFFICE_NAME JOB_CATEGORY_1 TOTAL_HC
1 DEER PARK Lead 10
1 DEER PARK Lead1 6
6 ST THOMAS Lead 4
5 HONOLULU, HI Lead 40
5 HONOLULU, HI Lead1 9
所需的输出
OFFICE_CENTER OFFICE_NAME Lead_Total Lead1_Total
1 DEER PARK 10 6
6 ST THOMAS 4
5 HONOLULU, HI 40 9
谢谢, 千瓦
答案 0 :(得分:1)
您可以使用PIVOT和UNPIVOT关系运算符将表值表达式更改为另一个表。 PIVOT通过将表达式中的一列的唯一值转换为输出中的多列来旋转表值表达式,并在需要对最终输出中所需的任何其余列值进行汇总的情况下进行汇总。
以下是PIVOT的带注释的语法。
SELECT <non-pivoted column>,
[first pivoted column] AS <column name>,
[second pivoted column] AS <column name>,
...
[last pivoted column] AS <column name>
FROM
(<SELECT query that produces the data>)
AS <alias for the source query>
PIVOT
(
<aggregation function>(<column being aggregated>)
FOR
[<column that contains the values that will become column headers>]
IN ( [first pivoted column], [second pivoted column],
... [last pivoted column])
) AS <alias for the pivot table>
<optional ORDER BY clause>;
答案 1 :(得分:0)
使用这样的东西可以帮助您:
SELECT OFFICE_CENTER, OFFICE_NAME, (SELECT COUNT(*) FROM Office WHERE JOB_CATEGORY_1 = Lead) AS Lead_Total, (SELECT COUNT(*) FROM Office WHERE JOB_CATEGORY_1 = Lead1) AS Lead1_Total
FROM Office
GROUP BY OFFICE_CENTER
答案 2 :(得分:0)
也许是这样?
SQL> with test (office_center, office_name, job_category_1, total_hc) as
2 (select 1, 'deer park', 'lead', 10 from dual union
3 select 1, 'deer park', 'lead1', 6 from dual union
4 select 6, 'st thomas', 'lead', 4 from dual union
5 select 5, 'honolulu, hi', 'lead', 40 from dual union
6 select 5, 'honolulu, hi', 'lead1', 9 from dual
7 )
8 select office_center, office_name,
9 sum(decode(job_category_1, 'lead', total_hc)) lead_total,
10 sum(decode(job_category_1, 'lead1', total_hc)) lead1_total
11 from test
12 group by office_center, office_name
13 order by office_center;
OFFICE_CENTER OFFICE_NAME LEAD_TOTAL LEAD1_TOTAL
------------- ------------ ---------- -----------
1 deer park 10 6
5 honolulu, hi 40 9
6 st thomas 4
SQL>