内部查询的结果集能否与最终结果集一起显示
Can the result set of inner query can be displayed with final result set
我有一个 table 包含一些数据说
====================
Record | Record_Count
1 | 12
3 | 87
5 | 43
6 | 54
1 | 43
3 | 32
5 | 65
6 | 43
我有一个查询 returns 按记录
分组的记录计数总和
select record,sum(record_count)
FROM table_name
WHERE <conditions>
GROUP BY tcpa_code
ORDER BY sum(record_count)
结果是这样的
====================
Record | Record_Count
1 | 55
3 | 119
5 | 108
6 | 97
现在我还想要总计 record_count(所有记录计数的总和)。
问题是我还想要上面的结果集以及总计。
我试过这个
select sum(subquery.record_count)
from (
select record,sum(record_count)
FROM table_name
WHERE <conditions>
GROUP BY tcpa_code
ORDER BY sum(record_count) ) as subquery
但是通过使用这个我失去了个人 record_count 总和。
所以我的问题是我能否在单个查询中获得包含 record_count 每条记录总和和总计 record_count 的结果集?
您可以使用union
来实现您需要的:
(select cast(record as varchar(16)) record,sum(record_count) from schema.table
group by 1)
union
(select 'Grand_Total' as record,sum(record_count) from schema.table
group by 1);
在这里查看 - SQL Fiddle
如果您的数据库支持group by ... with rollup
,您也可以使用:
select ifnull(record,'Grand Total')record,sum(record_count) total_count
from schema.table
group by record asc with rollup
在这里查看 - SQL Fiddle
为了节省一些输入,可以使用常见的table表达式 (cte):
with cte as
(
select record, sum(record_count) rsum
FROM table_name
WHERE <conditions>
GROUP BY record
)
select record, rsum from cte
union all
select 'Grand_Total', sum(rsum) from cte
您应该利用 PostgreSQL 的 windows functions。
对于这个查询,
SELECT record, record_count, sum(record_count) OVER()
FROM (
SELECT record, sum(record_count) record_count
FROM table_name
WHERE <conditions>
GROUP BY tcpa_code
ORDER BY sum(record_count)
) as subquery
我有一个 table 包含一些数据说
====================
Record | Record_Count
1 | 12
3 | 87
5 | 43
6 | 54
1 | 43
3 | 32
5 | 65
6 | 43
我有一个查询 returns 按记录
分组的记录计数总和select record,sum(record_count)
FROM table_name
WHERE <conditions>
GROUP BY tcpa_code
ORDER BY sum(record_count)
结果是这样的
====================
Record | Record_Count
1 | 55
3 | 119
5 | 108
6 | 97
现在我还想要总计 record_count(所有记录计数的总和)。
问题是我还想要上面的结果集以及总计。
我试过这个
select sum(subquery.record_count)
from (
select record,sum(record_count)
FROM table_name
WHERE <conditions>
GROUP BY tcpa_code
ORDER BY sum(record_count) ) as subquery
但是通过使用这个我失去了个人 record_count 总和。
所以我的问题是我能否在单个查询中获得包含 record_count 每条记录总和和总计 record_count 的结果集?
您可以使用union
来实现您需要的:
(select cast(record as varchar(16)) record,sum(record_count) from schema.table
group by 1)
union
(select 'Grand_Total' as record,sum(record_count) from schema.table
group by 1);
在这里查看 - SQL Fiddle
如果您的数据库支持group by ... with rollup
,您也可以使用:
select ifnull(record,'Grand Total')record,sum(record_count) total_count
from schema.table
group by record asc with rollup
在这里查看 - SQL Fiddle
为了节省一些输入,可以使用常见的table表达式 (cte):
with cte as
(
select record, sum(record_count) rsum
FROM table_name
WHERE <conditions>
GROUP BY record
)
select record, rsum from cte
union all
select 'Grand_Total', sum(rsum) from cte
您应该利用 PostgreSQL 的 windows functions。
对于这个查询,
SELECT record, record_count, sum(record_count) OVER()
FROM (
SELECT record, sum(record_count) record_count
FROM table_name
WHERE <conditions>
GROUP BY tcpa_code
ORDER BY sum(record_count)
) as subquery