Scenario 1:
Table Data
Name score
AAA 11
AAA 19
Bbb 12
Bbb 18
CCC 19
Ddd 21
The expected query result is as follows:
Name score
AAA 30
Bbb 30
CCC 19
Ddd 21
CopyCode The Code is as follows: --- check whether the table exists
If exists (select * From sysobjects where name = 'testsum ')
Drop table testsum
Go
--- Create a table
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
--- Query statement
Select tname, sum (tscor) from testsum group by tname
--- Only query 30 tscor total
Select tname, sum (tscor) from testsum group by tname having sum (tscor) = 30
Scenario 2:
Name subject score
Zhang San Chinese 30
John's mathematics 50
John's English 70
Li Si language 50
Li Si math 80
Li Si English 90
Expected query results:
name: Chinese, mathematics, and English
Zhang San 30 50 70
Li Si 50 80 90 copy Code the code is as follows: --- check whether the table exists
If exists (select * From sysobjects where name = 'testscore ')
drop table testscore
go
--- create a table
Create Table testscore
(
TID int Primary Key Identity (1, 1 ),
tname varchar (30) null,
tType varchar (10) null,
tscor int null
)
go
--- insert data
insert into testscore values ('zhang san', 'China', 90)
insert into testscore values ('zhang san ', 'mat', 20)
insert into testscore values ('zhang san', 'English ', 50)
insert into testscore values ('Li si', 'China ', 30)
insert into testscore values ('Li si', 'mat', 47)
insert into testscore values ('Li si', 'English ', 78)
--- query
select tname as 'name',
MAX (Case tType when' 'then tscor else 0 end) 'China ',
MAX (Case tType when 'mate' then tscor else 0 end) 'mate',
MAX (Case tType when 'then tscor else 0 end) 'English '
from testscore
group by tname
Scenario 3:
Table: Table1
Field: ID, name
Content:
----------------
1, AAA
1, BBB
2, CCC
2, DDD
3, eee
3, fff
--------------
Expected results:
---------------------
1 aaa bbb [aaa bbb is differentiated by halfwidth spaces, similar to the following]
2 ccc ddd
3 eee fff Copy code The Code is as follows: 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, 'A ')
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
)
Outer apply (
Select tname = stuff (replace (
(
Select tname from test1 n
Where tnum = A. tnum
For XML auto
), '<N tname = "',''), '"/>',''), 1, 1 ,'')
) N
Scenario 4:
I need to select the data in Table TB to get the data in the second table below. How do I write the SELECT statement?
Table TB
ID a flag class
---------- + --------- + -------- + ---------
1 2 1
2 2 1
3 4 1
4 5 2
5 3 2
6 4 1
7 2 1
8 3 2
9 4 2
10 5 3
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
...
The following table needs to be selected and grouped by class column. The A1, A2, and A3 fields are the sum of fields a in the TB table when flag = 1, 2, and 3 respectively.
After selection
A1 A2 A3 class
----------- + ------------ + ----------------- + --------------
Sum (a) sum (a)
Sum (a) sum (a) B
Sum (a) sum (a) c
Sum (a) sum (a) d
Sum (a) sum (a) E
Sum (a) sum (a) f
Sum (a) sum (a) g Copy code The Code is as follows: --- check whether the table exists
If exists (select * From sysobjects where name = 'testflag ')
Drop table testflag
Go
--- Create a table
Create Table testflag
(
TID int Primary Key Identity (1, 1 ),
Tname varchar (30) null,
Tflag int null,
Tscor int null
)
Go
--- Insert data
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
---- Query statement
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