一、SELECTselect [ALL|DISTINCT] select_list[into new table]FROM table_source[where serch_conditaion][GROUP BY group_by_expression][Having serc ...
一、SELECT
1、建立表格ifobject_id('tb')isnotnulldroptabletbgocreate table tb (姓名varchar(10),课程varchar(10),分数int)insert into tb values ('张三','语文',74)insert into tb values ('张三','数学',83)insert into tb values ('张三','物理',93)insertintotbvalues ('李四','语文',74)insert into tb values ('李四','数学',84)insert into tb values ('李四','物理',94)goselect * from tbgo姓名 课程 分数---------- ---------- -----------张三 语文 74张三 数学 83张三 物理 93李四 语文 74李四 数学 84李四 物理 94 2、使用SQL Server 2000静态SQL--cselect姓名, max(case 课程 when '语文' then 分数 else 0 end)语文, max(case 课程 when '数学' then 分数 else 0 end)数学, max(case 课程 when '物理' then 分数 else 0 end)物理from tbgroup by 姓名姓名 语文 数学 物理---------- ----------- ----------- -----------李四 74 84 94张三 74 83 93 3、使用SQL Server 2005静态SQLselect*fromtb pivot(max(分数)for课程in(语文,数学,物理))a
三、
UNPIVOT用于将列明转为列值(即列转行),在SQL Server 2000可以用UNION来实现
完整语法:
table_source --数据源 转化后的表结构查询 例如:select 姓名,课程,分数 from tb1
UNPIVOT(
value_column --列值(本身列名的值 如:语文 列 值 83 ) 例如:分数
FOR pivot_column --列名 (集合列转成行后的名字 例如:课程)
IN(<column_list>) --集合参数 (例如:[语文],[数学],[物理])
)
示例:
create table employee(empid integer,fname nvarchar(20),lname nvarchar(20))insert into employeeselect 2021110,'小','张' insert into employeeselect 2021110,'小','李' create table test(id integer,num integer) insert into testselect 2021110,2 insert into testselect 2,2 期望结果语句:select * from employee2021110,'大','张'2021110,'大','李'2,'大','大'
SQL语句实现:
<row>
<MyHobby>
<MyHobby>
那么其他类型的列怎么自定义? 没关系,我们将它们转换成字符串类型就行啦!例如:
SELECT '{'+STR(hobbyID)+'}','[ '+hName+' ]' FROM @hobby FOR
SELECT B.sName,LEFT(StuList,LEN(StuList)-1) as hobby FROM (
SELECT B.sName,LEFT(StuList,LEN(StuList)-1) as hobby FROM (
海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com
原标题:SQL 语句及关键字的用法
关键词:sql
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。