婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > sql 分組查詢問題

sql 分組查詢問題

熱門標簽:小e電話機器人 申請400電話在哪辦理流程 百度地圖標注改顏色 貴州房產智能外呼系統供應商 鎮江網路外呼系統供應商 外呼運營商線路收費 電銷外呼有錄音系統有哪些 一個導航軟件能用幾個地圖標注點 臨沂智能電銷機器人加盟哪家好
情景一:
表中數據
name score
aaa 11
aaa 19
bbb 12
bbb 18
ccc 19
ddd 21
期望查詢結果如下
name score
aaa 30
bbb 30
ccc 19
ddd 21
復制代碼 代碼如下:

---檢查表是否存在
if exists(select * from sysobjects where name='testSum')
drop table testSum
go
---創建表
create table testSum
(
tid int primary key identity(1,1),
tname varchar(30) null,
tscor int null
)
go
insert into testSum (tname,tscor)
select 'aaa',11
union all
select 'aaa',19
union all
select 'bbb',12
union all
select 'bbb',18
union all
select 'ccc',19
union all
select 'ddd',21
---查詢語句
select tname ,sum(tscor) from testSum group by tname
---只查詢tscor總和為30的
select tname ,sum(tscor) from testSum group by tname having sum(tscor)=30

情景二:
姓名 科目 分數
張三 語文 30
張三 數學 50
張三 英語 70
李四 語文 50
李四 數學 80
李四 英語 90

期望查詢結果:

姓名 語文 數學 英語
張三 30 50 70
李四 50 80 90
復制代碼 代碼如下:

---檢查表是否存在
if exists(select * from sysobjects where name='testScore')
drop table testScore
go
---創建表
create table testScore
(
tid int primary key identity(1,1),
tname varchar(30) null,
ttype varchar(10) null,
tscor int null
)
go
---插入數據
insert into testScore values ('張三','語文',90)
insert into testScore values ('張三','數學',20)
insert into testScore values ('張三','英語',50)
insert into testScore values ('李四','語文',30)
insert into testScore values ('李四','數學',47)
insert into testScore values ('李四','英語',78)
---查詢
select tname as '姓名' ,
max(case ttype when '語文' then tscor else 0 end) '語文',
max(case ttype when '數學' then tscor else 0 end) '數學',
max(case ttype when '英語' then tscor else 0 end) '英語'
from testScore
group by tname

情景三:
表:table1
字段:id , name
內容:
----------------
1,aaa
1,bbb
2,ccc
2,ddd
3,eee
3,fff
--------------
希望結果:
---------------------
1 aaa bbb [aaa bbb之間半角空格區分,以下類似]
2 ccc ddd
3 eee fff
復制代碼 代碼如下:

f exists(select * from sysobjects where name='test1')
drop table test1
go
create table test1
(
tid int primary key identity(1,1),
tnum int null,
tname varchar(30) null
)
go
insert into test1 values (1,'aa')
insert into test1 values (1,'bb')
insert into test1 values (2,'cc')
insert into test1 values (2,'dd')
insert into test1 values (3,'ee')
insert into test1 values (3,'ff')
SELECT * FROM ( SELECT DISTINCT tnum FROM test1
)A
OUTER APPLY(
SELECT tname= STUFF(REPLACE(REPLACE(
(
SELECT tname FROM test1 N
WHERE tnum = A.tnum
FOR XML AUTO
), 'N tname="', ' '), '"/>', ''), 1, 1, '')
)N

情景四:
我需要將表tb中的數據select出來,得到下面第二個表的數據,如何寫select語句?
表tb
id a flag class
----------+---------+--------+---------
1 2 1 A
2 2 1 A
3 4 1 A
4 5 2 A
5 3 2 A
6 4 1 A
7 2 1 A
8 3 2 A
9 4 2 A
10 5 3 A
11 5 1 B
12 2 1 B
13 3 1 B
14 4 1 B
15 2 3 B
16 7 3 B
17 3 2 B
18 4 1 B
19 5 1 B
20 2 2 B
21 1 1 B
22 1 1 C
23 2 3 C
24 6 3 C
25 3 2 C
...
需要選取出如下的表,按class列進行分組,a1,a2,a3字段分別為flag=1、2、3時tb表中a字段的求和
選取后
a1 a2 a3 class
-----------+------------+-----------------+--------------
sum(a) sum(a) sum(a) A
sum(a) sum(a) sum(a) B
sum(a) sum(a) sum(a) C
sum(a) sum(a) sum(a) D
sum(a) sum(a) sum(a) E
sum(a) sum(a) sum(a) F
sum(a) sum(a) sum(a) G
復制代碼 代碼如下:

---檢查表是否存在
if exists(select * from sysobjects where name='testFlag')
drop table testFlag
go
---創建表
create table testFlag
(
tid int primary key identity(1,1),
tname varchar(30) null,
tflag int null,
tscor int null
)
go
---插入數據
insert into testFlag (tname,tflag,tscor)
select 'aaa',1,11
union all
select 'aaa',2,19
union all
select 'aaa',3,12
union all
select 'aaa',1,18
union all
select 'aaa',2,19
union all
select 'aaa',3,21
union all
select 'bbb',1,11
union all
select 'bbb',2,19
union all
select 'bbb',3,12
union all
select 'bbb',1,18
union all
select 'bbb',2,19
union all
select 'bbb',3,21
----查詢語句
select distinct tname,(select sum(tscor) from testFlag where tflag=1 and testFlag.tname = t.tname) as 'flag1',(select sum(tscor) from testFlag where tflag=2 and testFlag.tname = t.tname) as 'flag2',(select sum(tscor) from testFlag where tflag=3 and testFlag.tname = t.tname) as 'flag3' from testFlag t group by tname,tflag
您可能感興趣的文章:
  • 詳解SQL中Group By的使用教程
  • Laravel 實現Eloquent模型分組查詢并返回每個分組的數量 groupBy()
  • Django ORM 聚合查詢和分組查詢實現詳解
  • 分組查詢GROUP BY的使用與SQL執行順序的講解
  • MySQL分組查詢Group By實現原理詳解
  • 詳解MySQL中的分組查詢與連接查詢語句
  • Oracle中分組查詢group by用法規則詳解

標簽:日照 晉城 延邊 合肥 三明 保定 嘉興 澳門

巨人網絡通訊聲明:本文標題《sql 分組查詢問題》,本文關鍵詞  sql,分組,查詢,問題,sql,分組,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《sql 分組查詢問題》相關的同類信息!
  • 本頁收集關于sql 分組查詢問題的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 海安县| 镇安县| 股票| 西乌珠穆沁旗| 射洪县| 牙克石市| 舟山市| 西藏| 怀化市| 梁山县| 酒泉市| 萨嘎县| 新乡市| 达拉特旗| 从江县| 濮阳市| 安化县| 新宁县| 台安县| 汉寿县| 德格县| 广汉市| 开远市| 英山县| 德惠市| 桃园市| 习水县| 六盘水市| 罗山县| 开化县| 新平| 安康市| 大港区| 会昌县| 黄大仙区| 通城县| 金平| 麻栗坡县| 修文县| 太仆寺旗| 鹿泉市|