易截截图软件、单文件、免安装、纯绿色、仅160KB

怎样用SQL语句判断一个数据表中至少N项不为空???

前两天一个学姐问我个SQL语句的问题,现在把解决方案贴出来,也算总结一下吧。
她的问题是:“一个表中有15个字段,用SQL语句判断其中5项或以上不为空,怎么判断,很急……”。
当时我很忙,没多看,呵呵,不够意思啦。但我请同事帮她写了个,以解燃眉之急,在此也谢谢小米。
今天再把QQ聊天记录翻出来,用本地数据库数据表试验了下, 有点问题,于是自己修改。
【详细情况,请参考SQL Server 联机丛书:索引“CASE‘;
ms-help://MS.SQLCC.v10/MS.SQLSVR.v10.zh-CHS/s10de_6tsql/html/658039ec-8dc2-4251-bc82-30ea23708cee.htm】

select distinct (case bulletinId when bulletinId then 1 else 0 end) +
(case title when title then 1 else 0 end) +
(case body when body then 1 else 0 end) +
(case bulletinType when bulletinType then 1 else 0 end) +
(case bulletinState when bulletinState then 1 else 0 end) +
(case companyId when companyId then 1 else 0 end) +
(case userId when userId then 1 else 0 end) +
(case createtime when createtime then 1 else 0 end) as '不为空字段数' from bulletin

当数据表bulletin中至少有一条记录时,也可以采取下面的方法。

1、简单 CASE 函数
select * from bulletin where
(case bulletinId when bulletinId then 1 else 0 end) +
(case title when title then 1 else 0 end) +
(case body when body then 1 else 0 end) +
(case bulletinType when bulletinType then 1 else 0 end) +
(case bulletinState when bulletinState then 1 else 0 end) +
(case companyId when companyId then 1 else 0 end) +
(case userId when userId then 1 else 0 end) +
(case createtime when createtime then 1 else 0 end) >= 5
2、CASE 搜索函数
select * from bulletin where
(case when bulletinId is not null or bulletinId!=''  then 1 else 0 end) +
(case when title is not null or title!='' then 1 else 0 end) +
(case when body is not null or body!='' then 1 else 0 end) +
(case when bulletinType is not null or bulletinType!='' then 1 else 0 end) +
(case when bulletinState is not null or bulletinState!='' then 1 else 0 end) +
(case when companyId is not


相关文档:

SQL Server 2005中的文件和文件组(二)

--> Title  : SQL Server 2005中的文件和文件组(二)
--> Author : wufeng4552
--> Date   : 2010-1-13
SQL Server 2005中的文件和文件组(ㄧ) 主要講解了理論部分
http://blog.csdn.net/wufeng4552/archive/2009/10/23/4716053.aspx
SQL Server 2005中的文件和文件组(二) 主要 ......

帮助你从MySQL移植到SQL Server的工具(SSMA)

For SQL Server2008:
http://www.microsoft.com/downloads/details.aspx?FamilyID=0e6168b0-2d0c-4076-96c2-60bd25294a8e&displaylang=en
For SQL Server2005:
http://www.microsoft.com/downloads/details.aspx?familyid=C6F14640-DA22-4604-AAAA-A45DE4A0CD4A&displaylang=en ......

理解 SQL Server 中系统表Sysobjects

总用户表:select count(*) 总表数 from sysobjects where xtype='u'
总用户表和系统表:select count(*) 总表数 from sysobjects where xtype in('u','s')
总视图数:select count(*) 总视图数 from sysobjects where xtype='v'
总存储过程数:select count(*) 总存储过程数 from sysobjects where xtype='p'
总触发器数:s ......

SQL索引高级应用

--结合sys.indexes和sys.index_columns,sys.objects,sys.columns查询索引所属的表或视图的信息
select
  o.name as 表名,
  i.name as 索引名,
  c.name as 列名,
  i.type_desc as 类型描述,
  is_primary_key as 主键约束,
  is_unique_constraint as 唯一约束,
  is_disable ......

SQL Server 行列转换示例

SQL Server的行列转换功能非常实用,但是由于其语法不好懂,使很多初学者都不愿意使用它。下面我就用示例的形式,逐一展现Pivot和UnPivot的魅力。如下图
 
1.从Wide Table of Months 转换到 Narrow Table的示例
select [Year],[Month],[Sales] from
(
select * from MonthsTable
)p
unpivot
(
[Sales] for ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号