求一条查询 语句

haojielyb 2006-10-25 03:35:50
CREATE TABLE [dbo].[C_Care] (
[C_ID] [bigint] NOT NULL ,
[求职] [bit] NULL ,
[招聘] [bit] NULL ,
[房产咨询] [bit] NULL ,
[跳蚤市场] [bit] NULL ,
[汽车咨询] [bit] NULL ,
[教育] [bit] NULL ,
[美容] [bit] NULL ,
[医疗] [bit] NULL ,
[其他] [bit] NULL
) ON [PRIMARY]


C_ID 求职 招聘 房产咨询 跳蚤市场 汽车咨询 教育 美容 医疗 其他
-------------------- ---- ---- ---- ---- ---- ---- ---- ---- ----
0 1 NULL NULL NULL NULL NULL NULL NULL 1
13 1 1 1 NULL NULL NULL 1 1 1
19 1 1 1 1 0 NULL NULL 1 1

select
count(求职),count(招聘),count(房产咨询),count(跳蚤市场),

count(汽车咨询),count(教育),count(美容),count(医疗),count(其他)

from c_care

得到结果如下 :
3 2 2 1 1 0 1 2 3
期望结果
3 2 2 1 0 0 1 2 3
也就是
只计算为 true 得值
...全文
104 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
早起晚睡 2006-10-25
  • 打赏
  • 举报
回复
select
case (when count(求职) is null then 0 else count(求职) end) 求职,
(when count(招聘) is null then 0 else count(招聘) end) 招聘,
(when count(房产咨询) is null then 0 else count(房产咨询) end) 房产咨询,
(when count(汽车咨询) is null then 0 else count(汽车咨询) end) 汽车咨询,
(when count(跳蚤市场) is null then 0 else count(跳蚤市场) end) 跳蚤市场,
(when count(教育) is null then 0 else count(教育) end) 教育,
(when count(美容) is null then 0 else count(美容) end) 美容,
(when count(医疗) is null then 0 else count(医疗) end) 医疗,
(when count(其他) is null then 0 else count(其他) end) 其他
from c_care
dawugui 2006-10-25
  • 打赏
  • 举报
回复
SUM(CASE WHEN ISNULL([**],0)=1 THEN 1 ELSE 0 END)
haojielyb 2006-10-25
  • 打赏
  • 举报
回复
谢谢 给条语句好吗?
九斤半 2006-10-25
  • 打赏
  • 举报
回复
count(**)
改成
SUM(CASE WHEN ISNULL([**],0)=1 THEN 1 ELSE 0 END)

ps:**为各字段名
dawugui 2006-10-25
  • 打赏
  • 举报
回复
case

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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