sql server with ...as 用法
SQL SERVER CTE
語法
[ WITH <common_table_expression> [ ,n ] ]
<common_table_expression>::=
expression_name [ ( column_name [ ,n ] ) ]
AS
( CTE_query_definition )
栗子1:
WITH tb_yield AS (
SELECT SUM(wdy.QTY) qty ,cl.LINE, wdy.WEEK FROM WIP_DAILY_YIELD wdy JOIN CFG_LINE cl
ON wdy.LINE_GUID = cl.GUID
WHERE SUBSTRING(wdy.WEEK,1,4) ='2018'
GROUP BY cl.LINE, wdy.WEEK
)
SELECT SUM(num) CNT, LINE,sum(qty) QTY FROM (
SELECT qty,LINE ,week,CONVERT(int,(ROW_NUMBER() OVER(PARTITION BY WEEK ORDER by qty desc))) as num FROM tb_yield
) AS A
WHERE A.num =1
GROUP BY A.LINE
栗子2:
with cte
as
(
select 1 as col1,'aa' as col2
union all
select 2,'bb'
)
--把cte的資料儲存在tb_cte表
select * into newtable from cte
select * from newtable ;
--運用cte,刪除資料
;with cte_delete
as
(
select * from newtable
)
delete from cte_delete where col1 = 1
相關文章
- Sql server with as update用法SQLServer
- sql server convert用法SQLServer
- SQL Server中@@ROWCOUNT的用法SQLServer
- sql server merge 的用法SQLServer
- SQL Server中distinct的用法SQLServer
- Sql Server Rollup 語句的用法SQLServer
- 初探SQL Server CE + Codesmith用法SQLServerMIT
- sql server select case when的用法SQLServer
- SQL Server中的dbcc page命令用法SQLServer
- SQL Server中的dbcc ind命令用法SQLServer
- SQL Server中的鎖型別及用法SQLServer型別
- Sql server 2005中output用法解析SQLServer
- SQL?Server新特性SequenceNumber用法介紹YTZBSQLServer
- SQL server 中SUBSTRING()以及CONVERT()的用法SQLServer
- [zt] SQL Server管理員必備的DBCC用法SQLServer
- SQL Server中row_number函式的常見用法SQLServer函式
- SQL AS 的用法SQL
- sql用法——group bySQL
- SQL——STUFF用法SQL
- SQL in ORACLE and SQL ServerSQLOracleServer
- sql ServerSQLServer
- sql server 2005中的分割槽函式用法(partition by 欄位)SQLServer函式
- SQL Server連線SQL Server、SQL Server連線ORACLE 連結伺服器SQLServerOracle伺服器
- SQL Server常用工具——SQL Server Powershell ExtensionsSQLServer
- SQL groupby sum 用法SQL
- Oracle sql trace用法OracleSQL
- SQL 中With as 的用法SQL
- SQL Server資料庫中Substring函式的用法例項詳解SQLServer資料庫函式
- SQL Server raiserrorSQLServerAIError
- SQL Server 鎖SQLServer
- SQL Server OptimizationSQLServer
- SQL SERVER 版本SQLServer
- SQL Server AttentionSQLServer
- sql server 使用SQLServer
- SQL Server教程SQLServer
- Moebius for SQL ServerSQLServer
- sql server agent與sql server有什麼區別 ?SQLServer
- SQL Server Express和SQL Server Compact的應用SQLServerExpress