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

sql中常用动态的行转列

//取出源数据
select groupname,totalnum,inputdate,groupid into #temp from
 (select count(*)as totalnum,p.groupid,g.groupname,convert(nvarchar(10),inputdate,120) as 'inputdate'
 from person p left join admin_group g on p.groupid = g.groupid and deleteflag = '0'
 where p.inactive='0' 
 group by p.groupid,g.groupname,convert(nvarchar(10),p.inputdate,120)) a
 left join
 (select a.groupid as areagroupid,b.groupname as area,b.areaid
 from admin_group a inner join admin_group b on a.upperid = b.groupid) b on a.groupid=b.areagroupid
//第一种方式能够控制显示的数据的样式,速度比较快
declare @s nvarchar(max)
select @s = 'select groupname as 社区名,groupid'
select @s = @s + ',isnull(max(case when inputdate=''' + cast(inputdate as varchar(10)) + ''' then totalnum end),0) as ''' + cast(inputdate as varchar(10)) + ''''  from #temp group by inputdate order by inputdate desc
select @s = @s + ' from #temp where groupid=71 group by groupname,groupid'
exec(@s)
//第二种使用pivot(sql2005以上版本中有)
declare @s nvarchar(max)
Select     @s=isnull(@s+',','')+quotename(inputdate) from #temp group by inputdate order by inputdate desc
exec('select * from #temp pivot (max(totalnum) for inputdate in('+@s+'))b')


相关文档:

使用SQLServer模板来写规范的SQL语句

如果你经常遇到下面的问题,你就要考虑使用SQL Server的模板来写规范的SQL语句了:
SQL初学者。
经常忘记常用的DML或是DDL SQL 语句。
在多人开发维护的SQL中,每个人都有自己的SQL习惯,没有一套统一的规范。
在SQL Server Management Studio中,已经给大家提供了很多常用的现成SQL规范模板。
SQL Server Management ......

sql语句 截取时间 只显示 年,月,日的格式


摘自http://blog.sina.com.cn/zhm85
SQL语句截取时间,只显示年月日(2004-09-12)
select CONVERT(varchar, getdate(), 120 )
‘getdate()’改为时间字段名‘createtime’
再重命名新加列(Select Name AS UName from Users)
例如 select convert(varchar(11),createtime,120) as Ndate fro ......

SQL SERVER 2005 高级查询(子查询查询)

--SQL高级程序设计:子查询
use AdventureWorks
GO
SELECT DISTINCT EmployeeID from HumanResources.JobCandidate WHERE EmployeeID IS NOT NULL;
SELECT e.EmployeeID,FirstName,LastName
from HumanResources.Employee e
INNER JOIN Person.Contact c
 ON e.ContactID = c.ContactID
WHERE e.EmployeeID IN ......

SQL Server中约束的介绍

摘要
对于SQL Server中的约束,想必大家并不是很陌生。但是约束中真正的内涵是什么,并不是很多人都很清楚的。本文以详细的文字来介绍了什么是约束,以及如何在数据库编程中应用和使用这些约束,来达到更好的编程效果。(本文部分内容参考了SQL Server联机手册)
内容
 
数据完整性分类
 
实体完整性
&nb ......

SQL 2005转换成2000 (SQL Server)


直接restore或附加应该是不行的, 用脚本+导数据肯定没有问题。
2005转到2000的步骤
1. 生成for 2000版本的数据库脚本
2005 的manger studio
-- 打开"对象资源管理器"(没有的话按F8), 连接到你的实例
-- 右键要转到2000的库
-- 任务
-- 生成脚本
-- 在"脚本向导"的"选择数据库"中, ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号