请问如何用SQL语句分类统计某个属性的null值和不为null的记录数?

fluxshadow 2010-03-28 06:03:59

如题。
假设A,B字段有下列记录
A B
1 <null>
2 aaa
3 bbb
4 <null>
5 <null>

统计结果为
2
3

(2个不为null,3个null).谢谢!~
...全文
763 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
SQL77 2010-03-28
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 beirut 的回复:]
引用 9 楼 sql77 的回复:
引用 4 楼 beirut 的回复:
SQL code
--or
select count(b),count(1)-count(b) from @tb

UP,
COUNT(1) 是会自动去除NULL的

不会呀,你是不是记错了?

SQL code
--> 测试数据:@tb
declare @tb table([A] int,[B] ……
[/Quote]
是,弄错了,COUNT(列)才去NULL
东那个升 2010-03-28
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 fluxshadow 的回复:]
能不能汇总呢。
把不为null的用同一个字符显示。
[/Quote]

看7#的
黄_瓜 2010-03-28
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 fluxshadow 的回复:]
能不能汇总呢。
把不为null的用同一个字符显示。
[/Quote]
没明白,请写出你想要的结果
黄_瓜 2010-03-28
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 sql77 的回复:]
引用 4 楼 beirut 的回复:
SQL code
--or
select count(b),count(1)-count(b) from @tb

UP,
COUNT(1) 是会自动去除NULL的
[/Quote]
不会呀,你是不是记错了?
--> 测试数据:@tb
declare @tb table([A] int,[B] varchar(3))
insert @tb
select 1,null union all
select 2,'aaa' union all
select 3,'bbb' union all
select 4,null union all
select 5,null

select count(b),count(1)-count(b) from @tb
/*
----------- -----------
2 3
警告: 聚合或其他 SET 操作消除了空值。

(1 行受影响)
*/
fluxshadow 2010-03-28
  • 打赏
  • 举报
回复
能不能汇总呢。
把不为null的用同一个字符显示。

ACMAIN_CHM 2010-03-28
  • 打赏
  • 举报
回复
select count(*) from t where b is not null
union all
select count(*) from t where b is null
SQL77 2010-03-28
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 beirut 的回复:]
SQL code
--or
select count(b),count(1)-count(b) from @tb
[/Quote]
UP,
COUNT(1) 是会自动去除NULL的
喜-喜 2010-03-28
  • 打赏
  • 举报
回复
select count(*) from TB--统计总的记录数
select count(*) from TB where B isnull--统计B列中属性值为null的记录数
--接下来不用说了吧!
htl258_Tony 2010-03-28
  • 打赏
  • 举报
回复
IF OBJECT_ID('[TB]') IS NOT NULL 
DROP TABLE [TB]
GO
CREATE TABLE [TB]([A] INT,[B] VARCHAR(3))
INSERT [TB]
SELECT 1,NULL UNION ALL
SELECT 2,'AAA' UNION ALL
SELECT 3,'BBB' UNION ALL
SELECT 4,NULL UNION ALL
SELECT 5,NULL

SELECT 'NULL数量' 名称,SUM(CASE WHEN B IS NULL THEN 1 ELSE 0 END) 数量 FROM TB
UNION ALL
SELECT '非NULL数量',SUM(CASE WHEN B IS NULL THEN 0 ELSE 1 END) 数量 FROM TB

/*
名称 数量
---------- -----------
NULL数量 3
非NULL数量 2

(2 行受影响)
*/
PxxxP 2010-03-28
  • 打赏
  • 举报
回复

--> 测试数据:[TB]
if object_id('[TB]') is not null drop table [TB]
create table [TB]([A] int,[B] varchar(3))
insert [TB]
select 1,null union all
select 2,'aaa' union all
select 3,'bbb' union all
select 4,null union all
select 5,null

select count(1) from TB
group by 0/ASCII(B)
order by 1


/*

-----------
2
3

(2 行受影响)

*/

drop table TB
PxxxP 2010-03-28
  • 打赏
  • 举报
回复

--> 测试数据:[TB]
if object_id('[TB]') is not null drop table [TB]
create table [TB]([A] int,[B] varchar(3))
insert [TB]
select 1,null union all
select 2,'aaa' union all
select 3,'bbb' union all
select 4,null union all
select 5,null

select count(1)
from (select B=case when B is null then null else '' end from TB)g
group by B
order by 1


/*

-----------
2
3

(2 行受影响)

*/

drop table TB

黄_瓜 2010-03-28
  • 打赏
  • 举报
回复
--or
select count(b),count(1)-count(b) from @tb
黄_瓜 2010-03-28
  • 打赏
  • 举报
回复
--> 测试数据:@tb
declare @tb table([A] int,[B] varchar(3))
insert @tb
select 1,null union all
select 2,'aaa' union all
select 3,'bbb' union all
select 4,null union all
select 5,null

select count(b),sum(case when [B] is null then 1 else 0 end) from @tb
/*
----------- -----------
2 3
警告: 聚合或其他 SET 操作消除了空值。

(1 行受影响)


*/
永生天地 2010-03-28
  • 打赏
  • 举报
回复
select count(*)
from t
group by (case when b is not null then 1 else 2 end)
永生天地 2010-03-28
  • 打赏
  • 举报
回复
select count(*) from t where b is not null
union all
select count(*) from t where b is null

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧