Learning SQL statements (strong group by and select from modes)

Source: Internet
Author: User

Powerful group
Copy codeThe Code is as follows:
Select stdname,
Isnull (sum (case stdsubject when 'chemistry 'then Result end), 0) [chemistry],
Isnull (sum (case stdsubject when 'mate' then Result end), 0) [math],
Isnull (sum (case stdsubject when 'physical 'then Result end), 0) [physical],
Isnull (sum (case stdsubject when 'China' then Result end), 0) [language]
From # student
Group by stdname

Combined with sum + case, group by can change the record (ROW) in table 1 to the field (column) in table 2 ). If there is no case in Sum, the value can only be the sum of all subjects. After case is used, the result of a certain subject is used. Then, here we use several Sum, each subject has a sum, so in Table 1, the "row" of a certain subject occupies a record is changed to a record of someone in table 2, and each subject has a field.

Use the select from (select from) mode to generate an SQL statement
Copy codeThe Code is as follows:
Declare @ SQL varchar (4000)
Set @ SQL = 'select stdname'
Select @ SQL = @ SQL + ', isnull (sum (case stdsubject when ''' + stdsubject + ''' then Result end), 0) [' + stdsubject + ']'
From (select distinct stdsubject from # student) as
Select @ SQL = @ SQL + 'from # student group by stdname'
Print @ SQL
Exec (@ SQL)


Copy codeThe Code is as follows:
Select [name] into # tmpCloumns
From tempdb. dbo. syscolumns
Where id = object_id ('tempdb. dbo. # student2 ')
And [name] <> 'stdname'
Select * from # tmpCloumns

Declare @ strSql nvarchar (800)
Select @ strSql =''
Select @ strSql = @ strSql + 'Union all' + char (10) + char (13) +
'Select [stdname], ''' + [name] + ''' as [subject], ['+ [name] +'] '+ char (10) + char (13) +
'From [# student2] '+ char (10) + char (13)
From # tmpCloumns

Select @ strSql = substring (@ strSql, 11, len (@ strSql) + 'order by stdname, [subject]'
Exec (@ strsql)

The preceding excerpted SQL code from some posts on the Internet. I saw it a long time ago. I don't know the original link.

Related Article

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.