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')
相关文档:
http://www.umgr.com/blog/PostView.aspx?bpId=36294
1. 执行sql语句
int sqlite3_exec(sqlite3*, const char *sql, sqlite3_callbacksql 语法
, void *, char **errmsg );
这就是执行一条 sql 语句的函数。
第1个参数不再说了,是前面open函数得到的指针。说了是关键数据结构。
第2个参数const char ......
1. SELECT
实例105
SELECT ID "编号",Name 姓名,
Math_Score '数学成绩', //怎么有的有AS,有的没有
Music_Score AS 音乐成绩,
English_Score AS 英文成绩
f ......
引用:http://blog.csdn.net/wizardlun/archive/2009/09/08/4531576.aspx
今天,终于把oracle中的数据库导入到sql server中了,方法如下:
一、在sql server中建个同名数据库,例如ssdb。
二、右键点击ssdb,选择“所有任务”——>“导入数据”,就会弹出一个“DTS ......
--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 ......