+ -
当前位置:首页 → 问答吧 → 如何让在没有查询内容返回的情况下也显示一行数据

如何让在没有查询内容返回的情况下也显示一行数据

时间:2011-12-02

来源:互联网

例表:t1( hosname varchar2(20),hostype varhcar2(10) , cash number(12,2));
数据:(hostype数据为:区内,区外,省外,四类;数据随便列举,不代表实际意义)
  hosname hostype cash
  一院 区内 20000.00
  二院 区内 30000.00
  五院 省外 50000.00

select sum(cash),count(1) from t1 gorup by hostype

上面的情况,是只显示,存在hosttype的数据。但区外,和省内的数据,因为不存在所以没有。我现在想要结果:

区内 50000.00
区外 0.00
省内 0.00
省外 50000.00

如何实现?琢磨一上午了,没想出来...

作者: guorui0526   发布时间: 2011-12-02

SQL code
with t1 as (
select '一院' hosname,'区内' hostype,20000.00 cash from dual  union all
select '二院' hosname,'区内' hostype,30000.00 cash from dual  union all
select '五院' hosname,'省外' hostype,50000.00 cash from dual 
) 


select  '区内',sum(decode(t1.hostype,'区内',cash,0)) sum1 from t1  union all
select  '区外',sum(decode(t1.hostype,'区外',cash,0)) from t1  union all
select  '省外',sum(decode(t1.hostype,'省外',cash,0)) from t1  union all
select  '省内',sum(decode(t1.hostype,'省内',cash,0)) from t1


------
1 区内 50000
2 区外 0
3 省外 50000
4 省内 0

作者: dws2004   发布时间: 2011-12-02

引用 1 楼 dws2004 的回复:
SQL code
with t1 as (
select '一院' hosname,'区内' hostype,20000.00 cash from dual union all
select '二院' hosname,'区内' hostype,30000.00 cash from dual union all
select '五院' hosname,'省外' hostype,50000……


这个太具有针对性了。需要用group by 的。 是需要按定点做的统计信息. 而且要显示的数据列不只这几个。
只是出于特殊原因的考虑,不能把信息在这里提供完全...

作者: guorui0526   发布时间: 2011-12-02

测试数据:
SQL code

CREATE TABLE T30
(
    hosname VARCHAR2(20),
    hostype VARCHAR2(20),
    cash    NUMBER(10)
);

INSERT INTO T30 VALUES('一院', '区内', 20000);
INSERT INTO T30 VALUES('二院', '区内', 30000);
INSERT INTO T30 VALUES('五院', '省外', 50000);



结果:

作者: LuiseRADL   发布时间: 2011-12-02