Must be SQL statement (vi) data query _mssql

Source: Internet
Author: User
Tags constant

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.