當前位置:首頁 » 編程語言 » sqlwithon
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sqlwithon

發布時間: 2022-05-17 06:44:24

『壹』 sql with xx as 編號

示例代碼如下:

;with a as(select * from table_a),
b as (select * from a where id in(3,4,5))
select * from b

記得一定要有逗號間隔開兩個查詢,最後一個查詢前是沒有逗號的

『貳』 誰能告訴我到底SQL的WITH CUBE,和WITH ROLLUP實現什麼樣的效果,區別是什麼,要是能舉例說明,感激不盡

The ROLLUP operator is useful in generating reports that contain subtotals and totals. The ROLLUP operator generates a result set that is similar to the result sets generated by the CUBE operator. For more information, see Summarizing Data Using CUBE.

The differences between CUBE and ROLLUP are:

CUBE generates a result set showing aggregates for all combinations of values in the selected columns.

ROLLUP generates a result set showing aggregates for a hierarchy of values in the selected columns.
For example, a simple table Inventory contains:

Item Color Quantity
-------------------- -------------------- --------------------------
Table Blue 124
Table Red 223
Chair Blue 101
Chair Red 210

This query generates a subtotal report:

SELECT CASE WHEN (GROUPING(Item) = 1) THEN 'ALL'
ELSE ISNULL(Item, 'UNKNOWN')
END AS Item,
CASE WHEN (GROUPING(Color) = 1) THEN 'ALL'
ELSE ISNULL(Color, 'UNKNOWN')
END AS Color,
SUM(Quantity) AS QtySum
FROM Inventory
GROUP BY Item, Color WITH ROLLUP

Item Color QtySum
-------------------- -------------------- --------------------------
Chair Blue 101.00
Chair Red 210.00
Chair ALL 311.00
Table Blue 124.00
Table Red 223.00
Table ALL 347.00
ALL ALL 658.00

(7 row(s) affected)

If the ROLLUP keyword in the query is changed to CUBE, the CUBE result set is the same, except these two additional rows are returned at the end:

ALL Blue 225.00
ALL Red 433.00

The CUBE operation generated rows for possible combinations of values from both Item and Color. For example, not only does CUBE report all possible combinations of Color values combined with the Item value Chair (Red, Blue, and Red + Blue), it also reports all possible combinations of Item values combined with the Color value Red (Chair, Table, and Chair + Table).

For each value in the columns on the right in the GROUP BY clause, the ROLLUP operation does not report all possible combinations of values from the column (or columns) on the left. For example, ROLLUP does not report all the possible combinations of Item values for each Color value.

以上是Books Online中的原文.
兩者都是配合Group by進行統計,CUBE會對Group by後的每個欄位值進行組合統計,而對於ROLLUP,Group by後靠左的欄位不會對靠右的欄位值產生所有可能的組合統計.(翻譯水平有限,汗一個!看例子意會就行)

『叄』 sql執行順序以及on和where的區別

(1.)select語句的執行順序
Processing Order of the SELECT statement
The following steps show the processing order for a SELECT statement.
1.FROM
2.ON
3.JOIN
4.WHERE
5.GROUP BY
6.WITH CUBE or WITH ROLLUP
7.HAVING
8.SELECT
9.DISTINCT
10.ORDER BY
11.TOP
(5)SELECT DISTINCT
(7)TOP(<top_specification>) <select_list>
(1)FROM <left_table> <join_type> JOIN <right_table> ON <on_predicate>
(2)WHERE <where_predicate>
(3)GROUP BY <group_by_specification>
(4)HAVING <having_predicate>
(6)ORDER BY <order_by_list>
T-SQL在查詢各個階級分別幹了什麼:
(1)FROM 階段
FROM階段標識出查詢的來源表,並處理表運算符。在涉及到聯接運算的查詢中(各種join),主要有以下幾個步驟:
a.求笛卡爾積。不論是什麼類型的聯接運算,首先都是執行交叉連接(cross join),求笛卡兒積,生成虛擬表VT1-J1。
b.ON篩選器。這個階段對上個步驟生成的VT1-J1進行篩選,根據ON子句中出現的謂詞進行篩選,讓謂詞取值為true的行通過了考驗,插入到VT1-J2。
c.添加外部行。如果指定了outer join,還需要將VT1-J2中沒有找到匹配的行,作為外部行添加到VT1-J2中,生成VT1-J3。
經過以上步驟,FROM階段就完成了。概括地講,FROM階段就是進行預處理的,根據提供的運算符對語句中提到的各個表進行處理(除了join,還有apply,pivot,unpivot)
(2)WHERE階段
WHERE階段是根據<where_predicate>中條件對VT1中的行進行篩選,讓條件成立的行才會插入到VT2中。
(3)GROUP BY階段
GROUP階段按照指定的列名列表,將VT2中的行進行分組,生成VT3。最後每個分組只有一行。
(4)HAVING階段
該階段根據HAVING子句中出現的謂詞對VT3的分組進行篩選,並將符合條件的組插入到VT4中。
(5)SELECT階段
這個階段是投影的過程,處理SELECT子句提到的元素,產生VT5。這個步驟一般按下列順序進行
a.計算SELECT列表中的表達式,生成VT5-1。
b.若有DISTINCT,則刪除VT5-1中的重復行,生成VT5-2
c.若有TOP,則根據ORDER BY子句定義的邏輯順序,從VT5-2中選擇簽名指定數量或者百分比的行,生成VT5-3
(6)ORDER BY階段
根據ORDER BY子句中指定的列明列表,對VT5-3中的行,進行排序,生成游標VC6.
如果On和where只能選其一的話:
先進行on的過濾, 而後才進行join, 這樣就避免了兩個大表產生全部數據的笛卡爾積的龐大數據.
這些步驟執行時, 每個步驟都會產生一個虛擬表,該虛擬表被用作下一個步驟的輸入。這些虛擬表對調用者(客戶端應用程序或者外部查詢)不可用。只是最後一步生成的表才會返回 給調用者。
如果沒有在查詢中指定某一子句,將跳過相應的步驟。
(2) 那 on 和where 那個更高效呢
如果是inner join, 放on和放where產生的結果一樣, 但沒說哪個效率速度更高? 如果有outer join (left or right), 就有區別了, 因為on生效在先, 已經提前過濾了一部分數據, 而where生效在後.
綜合一下, 感覺還是放在on里更有效率, 因為它先於where執行.
先笛卡爾積, 然後再on過濾, 如果join是inner的, 就繼續往下走, 如果join 是left join, 就把on過濾掉的左主表中的數據再添加回來; 然後再執行where里的過濾;
on中不是最終過濾, 因為後面left join還可能添加回來, 而where才是最終過濾.
只有當使用外連接(left, right)時, on 和 where 才有這個區別, 如果用inner join, 在哪裡制定都一樣, 因為on 之後就是where, 中間沒有其它步驟.

『肆』 SQL創建表裡邊的with是什麼意思

你寫上去的編譯的時候有點小錯誤,正確的應該是這樣寫的
Create table [dbo].[adminitable](
[adminpassword] [varchar](50) null,
[adminname] [varchar](20) null,

constraint [pk_adminitable] primary key clustered
(
[adminname] asc
)
with (pad_index=off,statistics_norecompute=off,ignore_p_key=off,allow_row_locks=on,allow_page_locks=on)
on [primary])
on [primary]
constraint 是子句限制
on [primary]是指的該表位於primary文件組,也就是主文件組,一個資料庫可以分為n個文件組

with 後面接的是索引描述
pad_index是指定非頁級索引頁的數據充滿度...

『伍』 mysql 怎麼實現sql的with遞歸

概念說不一定理解,給你一個例子你自己參悟。 SELECT TEACHER FROM C AS X WHERE UNIQUE(SELECT TEACHER FROM C AS Y WHERE Y.TEACHER=X.TEACHER);

『陸』 sql中with as的用法

你可以查詢CTE,即common_table_expression,創建個臨時表。
用途:1,以前的子查詢可以用它代替了,看上去很明了;2,也即他的優點,可以遞歸調用:select uinon all select cte
用法你可以F1。
注意:1,一般我們寫 ;with cte as , 因為若他不是批處理的開始則加;分號。
2,一個with中 不同的表用,逗號分開,如
;with cet1 as ()
,cte2 as()
cte3 as()