sql server 查詢,T-SQL查詢2

 2023-11-19 阅读 16 评论 0

摘要:? 嵌套子查詢 ??? 子查詢是一個嵌套在select、insert、update或delete語句或其他子查詢中的查詢。任何允許使用表達式的地方都可以使用子查詢。子查詢也稱為內部查詢或內部選擇,而包含子查詢的語句也成為外部查詢或外部選擇。 ? sql server 查詢?# from (select … ta

? 嵌套子查詢

??? 子查詢是一個嵌套在select、insert、update或delete語句或其他子查詢中的查詢。任何允許使用表達式的地方都可以使用子查詢。子查詢也稱為內部查詢或內部選擇,而包含子查詢的語句也成為外部查詢或外部選擇。

?

sql server 查詢?# from (select … table)示例

將一個table的查詢結果當做一個新表進行查詢
select * from (
    select id, name from student where sex = 1
) t where t.id > 2;

上面括號中的語句,就是子查詢語句(內部查詢)。在外面的是外部查詢,其中外部查詢可以包含以下語句:

???? 1、 包含常規選擇列表組件的常規select查詢

???? 2、 包含一個或多個表或視圖名稱的常規from語句

sql條件查詢、???? 3、 可選的where子句

???? 4、 可選的group by子句

???? 5、 可選的having子句

?

sql查詢表。# 示例

查詢班級信息,統計班級學生人生
select *, (select count(*) from student where cid = classes.id) as num 
from classes order by num;

?

# in, not in子句查詢示例

查詢班級id大于小于的這些班級的學生信息
select * from student where cid in (
    select id from classes where id > 2 and id < 4
);
?
查詢不是班的學生信息
select * from student where cid not in (
    select id from classes where name = '2班'
)

in、not in 后面的子句返回的結果必須是一列,這一列的結果將會作為查詢條件對應前面的條件。如cid對應子句的id;

SQL、?

# exists和not exists子句查詢示例

查詢存在班級id為的學生信息
select * from student where exists (
    select * from classes where id = student.cid and id = 3
);
?
查詢沒有分配班級的學生信息
select * from student where not exists (
    select * from classes where id = student.cid
);

exists和not exists查詢需要內部查詢和外部查詢進行一個關聯的條件,如果沒有這個條件將是查詢到的所有信息。如:id等于student.id;

?

T—SQL、?

# some、any、all子句查詢示例

?

-- t1表數據 2,3
-- t2表數據 1,2,3,4
-- '>all' 表示:t2表中列n的數據大于t1表中列n的數據的數,結果只有4.
select * from t2 where n > all(select n from t1 )    --4
select * from t2 where n > any(select n from t1 )    --3,4
select * from t2 where n > some(selectn from t1)      --3,4select * from t2 where n = all(select n from t1 )    --無數據
select * from t2 where n = any(select n from t1 )    --2,3
select * from t2 where n = some(selectn from t1)      --2,3select * from t2 where n < all(select n from t1 )    --1
select * from t2 where n < any(select n from t1 )    --1,2
select * from t2 where n < some(selectn from t1)      --1,2select * from t2 where n <>all (select n from t1 )    --1,4
select * from t2 where n <>any (select n from t1 )    --1,2,3,4
select * from t2 where n <>some(select n from t1)      --1,2,3,4

? 聚合查詢

sql2008怎么查詢表中數據,1、 distinct去掉重復數據

?

select distinct sex from student;
select count(sex), count(distinct sex) from student;

2、 compute和compute by匯總查詢
這里過于復雜,參考http://www.cnblogs.com/waitingfor/articles/2220698.html

3、 cube匯總

cube匯總和compute效果類似,但語法較簡潔,而且返回的是一個結果集。

select count(*), sex from student group by sex with cube;
select count(*), age, sum(age) from student where age is not null group by age with cube;

cube要結合group by語句完成分組匯總

?

CUBE運算符生成的結果集是多維數據集,多維數據集是事實數據的擴展,事實數據即記錄個別時間的數據,擴展建立在用戶準備分析的列上,這些列被稱為維,多維數據集是一個結果集,其中包含各緯度所有可能的交叉表格.

?

CUBE運算符是在Select語句的group by子句中指定的,group by應指定維度列和關鍵字with cube,結果集將包括維度列中各值的所有可能組合.

?

示例1.

?

Sql語句如下:

?

select * from student

?

?

select sex,sclass,sum(score) as 合計

?

from student

?

group by sex,sclass with cube

?

?

select sex,sclass,sum(score) as 合計

?

from student

?

group by sclass,sex with cube

?

?

Sql查詢時這樣運行:

?

1. 查詢到性別的第一個性別為男,則先查詢男生,然后分班級

?

2. 查詢完成之后,對性別為Sex為男的數據進行合計

?

3. 查詢性別為女的數據,查詢完成之后同樣也進行合計

?

4. 不分性別、班級進行合計匯總

?

5. 以上均是以性別為組來分類,因為至此時關于性別的所有匯總都已經完成

?

6. 按照sclass進行分組匯總.

?

注意:

?

1. 分類依據并不是根據select 中的順序,而是根據group by中的順序.

?

2. 盡量按照使select和group by中的字段順序一致,這樣在顯示起來看著更舒服,具體情況具體分析.

?

對于上述查詢的結果,我們可以看出,數據中存在空置問題,綁定到GridView后顯示如下:

?

?

此中效果并沒有達到能夠滿足實際項目中的需要,所以,我們對Sql語句應進行改進.

?

使用Grouping區分空值.

?

如何區分使用CUBE之后產生的空值和實際查詢中得到的空值.這個問題可以用grouping函數來解決.如果列中的值來來自查詢數據,則grouping返回0,如果列中的值是cube產生的空值,則返回1

?

示例2.

?

Sql如下:

?

select case when(grouping(sex)=1) then '小記' else sex

?

end as 性別,

?

case when(grouping(sclass)=1) then '小記' else sclass

?

end as 班級,

?

sum(score)

?

from student

?

group by sex,sclass with cube

?

?

在頁面上顯示時如下:

?

?

CUBE可以生成n維的多維數據集,即具有任意維目的多維數據集,只有一個維度的多維數據集可用于生成合計.

?

示例3:

?

SQL:

?

select case when(grouping(sex)=1) then '合計' else sex end as 性別,

?

sum(score) as 合計

?

from student

?

group by sex with cube

?

?

生成許多維度的數據集合結果可能很大,辦法就是生成一個大的視圖,選擇顯示即可.

?

轉載于:https://www.cnblogs.com/shuaihei/p/6224487.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/180210.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息