Group query, view, and transpose row and column

Source: Internet
Author: User

I:GroupGrouping query results

ExploitationSQLOfGroupClause to quicklyIt is easy to divide the query result table according to the specified fieldGroup, records with the same value are divided into one group.GroupClauseSQLOfAggregate functionsIStart to use.

[Basic format] : Select < Field expression > From < Table Name >Group by <Field name>  Ii. View 1. view definition:(1 ). View is 1 Zhang Virtual table , It indicates part of the data in a table. Or comprehensive data of multiple tables Its structure and data are built on Based on the query of the table(2 ). The view does not store data, but is stored in the reference In the original table (base table)(3 ). For the same original table, you can To create different View  2.View usage(1) Filter table Medium Rows(2) Prevent Unauthorized users from accessing sensitive data(3) Reduce database complexity(4) Set Multiple A physical database is abstracted as a logical database.  3. Transpose rows and columnsDeclare @ sqlstr varchar (8000) set @ sqlstr = 'select [transid], [keyno] '// read the column information to be transposed. Select @ sqlstr = @ sqlstr + ', (select sum ([expr4]) distinct from [best]. [DBO]. [view_temp] group by expr1 having [expr1] = ''' + expr1 + ''') as ['+ expr1 +'] 'from [best]. [DBO]. [view_temp] group by [expr1] // concatenate sqlselect @ sqlstr = @ sqlstr + ', [ssje], [yfje], [mlje], zlje from [view_temp] group by [keyno], [transid], [ssje], [yfje], [mlje], zlje '// execute sqlexec (@ sqlstr) // output the concatenated SQL print @ sqlstr

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.