展开按查询分组的分组行

时间:2018-09-24 18:26:56

标签: oracle11g oracle-sqldeveloper

我很好奇Oracle SQL Developer中是否可以扩展报表或数据网格中的“按查询分组”中包含的行。例如,假设我有一个带有付款金额的付款交易。我将这些字段分组,并获得了总金额的项目总和。如果我有一个功能可以让我查看构成总付款额的交易中的项目,而不必删除“分组依据”字段和“汇总SUM”功能,那将是非常不错的。

使用下面的数据作为示例来显示我在寻找什么。下面的示例数据中只有两个表。 pymt_transactions和transaction_items

WITH pymt_transactions AS
(
  SELECT 1 AS tran_id FROM dual UNION
  SELECT 2 AS tran_id FROM dual UNION
  SELECT 3 AS tran_id FROM dual UNION
  SELECT 4 AS tran_id FROM dual UNION
  SELECT 5 AS tran_id FROM dual
) /* END pymt_transactions CTE */
--SELECT * FROM pymt_transactions;
, transaction_items AS
(
  SELECT 1 AS tran_id, 'T-Shirt' AS retail_item, 15 AS amt FROM dual UNION
  SELECT 1 AS tran_id, 'Shoes'   AS retail_item, 50 AS amt FROM dual UNION
  SELECT 1 AS tran_id, 'Pants'   AS retail_item, 40 AS amt FROM dual UNION
  SELECT 1 AS tran_id, 'Comb'    AS retail_item, 3  AS amt FROM dual UNION
  SELECT 2 AS tran_id, 'Sweater' AS retail_item, 15 AS amt FROM dual UNION
  SELECT 2 AS tran_id, 'Belt'    AS retail_item, 12 AS amt FROM dual UNION
  SELECT 2 AS tran_id, 'Pants'   AS retail_item, 40 AS amt FROM dual UNION
  SELECT 2 AS tran_id, 'Watch'   AS retail_item, 23 AS amt FROM dual
) /* END transaction_items CTE */

SELECT pt.tran_id, ti.retail_item, ti.amt
FROM pymt_transactions pt
  LEFT JOIN transaction_items ti ON ti.tran_id = pt.tran_id
ORDER BY pt.tran_id
;

上面产生了下面的结果集。

enter image description here

现在为分组依据版本。在上面的示例中使用相同的两个CTE:

SELECT pt.tran_id, COUNT(ti.retail_item) AS num_retail_items, 
NVL(SUM(ti.amt),0) AS payment_amount
FROM pymt_transactions pt
  LEFT JOIN transaction_items ti ON ti.tran_id = pt.tran_id
GROUP BY pt.tran_id
ORDER BY pt.tran_id
;

上面的“按查询分组”会产生以下结果,并按tran_id进行分组:

enter image description here

同样,我不必编写两个不同的查询,有没有一种方法可以让我简单地拥有一个Group By查询,并提供单击以展开以查看retail_items的功能?我已经使用了具有此功能的报表和其他系统。在交易1和交易2中都有四个零售项目。我想查看这些项目,而不必删除Grouping子句和正在生成payment_amount列中总计的汇总SUM函数。

3 个答案:

答案 0 :(得分:1)

尝试构建报告。

enter image description here 查看>报告。

用户定义的报告。

添加新报告。

提供主SQL,然后添加一个子报告,并将数据与:BIND绑在一起。

然后单击顶部的一行,并在下面获取所需的详细数据。 enter image description here

如果您愿意,也可以开始使用Charts。

答案 1 :(得分:1)

可以一次访问所有值,也许这可以解决?

SELECT pt.tran_id
, ti.retail_item
, ti.amt
,COUNT(ti.retail_item) over (Partition by pt.tran_id)
,SUM(ti.amt)  over (Partition by pt.tran_id)
FROM pymt_transactions pt
  LEFT JOIN transaction_items ti ON ti.tran_id = pt.tran_id
ORDER BY pt.tran_id
;

答案 2 :(得分:1)

好,让我们尝试建立深入的报告。 首先建立一个用户定义报告,计算总和并添加一个过滤器(我相信大写字母在这里很重要) enter image description here

然后使用初始sql添加另一个报告。 enter image description here

并添加对sumcnt报告的引用 enter image description here

现在运行顶部报告并右键单击它,您将获得以下信息: enter image description here

祝你好运