1。你的语句可以简化:
select 'aaa', count(distinct bbb) count1 from T_table1 where are= '010'
union
select 'aaa', count(distinct bbb) count1 from T_table1 where are= '021'2。“批注的东西在变,而且那些值是从一个表中取出来的”
那么就应该join那个表
select 'aaa',count(distinct bbb) count1 from t_table1 join 你的那个表
select 'aaa', count(distinct bbb) count1 from T_table1 where are= '010'
union
select 'aaa', count(distinct bbb) count1 from T_table1 where are= '021'2。“批注的东西在变,而且那些值是从一个表中取出来的”
那么就应该join那个表
select 'aaa',count(distinct bbb) count1 from t_table1 join 你的那个表
aaa|11|22|... --北京
bbb|33|44|... --天津
ccc|55|66|... --上海
ddd|77|88|... --广州
想把那个union通过一个循环来弄。jion怎么弄?多谢。
(
OriginType,
ContractNum
)
select t1.bbb,count(*) count1 from T_table1 t1,T_table2 t2 -- T_table2为地名编码表
where t1.are=t2.are
group by bbb;
SQL> select * from t_title; ID NAME
---------- ----------
1 北京
2 上海
3 广州已用时间: 00: 00: 00.20SQL> create view v_t as
2 select tt.name,t.* from t,(select name from t_title where id=1) tt
3 union
4 select tt.name,t.* from t,(select name from t_title where id=2) tt
5 union
6 select tt.name,t.* from t,(select name from t_title where id=3) tt;视图已建立。已用时间: 00: 00: 00.30
SQL> select * from v_t;NAME AAA BBB
---------- ---------- ----------
北京 1 2
北京 3 4
北京 5 6
广州 1 2
广州 3 4
广州 5 6
上海 1 2
上海 3 4
上海 5 6已选择9行。已用时间: 00: 00: 00.40
SQL> update t_title set name='天津' where id=1;--将北京的改为天津已更新 1 行。已用时间: 00: 00: 00.10
SQL> select * from v_t;NAME AAA BBB
---------- ---------- ----------
广州 1 2
广州 3 4
广州 5 6
上海 1 2
上海 3 4
上海 5 6
天津 1 2
天津 3 4
天津 5 6已选择9行。已用时间: 00: 00: 00.40
SQL> 是不是这个意思?
create view v_assetmanage_origin
(
OriginType,
ContractNum,
Sum--多一个sum
)
select 'aaa', t1.count1 ,t1.sum1
from
(
select count(bbb) count1, sum(ccc) sum1 from --多一个sum
(
select distinct bbb, ccc from
T_table1 where are= '010'--总是这个变
)
) t1
union
select 'aaa', t1.count1
from
(
select count(bbb) count1, sum(ccc) sum1 from --多一个sum
(
select distinct bbb ccc from
T_table1 where are= '021'--总是这个变
)
) t1
union
......
谢谢。