Home > database >  How to conduct field statistics?
How to conduct field statistics?

Time:10-19

Id name subj score
1 a language 88
2 a math 89
3 b language 89
()
A SQL shows: calculate the score field in 89 the number of occurrences of, and in the field most shown below, in () place, and can automatically change according to the records

CodePudding user response:

 

If not object_id (N 'tempdb for.. # a ') is null
Drop table # a
Go
The create table # a (
Id int,
[name] nvarchar (10),
Subj nvarchar (10),
Score nvarchar (10)
)
Go
Insert # a
Select 1, 'a', 'language', '88' union all
Select 2, 'a', 'mathematics',' 89 'union all
Select 3, 'b', 'English', '89'
Go
Select * from # a
Go

Select * from # a
The union
Select 4, 'a', ' ', the count (*) from # where a score=89


CodePudding user response:

The
reference 1/f, one tree forest _ response:
 

If not object_id (N 'tempdb for.. # a ') is null
Drop table # a
Go
The create table # a (
Id int,
[name] nvarchar (10),
Subj nvarchar (10),
Score nvarchar (10)
)
Go
Insert # a
Select 1, 'a', 'language', '88' union all
Select 2, 'a', 'mathematics',' 89 'union all
Select 3, 'b', 'English', '89'
Go
Select * from # a
Go

Select * from # a
The union
Select 4, 'a', ' ', the count (*) from # where a score=89


 

- can also be changed to the following 4 this
Select * from # a
The union
Select Max (id) + 1, "', ', the count (*) from # where a score=89



CodePudding user response:

Every step can explain? See not too understand

CodePudding user response:

The
reference 3 floor qq_26600333 response:
can explain each step? See not too understand


 


- whether the database is # a temporary table, if there is a delete
If not object_id (N 'tempdb for.. # a ') is null
Drop table # - this is a drop the temporary table
Go - go end can be understood as a
# - create a temporary table
The create table # a (
Id int,
[name] nvarchar (10),
Subj nvarchar (10),
Score nvarchar (10)
)
Go
- to the temporary table insert data
Insert # a
Select 1, 'a', 'language', '88' union all
Select 2, 'a', 'mathematics',' 89 'union all
Select 3, 'b', 'English', '89'
Go
- query temporary table
Select * from # a
Go
- joining together the query
Select * from # - a first query out all data
The union - using keywords for Mosaic
Select Max (id) + 1, "', ', the count (*) from # where a score=89




CodePudding user response:

Still don't fully understand, but thank god
  • Related