Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Oracle在一个表中显示多个总计_Oracle - Fatal编程技术网

从Oracle在一个表中显示多个总计

从Oracle在一个表中显示多个总计,oracle,Oracle,我试图通过以下查询计算名为DRAWING的表中的总行数: 按字段、平台从图纸组中选择字段、平台、计数(单据id)作为总计 但我还需要显示每个平台的附件/非附件总数 SQL: 从图纸中选择字段、平台、计数(doc_id),文件_bin不为空,按字段、平台分组 从文件为空的图纸中选择字段、平台、计数(文档id)作为非附件,按字段、平台分组 有没有办法将这3个值合并到一个视图中? 例如 字段、平台、总计、已连接、未连接 select field, platform, count(do

我试图通过以下查询计算名为DRAWING的表中的总行数: 按字段、平台从图纸组中选择字段、平台、计数(单据id)作为总计

但我还需要显示每个平台的附件/非附件总数

SQL:

从图纸中选择字段、平台、计数(doc_id),文件_bin不为空,按字段、平台分组

从文件为空的图纸中选择字段、平台、计数(文档id)作为非附件,按字段、平台分组

有没有办法将这3个值合并到一个视图中? 例如 字段、平台、总计、已连接、未连接

select 
  field, 
  platform, 
  count(doc_id) as total,
  sum(iif(file_bin is null, 1, 0)) as attached,
  sum(iif(file_bin is not null, 1, 0)) as non_attached
from drawing 
where doc_id is not null 
group by field, platform

多亏了道格拉斯·托西的建议,我成功地改用了案例法

选择字段, 平台, 将(文档id)计为总计, 总数(例) 当文件_bin为空时,则为1 当文件_bin不为空时,则0 完)如附件所示, 总数(例) 当文件_bin为空时,则为0 当文件_bin不为空时,则为1 结束)作为图纸的非附件,其中文档id不为空 按场地、平台分组

太好了


再次感谢Douglas

我会使用decode而不是case,不知道什么性能更好(未经测试):


附件中的sum(iif(file_bin为null,1,0)),第5行出现错误:ORA-00907:缺少右括号我肯定遗漏了什么,你能澄清一下吗?谢谢,我搜索了一些帖子,发现decode的性能与case相同。Decode非常适合我的桌子,谢谢。尽管case对于需要嵌套条件语句的SQL语句有优势
select field
,      platform
,      count(doc_id) as total
,      sum(decode(file_bin,null,1,0)) attached 
,      sum(decode(file_bin,null,0,1)) non_attached
from   drawing 
where  doc_id is not null 
group by field,platform