select type,sum(case vender when 'A' then pcs else 0 end),sum(case vender when 'C' then pcs else 0 end),sum(case vender when 'B' then pcs else 0 end) FROM tablename group by type
显示结果:
type vender pcs
电脑 A 1
电脑 A 1
光盘 B 2
光盘 A 2
手机 B 3
手机 C 3
初始化表table1
TRUNCATE TABLE table1
选择从10到15的记录
select top 5 * from (select top 15 * from table order by id asc) table_别名 order by id desc
数据类型转换
declare @numid int
declare @id varchar(50)
set @numid=2005
set @id=convert(varchar,@numid)
通过上述语句完成数据类型Int转换成varchar,其他转换类似,可参看convert函数
编辑本段技巧
1=1,1=2的使用
在SQL语句组合时用的较多
“where 1=1” 是表示选择全部 “where 1=2”全部不选,
如:
if @strWhere !='
begin
set @strSQL ='select count(*) as Total from ['+@tblName +'] where '+@strWhere
end
else
begin
set @strSQL ='select count(*) as Total from ['+@tblName +']'
end
我们可以直接写成
set @strSQL ='select count(*) as Total from ['+@tblName +'] where 1=1 and '+@strWhere