sql 分組查詢問題
更新時間:2009年12月22日 22:11:03 作者:
sql 分組查詢問題,需要的朋友可以參考下。
情景一:
表中數(shù)據(jù)
name score
aaa 11
aaa 19
bbb 12
bbb 18
ccc 19
ddd 21
期望查詢結(jié)果如下
name score
aaa 30
bbb 30
ccc 19
ddd 21
---檢查表是否存在
if exists(select * from sysobjects where name='testSum')
drop table testSum
go
---創(chuàng)建表
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
情景二:
姓名 科目 分?jǐn)?shù)
張三 語文 30
張三 數(shù)學(xué) 50
張三 英語 70
李四 語文 50
李四 數(shù)學(xué) 80
李四 英語 90
期望查詢結(jié)果:
姓名 語文 數(shù)學(xué) 英語
張三 30 50 70
李四 50 80 90
---檢查表是否存在
if exists(select * from sysobjects where name='testScore')
drop table testScore
go
---創(chuàng)建表
create table testScore
(
tid int primary key identity(1,1),
tname varchar(30) null,
ttype varchar(10) null,
tscor int null
)
go
---插入數(shù)據(jù)
insert into testScore values ('張三','語文',90)
insert into testScore values ('張三','數(shù)學(xué)',20)
insert into testScore values ('張三','英語',50)
insert into testScore values ('李四','語文',30)
insert into testScore values ('李四','數(shù)學(xué)',47)
insert into testScore values ('李四','英語',78)
---查詢
select tname as '姓名' ,
max(case ttype when '語文' then tscor else 0 end) '語文',
max(case ttype when '數(shù)學(xué)' then tscor else 0 end) '數(shù)學(xué)',
max(case ttype when '英語' then tscor else 0 end) '英語'
from testScore
group by tname
情景三:
表:table1
字段:id , name
內(nèi)容:
----------------
1,aaa
1,bbb
2,ccc
2,ddd
3,eee
3,fff
--------------
希望結(jié)果:
---------------------
1 aaa bbb [aaa bbb之間半角空格區(qū)分,以下類似]
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中的數(shù)據(jù)select出來,得到下面第二個表的數(shù)據(jù),如何寫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列進(jìn)行分組,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
---創(chuàng)建表
create table testFlag
(
tid int primary key identity(1,1),
tname varchar(30) null,
tflag int null,
tscor int null
)
go
---插入數(shù)據(jù)
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
表中數(shù)據(jù)
name score
aaa 11
aaa 19
bbb 12
bbb 18
ccc 19
ddd 21
期望查詢結(jié)果如下
name score
aaa 30
bbb 30
ccc 19
ddd 21
復(fù)制代碼 代碼如下:
---檢查表是否存在
if exists(select * from sysobjects where name='testSum')
drop table testSum
go
---創(chuàng)建表
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
情景二:
姓名 科目 分?jǐn)?shù)
張三 語文 30
張三 數(shù)學(xué) 50
張三 英語 70
李四 語文 50
李四 數(shù)學(xué) 80
李四 英語 90
期望查詢結(jié)果:
姓名 語文 數(shù)學(xué) 英語
張三 30 50 70
李四 50 80 90
復(fù)制代碼 代碼如下:
---檢查表是否存在
if exists(select * from sysobjects where name='testScore')
drop table testScore
go
---創(chuàng)建表
create table testScore
(
tid int primary key identity(1,1),
tname varchar(30) null,
ttype varchar(10) null,
tscor int null
)
go
---插入數(shù)據(jù)
insert into testScore values ('張三','語文',90)
insert into testScore values ('張三','數(shù)學(xué)',20)
insert into testScore values ('張三','英語',50)
insert into testScore values ('李四','語文',30)
insert into testScore values ('李四','數(shù)學(xué)',47)
insert into testScore values ('李四','英語',78)
---查詢
select tname as '姓名' ,
max(case ttype when '語文' then tscor else 0 end) '語文',
max(case ttype when '數(shù)學(xué)' then tscor else 0 end) '數(shù)學(xué)',
max(case ttype when '英語' then tscor else 0 end) '英語'
from testScore
group by tname
情景三:
表:table1
字段:id , name
內(nèi)容:
----------------
1,aaa
1,bbb
2,ccc
2,ddd
3,eee
3,fff
--------------
希望結(jié)果:
---------------------
1 aaa bbb [aaa bbb之間半角空格區(qū)分,以下類似]
2 ccc ddd
3 eee fff
復(fù)制代碼 代碼如下:
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中的數(shù)據(jù)select出來,得到下面第二個表的數(shù)據(jù),如何寫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列進(jìn)行分組,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
復(fù)制代碼 代碼如下:
---檢查表是否存在
if exists(select * from sysobjects where name='testFlag')
drop table testFlag
go
---創(chuàng)建表
create table testFlag
(
tid int primary key identity(1,1),
tname varchar(30) null,
tflag int null,
tscor int null
)
go
---插入數(shù)據(jù)
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
相關(guān)文章
如何遠(yuǎn)程連接SQL Server數(shù)據(jù)庫的圖文教程
如何遠(yuǎn)程連接SQL Server數(shù)據(jù)庫的圖文教程...2007-03-03Sql Server 2012完全卸載方法 只需8步輕松卸載
這篇文章主要為大家詳細(xì)介紹了Sql Server 2012完全卸載方法,八步輕松實(shí)現(xiàn)Sql Server 2012完全卸載,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02SQL Server 2016 查詢存儲性能優(yōu)化小結(jié)
SQL Server 2016已經(jīng)發(fā)布了有半年多,相信還有很多小伙伴還沒有開始使用,今天我們來談?wù)凷QL Server 2016 查詢存儲性能優(yōu)化,希望大家能夠喜歡2016-01-01參考sql2012存儲過程寫的統(tǒng)計所有用戶表尺寸大小的示例
參考SQL2005, 2008和2012的系統(tǒng)存儲過程master.sys.sp_spaceused代碼后,寫了下面一條語句來方便平時統(tǒng)計所有用戶表尺寸大小2014-01-01SQL Server誤區(qū)30日談 第22天 資源調(diào)控器可以調(diào)控IO
資源調(diào)控器無法調(diào)控IO,希望下一個版本的SQL Server支持調(diào)控IO,調(diào)控IO對于對于減少對于大表的scan操作帶來的性能影響很有幫助2013-01-01SQL Server 不存在或訪問被拒絕(轉(zhuǎn))
在使用 SQL Server 的過程中,用戶遇到最多的問題莫過于連接失敗了。一般而言,有兩種連接SQL Server 的方式,一是利用 SQL Server 自帶的客戶端工具2009-06-06SQLServer2019 數(shù)據(jù)庫的基本使用之圖形化界面操作的實(shí)現(xiàn)
這篇文章主要介紹了SQLServer2019 數(shù)據(jù)庫的基本使用之圖形化界面操作的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04H2 數(shù)據(jù)庫導(dǎo)入CSV文件實(shí)現(xiàn)原理簡析
在開發(fā)應(yīng)用中經(jīng)常會碰到一些數(shù)據(jù)庫方面的問題,例如:csv文件導(dǎo)入數(shù)據(jù)庫,本文將以此問題進(jìn)行深入介紹,需要的朋友可以參考下2012-11-11