—————————Subqueries ——————————————
——– Use the result of the query as a sub-table, and query the table
select * from StuInfo s1, (select * from StuMarks where Score >80)s2 where s1.StuID=s2.StuID AND s1.StuName='Li Si'
——–Use the query result as a condition
select * from StuInfo WHERE StuID>(select StuID from StuInfo where StuName='Wangwu')
——–Use the subquery as a temporary table
select s1. ,s2 Score from StuInfo s1 left outer join (select from StuMarks where Subject='HTML') s2 on s1.StuID=s2.StuID
——– use subqueries as columns (deprecated)
select s1.*,(select Score from StuMarks s2 where s1.StuID=s1.StuID and subject='html') Score from StuInfo s1
———– use in and not in to complete the subquery
select StuName from StuInfo where StuID In (select StuID from StuMarks where Score >85 and Subject=' java')
——– use exist or not exist to complete subqueries
select * from StuInfo where exists (select * from StuMarks where StuMarks.StuID=StuInfo.StuID) ——–
use some, any, all for subqueries——
—————Aggregate Function ————————————————————
———— Use compute and compute by to summarize
—————— Sorting function
————sorting function over([grouping clause]sorting clause [desc][asc])
———— —sort clause order by sort column, sort column….
———— grouping sorting partition by grouping column, grouping column
————————row__number()函数
select row_number() over (order by Score DESC) AS 排名
s1.StuName,s2.Score from StuInfo s1,StuMarks s2 where s1.StuID=s2.StuID and s2.Subject=’Java’
————————rank函数
select rank() over (order by Score DESC) AS 排名
s1.StuName,s2.Score from StuInfo s1,StuMarks s2 where s1.StuID=s2.StuID and s2.Subject=’Java’
————————DENSE_RANK()函数
select dense_rank() over (order by Score DESC) AS 排名
s1.StuName,s2.Score from StuInfo s1,StuMarks s2 where s1.StuID=s2.StuID and s2.Subject=’Java’
————————-使用partition by 分组子句
select dense_rank() over (partition by s2.Subject order by Score DESC) AS 排名
s1.StuName,s2.Subject,s2.Score from StuInfo s1,StuMarks s2 where s1.StuID=s2.StuID
————————-公用表达式
————–CTE的基本用法
WITH expression_name[(column_name[,…n])]
as
(CTE_query_definition)
with StuInfo_Mark (StuID,StuName,Subject,Score)
as
(
select s1.StuID,s1.StuName,s2.Subject,
s2.Score from StuInfo s1,StuMarks s2
where s1.StuID=s2.StuID
)
select * from StuInfo_Mark
go
———————-2016.8.15