Oracle SQL - 具有多个标头的条件聚合透视

时间:2018-05-30 23:00:45

标签: sql oracle header pivot

这个问题是我之前post的大大增强版本,希望我能在这里得到一些帮助。我有下面的表/视图与样本或虚拟数据:
TABLE / VIEW名称: CUST_HOTEL_VIEW

+----+----------------+---------------+---------------+--------+---------------+
| ID |    Customer    |     Hotel     | Booked_Status | Budget | Booked_Amount |
+----+----------------+---------------+---------------+--------+---------------+
|  1 | John Smith     | Beverly Hills | Booked        |   2000 |           500 |
|  2 | John Smith     | Royal Palms   | Cancelled     |   3000 |               |
|  3 | Marilyn Lawson | Beverly Hills |               |    500 |               |
|  4 | John Smith     | Ritz-Carlton  |               |    250 |               |
|  5 | Marilyn Lawson | Royal Palms   |               |    700 |               |
|  6 | Sarah Elliot   | Royal Palms   | Cancelled     |   1500 |               |
|  7 | Sarah Elliot   | Ritz-Carlton  | Booked        |   2000 |          1500 |
|  8 | Sarah Elliot   | Royal Palms   | Booked        |   2500 |          1000 |
+----+----------------+---------------+---------------+--------+---------------+

需要帮助以获得具有多个标题的输出,即获得客户数和每个酒店的预算总和(以下是excel数据透视表的输出): enter image description here

我正在使用Oracle 12c R1 db,如果我使用条件聚合查询有帮助;因为我发现这更简单,更容易理解。

我尝试了以下查询以获取计数和预订金额:

SELECT COALESCE(CUSTOMER, 'Grand Total') as " " ,
       (case when COUNT(booked_status) != 0 then count(booked_status) else null end) as "# Booked",
sum(case when booked_status = 'Booked' THEN booking_amount ELSE null END) as "Booked Amount"
FROM CUST_HOTEL_VIEW
GROUP BY ROLLUP(CUSTOMER)
order by CUSTOMER

并得到以下输出:

+--------------+----------+---------------+
|              | # Booked | Booked Amount |
+--------------+----------+---------------+
| John Smith   |        1 |           500 |
| Sarah Elliot |        2 |          2500 |
| Grand Total  |        3 |          3000 |
+--------------+----------+---------------+

但是无法使用合并标头获得输出。

我感谢任何帮助。

谢谢,
Richa

更新 添加下面的最终输出表部分: 最终输出表:

+--------------+---------------+--------+-------------+--------+--------------+--------+-------------+------+
|   CUSTOMER   |      Beverly Hills     |      Royal Palms     |      Ritz-Carlton     |      Grand Total   |
+--------------+---------------+--------+-------------+--------+--------------+--------+-------------+------+
|              | Count         | Sum    | Count       | Sum    | Count        | Sum    | Count       | Sum  |
+--------------+---------------+--------+-------------+--------+--------------+--------+-------------+------+
| Sarah Elliot | 0             | (null) | 1           | 2500   | 1            | 2000   | 2           | 4500 |
| John Smith   | 1             | 2000   | 0           | (null) | 0            | (null) | 1           | 2000 |
| Grand Total  | 1             | 2000   | 1           | 2500   | 1            | 2000   | 3           | 6500 |
+--------------+---------------+--------+-------------+--------+--------------+--------+-------------+------+

1 个答案:

答案 0 :(得分:1)

从问题的描述中我无法推断究竟是什么要求,但在我看来,你正在寻找类似的东西:

演示:http://sqlfiddle.com/#!4/dbd49/7

SELECT *
FROM (
  SELECT CUSTOMER, HOTEL, BUDGET
  FROM Table1
  WHERE BOOKED_STATUS = 'Booked'
)
PIVOT (
  count(*) as cnt, sum(BUDGET) as budget
  FOR HOTEL IN ('Beverly Hills','Royal Palms','Ritz-Carlton')
 )
|     CUSTOMER | 'Beverly Hills'_CNT | 'Beverly Hills'_BUDGET | 'Royal Palms'_CNT | 'Royal Palms'_BUDGET | 'Ritz-Carlton'_CNT | 'Ritz-Carlton'_BUDGET |
|--------------|---------------------|------------------------|-------------------|----------------------|--------------------|-----------------------|
| Sarah Elliot |                   0 |                 (null) |                 1 |                 2500 |                  1 |                  2000 |
|   John Smith |                   1 |                   2000 |                 0 |               (null) |                  0 |                (null) |