SQLServer2012 book learning seventy-eight statistical data with a view

Chapter VII of the query and statistical data

  Goal: to understand the composition, use wildcards and aggregation algorithm, the SELECT statement in each clause of the SELECT statement to use, sub-queries and connection method query, use the SELECT statement to solve practical problems of inquiry

  1, the full syntax of the SELECT statement, including SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY clause. Shihai UNION queries can be used in, or the EXCEPT and compare the result set, a synthesis of the final result set. 

      select * into new table from table where () group by () order by () asc desc (升序,降序)

  2, Tsuhaifu

    % Represents any number of characters, _ represents any character, any character [] brackets represent listed, [^] not represent any one character in brackets

    LIKE '% AB' returns any ending AB a character LIKE 'AB%' return any order AB end of a character LIKE '[AB]%' return arbitrary beginning with A or B is a character LIKE 'A [^ a] % 'return any string begins with a, not to a second character

    [%] Indicates a wildcard match without% pure, LIKE '5 [%]' matches the character represented 5%

  3 polymerizable function

    Statistical functions polymerization function is also known, is a set of values ​​is calculated and returns a value, unobstructed use with GROUP BY. In addition to COUNT, others will ignore the NULL value

      COUNT (ALL | DISTINCT) seeking the number of columns, ALL removal column is empty, DISTINCT repeated removal and blank columns

      SUN (ALL | DISTINCT) summing

      MAX (ALL | DISTINCT) seeking maximum

      MIN (ALL | DISTINCT) for the minimum

      AVG (ALL | DISTINCT) averaged

 Code:

. 1  the USE XK
 2  the GO 
. 3  - Query Class table of contents 
. 4  the SELECT  *  the FROM class
 . 5  the GO 
. 6  - updated performance information 
. 7  the UPDATE Elective the SET course_score = . 3  the WHERE COURSE_ID = ' 11,111,116 '     OR COURSE_ID = ' 11,111,114 ' 
. 8  the GO 
. 9  - Query class name and department number 
10  the SELECT class_name, department_id the FROM class
 11  GO
12  - - 5 query data and the need for separate top 
13  the SELECT  TOP  5  *   the FROM Elective
 14  GO 
15  - Number of modifying the curriculum to the subjects chosen \ 
16  UPDATE Course, the SET student_num = ( the SELECT  COUNT (COURSE_ID) the FROM Elective the WHERE Course, .course_id = elective.course_id)  
 . 17  the GO 
18 is  - - or 
. 19  the DECLARE  @id  int 
20 is  the SET  @id = 11111111 
21 is  the WHILE @id < 11,111,116 
22 is      the BEGIN 
23 is          the UPDATE Course the SET student_num = ( the SELECT  COUNT (COURSE_ID) the FROM Elective the WHERE COURSE_ID = @id ) the WHERE COURSE_ID = @id 
24          the SET  @id + = . 1 
25      the END 
26 is  the GO 
27  - isolated Elective table the number of students the most and least three courses   
28   
29   
30  the SELECT   TOP  3     COURSE_NAME, student_num  
 31  the FROMCourse    the ORDER  BY student_num the ASC 
32   
33 is  the SELECT  the TOP  . 3    COURSE_NAME, student_num  
 34 is      the FROM Course   the ORDER  BY student_num DESC 
35  the GO 
36   - modified Duration 
37 [  the SELECT   TEACHER_NAME the AS  ' teacher's name ' , COURSE_NAME the AS  ' program name ' 
38 is  the FROM Course 
 39  the WHERE course_time = ' Thursday night ' 
40  the GO 
41 is - SUNSTRING 2, taken from one taken 12 0,2 01 taken string index is not 0, the equivalent of only interception 1 
42 is  the SELECT  *  the FROM Student 
 43 is  the WHERE  the SUBSTRING (student_name, 1 , 2 ) = ' Wang '  OR  the SUBSTRING (student_name, 1 , 2 ) = ' Bob ' 
44 is  the GO 
45  - query software elective class 1 where 
46 is  the SELECT   COURSE_NAME, elective.student_id, student_name, class_name, course_time the FROM Student
 47  the JOIN class the ON class.class_id=student.class_id AND class.class_name='软件1班'
48 JOIN elective ON elective.student_id =student.student_id
49 JOIN course ON course.course_id=elective.course_id
50 --查询小猪的选修情况
51 SELECT  course_name,elective.student_id,student_name,class_name,course_time FROM student 
52 JOIN class ON class.class_id=student.class_id  
53 JOINElective the ON elective.student_id = student.student_id
 54 is  the JOIN Course the ON course.course_id = elective.course_id the WHERE student_name = ' pig ' 
55  the GO 
56 is  - check out the selected program no 
57 is  the SELECT  *  the FROM Course the WHERE COURSE_ID the NOT  the IN ( the SELECT  DISTINCT COURSE_ID the FROM Elective)

Chapter VIII founded management views

  

  

  

  

Guess you like

Origin www.cnblogs.com/27floor/p/11259013.html