‘壹’ 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()