首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

SQL多表统计总数?请大家帮帮忙?解决方法

2012-03-20 
SQL多表统计总数?请大家帮帮忙?清问比如我有3个表一模一样的表,要统计某列字段出现一共出现次数,怎么写呀。

SQL多表统计总数?请大家帮帮忙?
清问
比如我有3个表一模一样的表,要统计某列字段出现一共出现次数,怎么写呀。

A表

a b
1 1
2 2
3 3
4 4
5 5

B表
a b
11 1
22 2
33 3
4 4
55 5

C表
a b
11 1
2 2
3 3
44 4
55 5

统计a列
结果为
a sum
1 1
2 2
3 2
4 2
5 1
11 2
22 1
33 1
44 1
55 2

请大家帮帮忙?

[解决办法]

SQL code
create table A(a int, b int)insert into a values(1 ,1)insert into a values(2 ,2)insert into a values(3 ,3)insert into a values(4 ,4)insert into a values(5 ,5)create table b(a int, b int)insert into b values(11 ,1)insert into b values(22 ,2)insert into b values(33 ,3)insert into b values(4 ,4)insert into b values(55 ,5)create table C(a int, b int)insert into c values(11 ,1)insert into c values(2 ,2)insert into c values(3 ,3)insert into c values(44, 4)insert into c values(55, 5)goselect a , count(1) [sum] from (select a , b from aunion allselect a , b from bunion allselect a , b from c) tgroup by a /*a           sum         ----------- ----------- 1           12           23           24           25           111          222          133          144          155          2(所影响的行数为 10 行)*/drop table a,b , c 

热点排行