行列转换等(学习中)行列转换等经典SQL语句
行列转换等(学习中)
行列转换等(学习中)
1.--行列转换
行列转换等(学习中)
原表:   姓名     科目   成绩
行列转换等(学习中)           张三      语文     
80
行列转换等(学习中)            张三     数学    
90
行列转换等(学习中)          张三     物理    
85
行列转换等(学习中)         李四      语文    
85
行列转换等(学习中)           李四      物理    
82
行列转换等(学习中)          李四      英语    
90
行列转换等(学习中)            李四      政治    
70
行列转换等(学习中)          王五      英语    
90
行列转换等(学习中)
行列转换等(学习中)转换后的表: 姓名       数学    物理     英语    语文    政治
行列转换等(学习中)                       李四          
0          82         90       85        70
行列转换等(学习中)                       王五         
0           0          90       0         0
行列转换等(学习中)                       张三        
90        85          0       80        0
行列转换等(学习中)
行列转换等(学习中)实例:
行列转换等(学习中)
create table cj --创建表cj
行列转换等(学习中)
(
行列转换等(学习中)    ID       
Int IDENTITY (1,1)     not null--创建列ID,并且每次新增一条记录就会加1
行列转换等(学习中)
    Name     Varchar(50),  
行列转换等(学习中)    Subject 
Varchar(50),
行列转换等(学习中)    Result   
Int,
行列转换等(学习中)    
primary key (ID)      --定义ID为表cj的主键     
行列转换等(学习中)
);
行列转换等(学习中)
--Truncate table cj
行列转换等(学习中)--
Select * from cj
行列转换等(学习中)
Insert into cj
行列转换等(学习中)
Select '张三','语文',80 union all
行列转换等(学习中)
Select '张三','数学',90 union all
行列转换等(学习中)
Select '张三','物理',85 union all
行列转换等(学习中)
Select '李四','语文',85 union all
行列转换等(学习中)
Select '李四','物理',82 union all
行列转换等(学习中)
Select '李四','英语',90 union all
行列转换等(学习中)
Select '李四','政治',70 union all
行列转换等(学习中)
Select '王五','英语',90
行列转换等(学习中)
--行列转换
行列转换等(学习中)
Declare @sql varchar(8000)
行列转换等(学习中)
Set @sql = 'Select Name as 姓名'
行列转换等(学习中)
Select @sql = @sql + ',sum(case Subject when '''+Subject+''' then Result else 0 end) ['+Subject+']'
行列转换等(学习中)
from (select distinct Subject from cj) as cj --把所有唯一的科目的名称都列举出来
行列转换等(学习中)
Select @sql = @sql+' from cj group by name'
行列转换等(学习中)
Exec (@sql)
行列转换等(学习中)
行列转换等(学习中)
行列转换等(学习中)
2. 行列转换--合并
行列转换等(学习中)
原表:   班级    学号    
行列转换等(学习中)           
1          1  
行列转换等(学习中)            
1          2
行列转换等(学习中)            
1          3
行列转换等(学习中)            
2          1
行列转换等(学习中)            
2          2
行列转换等(学习中)            
3          1
行列转换等(学习中)转换后的表: 班级 学号           
行列转换等(学习中)                     
1   1,2,3
行列转换等(学习中)                       
2   1,2
行列转换等(学习中)                       
3   1  
行列转换等(学习中)
行列转换等(学习中)实例:
行列转换等(学习中)
Create table ClassNo --创建表ClassNo
行列转换等(学习中)
(
行列转换等(学习中)    ID 
Int IDENTITY(1,1not null--创建列ID,并且每次新增一条记录就会加1
行列转换等(学习中)
    Class Varchar(50),    --班级列
行列转换等(学习中)
    Number Varchar(50),    --学号列
行列转换等(学习中)
    Primary Key(ID)        --定义ID为表ClassNo的主键
行列转换等(学习中)
);
行列转换等(学习中)
--Truncate Table ClassNo
行列转换等(学习中)--
Select * from ClassNo
行列转换等(学习中)
Insert Into ClassNo
行列转换等(学习中)
Select 1,1 Union all
行列转换等(学习中)
Select 1,2 Union all
行列转换等(学习中)
Select 1,3 Union all
行列转换等(学习中)
Select 2,1 Union all
行列转换等(学习中)
Select 2,2 Union all
行列转换等(学习中)
Select 3,1
行列转换等(学习中)
行列转换等(学习中)创建一个合并的函数
行列转换等(学习中)
--Drop Function KFReturn
行列转换等(学习中)
Create Function KFReturn(@Class Varchar(50))
行列转换等(学习中)
Returns Varchar(8000)
行列转换等(学习中)
as
行列转换等(学习中)
Begin
行列转换等(学习中)
Declare @str Varchar(8000)
行列转换等(学习中)
Set @str = ''
行列转换等(学习中)
Select @str = @str + cast(Number as Varchar(50)) + ',' from ClassNo Where Class = @Class
行列转换等(学习中)
Set @str = SubString(@str,1,len(@str)-1)
行列转换等(学习中)
Return(@str)
行列转换等(学习中)
End
行列转换等(学习中)
行列转换等(学习中)
--调用自定义函数得到结果
行列转换等(学习中)
Select Distinct Class,dbo.KFReturn(Class) From ClassNo
行列转换等(学习中)
行列转换等(学习中)
行列转换等(学习中)
3:列转行
行列转换等(学习中)
--Drop Table ColumnToRow
行列转换等(学习中)
Create table ColumnToRow
行列转换等(学习中)(
行列转换等(学习中)   ID 
Int IDENTITY(1,1not null--创建列ID,并且每次新增一条记录就会加1
行列转换等(学习中)
   a int,
行列转换等(学习中)   b 
int,
行列转换等(学习中)   c 
int,
行列转换等(学习中)   d 
int,
行列转换等(学习中)   e 
int,
行列转换等(学习中)   f 
int,
行列转换等(学习中)   g 
int,
行列转换等(学习中)   h 
int,
行列转换等(学习中)   
Primary Key(ID)        --定义ID为表ColumnToRow的主键     
行列转换等(学习中)
);
行列转换等(学习中)
--Truncate Table ColumnToRow
行列转换等(学习中)--
Select * from ColumnToRow
行列转换等(学习中)
Insert Into ColumnToRow
行列转换等(学习中)
Select 15,9,1,0,1,2,4,2 Union all
行列转换等(学习中)
Select 22,34,44,5,6,7,8,7 Union all
行列转换等(学习中)
Select 33,44,55,66,77,88,99,12
行列转换等(学习中)
行列转换等(学习中)
Declare @sql Varchar(8000)
行列转换等(学习中)
Set @sql = ''
行列转换等(学习中)
Select @sql = @sql + rtrim(name) + ' from ColumnToRow union all Select ' from SysColumns Where id = object_id('ColumnToRow')
行列转换等(学习中)
Set @sql = SubString(@sql,1,len(@sql)-70)
行列转换等(学习中)
--70的长度就是这个字符串'from ColumnToRow union all Select ID from ColumnToRow union all Select ',因为它会把ID这一列的值也算进去,所以要把它截掉
行列转换等(学习中)
Exec ('Select ' + @sql + ' from ColumnToRow')
行列转换等(学习中)
行列转换等(学习中)
行列转换等(学习中)
4. 如何取得一个数据表的所有列名
行列转换等(学习中)方法如下:先从sysobjects系统表中取得数据表的systemid,然后再syscolumns表中取得该数据表的所有列名。
行列转换等(学习中)SQL语句如下:
行列转换等(学习中)
Declare @objid int,@objname char(40)
行列转换等(学习中)
set @objname = 'ColumnToRow'
行列转换等(学习中)
--第1种方法
行列转换等(学习中)
select @objid = id from sysobjects where id = object_id(@objname)
行列转换等(学习中)
select 'Column_name' = name from syscolumns where id = @objid order by colid
行列转换等(学习中)
--或也可以写成
行列转换等(学习中)
select name as 'Column_name' from syscolumns where id = @objid order by colid
行列转换等(学习中)
--第2种方法:
行列转换等(学习中)
Select name as 'Column_Name' from SysColumns where id = object_id(@objnameOrder by colid
行列转换等(学习中)
行列转换等(学习中)
5. 通过SQL语句来更改用户的密码
行列转换等(学习中)修改别人的,需要sysadmin role
行列转换等(学习中)
Exec Sp_password '原始密码','更改后密码','账号'
行列转换等(学习中)
Exec sp_password null,ok,sa
行列转换等(学习中)
行列转换等(学习中)
6. 怎么判断出一个表的哪些字段不允许为空?
行列转换等(学习中)
Declare @objname Varchar(50)
行列转换等(学习中)
set @objname = 'ColumnToRow'
行列转换等(学习中)
Select Column_Name from information_schema.Columns where is_nullable = 'No' and Table_Name = @objname
行列转换等(学习中)
行列转换等(学习中)
7. 如何在数据库里找到含有相同字段的表?
行列转换等(学习中)a. 查已知列名的情况
行列转换等(学习中)
Select a.name as Columnname,b.name as tablename from SysColumns a inner join sysobjects b on a.id = b.id
行列转换等(学习中)
and b.type = 'U' and a.name = '您要查找的字段名'
行列转换等(学习中)b. 未知列名查所有在不同表出现过的列名
行列转换等(学习中)
Select s.name as tablename,s1.name as columnname from SysColumns s1,Sysobjects s
行列转换等(学习中)
Where s1.id = s.id and s.Type = 'U' and Exists (Select 1 from syscolumns s2 where s1.name = s2.name and s1.id <> s2.id)
行列转换等(学习中)
行列转换等(学习中)
8.查询第N行数据
行列转换等(学习中)假设id是主键:
行列转换等(学习中)
select *
行列转换等(学习中)
from (select top N * from 表) aa
行列转换等(学习中)
where not exists(select 1 from (select top N-1 * from 表) bb where aa.id=bb.id)
行列转换等(学习中)
行列转换等(学习中)
9. SQL Server日期计算
行列转换等(学习中)a. 一个月的第一天
行列转换等(学习中)
SELECT DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)
行列转换等(学习中)b. 本周的星期一
行列转换等(学习中)
SELECT DATEADD(wk, DATEDIFF(wk,0,getdate()), 0)
行列转换等(学习中)c. 一年的第一天
行列转换等(学习中)
SELECT DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)
行列转换等(学习中)d. 季度的第一天
行列转换等(学习中)
SELECT DATEADD(qq, DATEDIFF(qq,0,getdate()), 0)
行列转换等(学习中)e. 上个月的最后一天
行列转换等(学习中)
SELECT dateadd(ms,-3,DATEADD(mm, DATEDIFF(mm,0,getdate()), 0))
行列转换等(学习中)f. 去年的最后一天
行列转换等(学习中)
SELECT dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0))
行列转换等(学习中)g. 本月的最后一天
行列转换等(学习中)
SELECT dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+10))
行列转换等(学习中)h. 本月的第一个星期一
行列转换等(学习中)
select DATEADD(wk, DATEDIFF(wk,0,dateadd(dd,6-datepart(day,getdate()),getdate())), 0)
行列转换等(学习中)i. 本年的最后一天
行列转换等(学习中)
SELECT dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate())+10))
行列转换等(学习中)

相关文章:

  • 2021-08-20
  • 2021-12-28
  • 2021-09-04
  • 2021-06-16
  • 2021-07-21
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-13
  • 2021-06-20
相关资源
相似解决方案