例如: select ProductID, Name, ProductNumber, IsNull(Color, ‘‘), IsNull(Size, ‘‘), ListPrice
from Production.Product order by 2 eg3: as 关键字:给表列取别名。
例如: select ProductID, Name, ProductNumber, IsNull(Color, ‘‘) as Color, IsNull(Size, ‘‘) as Size, ListPrice
from Production.Product order by 2 eg4: wildcard: 通配符 例如: select * from Production.Product where name like ‘%Mountain%‘ ----Wildcard % matches any zero or more characters select * from Production.Product
where name like ‘_ountain%‘ eg5: in ; not in 例如: select * from Production.Product where size in (‘20‘, ‘50‘,‘55‘) select * from Production.Product where size not in (‘20‘, ‘50‘,‘55‘) eg6: is null ; is not null 例如: select * from Production.Product where size is null select * from Production.Product where size is not null eg6: and ; or 例如: select * from Production.Product where color = ‘red‘ and color = ‘black‘ select * from Production.Product where color = ‘red‘ or color = ‘black‘ 5. 聚合函数 eg1: count ; distinct 例如: select count(SalesPersonID)
from [Sales].[SalesOrderHeader] where SalesPersonID is not null select distinct(SalesPersonID) from [Sales].[SalesOrderHeader] where SalesPersonID is not null select count(distinct(SalesPersonID)) from [Sales].[SalesOrderHeader] where SalesPersonID is not null eg2: Avg, Min, Max,Sum 例如: select Avg(SalesPersonID) as AverageTotalSales , Min(SalesPersonID) as MinimumTotalSales , Max(SalesPersonID) as MaximumTotalSales , Sum(SalesPersonID) as SummaryTotalSales from [Sales].[SalesOrderHeader] eg3: The classical T-SQL query !!! 例如: select SalesPersonID, OrderDate, Max(TotalDue) as MaximumTotalSales from [Sales].[SalesOrderHeader] where SalesPersonID is not null and OrderDate > ‘2016/1/1‘ group by SalesPersonID, OrderDate having Max(TotalDue) > 150000 order by SalesPersonID desc 6. 小技巧 eg1: 如何显示Line Number? 解决方案: 在SS Management Studio 最上面一行 ribbon 里面找到 Tool --> Options eg2: 如何自由转换 queries 大小写? 解决方案: 在SS Management Studio 最上面一行 ribbon 里面找到 Edit --> Advanced --> Make UpperCase / LowerCase
SQL SERVER Study
标签: