关于统计的问题
下面有一段测试代码:
create table A1
(
C1 char(8),
C2 char(8)
)
create table A2
(
C1 char(8),
C2 int
)
insert into A1
select 'A ', 'A1 ' union all
select 'B ', 'A1 ' union all
select 'C ', 'C1 '
insert into A2
select 'A ',10 union all
select 'A ',20 union all
select 'A ',15 union all
select 'B ',10 union all
select 'B ',20 union all
select 'B ',15 union all
select 'B ',10 union all
select 'B ',20 union all
select 'B ',15
select * from A1
select * from A2
--1处
select distinct A1.C1,sum(A2.C2) from A1 left outer join A2 on A1.C1=A2.C1 group by A1.C1
--2处
select distinct A1.C1,sum(A2.C2) from A1 left outer join A2 on A1.C1=A2.C1 and A1.C2 = 'A1 ' group by A1.C1
--3处
select distinct A1.C1,sum(A2.C2) from A1 left outer join A2 on A1.C1=A2.C1 and A1.C2 != 'A1 ' group by A1.C1
运行结果为:
C1
-------- -----------
A 45
B 90
C NULL
(所影响的行数为 3 行)
警告: 聚合或其它 SET 操作消除了空值。
C1
-------- -----------
A 45
B NULL
C NULL
(所影响的行数为 3 行)
警告: 聚合或其它 SET 操作消除了空值。
C1
-------- -----------
A NULL
B 90
C NULL
(所影响的行数为 3 行)
为什么我在2处加上了条件"A1.C2 = 'A1 '",它还把C列出来,而且只统计A的?
为什么我在3处把条件写反,它却只统计B的?
如果我只想要A1.C2 = 'A1 '的行呢?该怎么写?
如:
C1
-------- -----------
A 45
B