1. The basis of the query
1) Renaming columns
Select name as ' name ' from table name
2) Defining a constant column
Select whether = ' yes ' from table name
3 Top Usage percent
This can be used to get the first 20% fields.
Select top percent * from table name
4) Removing duplicate columns
Select DISTINCT column name from table name
5) Aggregate function
Max avg Count min sum
--Multiple aggregation results in one result set
Select
Maximum age = (select Max (aged) from table name),
Minimum age = (select Min. from table name)
6) between and
SELECT * FROM table where xx between 5 and 6
2.Union brings together two result sets using Union.
--Age wage
-- ————————
--19 $20000
--50 $20005
--30 $23000
--Summary $63005
-- queries all age groups for wages and displays all payroll totals. (like the table above)
Select
-Convert age to varchar type
CONVERT (varchar, [age]) as age
Sum ([salary]) as salary
from employee table
GROUP BY age
--merges two result sets into one result set
Union
Select
--Rollup is a constant column
' rollup ', sum (Salary)
from employee table
when you combine two result sets with union, the
two result set columns must be the same number and the data type corresponds.
This is why the age is converted to varchar in the code.
3.Order by
-ordered by for result set sorting,
--its order he's not going to get a field,
--You can also get an expression.
Select *
from table
ORDER by (age+salary)/2.0 desc