sql - 显示 SQL 表的总数

标签 sql postgresql

我正在尝试在此 sql 语法的底部添加一个 Total 行,但到目前为止还没有任何突破。我查看了以下内容,但两者都不符合我的条件。有人可以就此提供帮助吗。

Add a summary row with totals

Adding a total row to the end of query result

select dm.Builder ||' ('|| dm.Lot_Size || '''s)' as"Builder",count(sd.Address) "The Count",
dm."Construction_ID"
from input dm
left join data sd on sd.inputfk = dm.inputpk
and sd.Closing Date >= DATE '01/01/2017' and sd.Closing Date < DATE '06/30/2017'
where dm.Construction_ID = 'AJR'
group by dm.Builder,dm.Lot_Size, dm.Project_ID
having count(sd.Address) > 0
order by dm.Builder

当我运行它时:

  Builder            The Count     Construction_ID 
Jake's Homes (55's)     2               AJR
Jake's Homes (65's)     3               AJR
Maggie's Homes (65's)   5               AJR
Maggie's Homes (66's)   2               AJR
Maggie's Homes (75's)   3               AJR
Maggie's Homes (90's)   1               AJR

 Total ---------->     16

最佳答案

你的 group bydm.Project_ID, sd.Address 这可能是导致它的原因。

对于总数,你可以使用ROLLUP:

试试这个:

select coalesce(dm.Builder || ' (' || dm.Lot_Size || '''s)', 'Total') as "Builder",
    count(sd.Address) "The Count",
    dm."Construction_ID"
from input dm
left join data sd on sd.inputfk = dm.inputpk
    and sd.Closing date >= date '01/01/2017'
    and sd.Closing date < date '06/30/2017'
where dm.Construction_ID = 'AJR'
group by rollup(dm.Builder || ' (' || dm.Lot_Size || '''s)')
having count(sd.Address) > 0
order by "Builder"

关于sql - 显示 SQL 表的总数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42232085/

相关文章:

java - 如何重写sql语句以与hibernate一起工作?

php - 在 ProductSell 模型中,有一个日期列类型为 varchar,格式为 d/m/Y 我如何使用其间检索数据?

php - SQL:检查值是否在 tbl1 或 tbl2 中可用

postgresql - 如何解决 Postgresql 8.4 错误 'language “plpgsql” 已经存在'?

postgresql - 将单个文本字段转储到 psql 中的单独文件中

ruby-on-rails - postgres 中的小数列。有没有一种方法可以存储没有尾随零的值

sql - 使用 SQL Server 正则表达式过滤包含垃圾字符的行

php - 从表中选择 NOT NULL 列

sql - 按月统计的 Access 查询数

PostgreSQL:获取记录平均值的有效方法