SQL query statement row-column horizontal display instance resolution _MSSQL

Source: Internet
Author: User

This article has shared two examples of row-and-column horizontal display of SQL query statements for your reference, the following details

Example 1:

There is no case in Access for SQL query statement row-column horizontal display, use IIF instead

Select IIF (sex= ' 1 ', ' male ', ' female ') from TableName

Select Country, sum (case when type= ' a ' then $) as A,
sum (case when type= ' B ' then) as B, sum (case
When Type= ' C ' then $ end] as C from
table1
GROUP BY country

Example 2:

/* Question: Suppose there is a student score table (TB) as follows: Name Course score John Language 74 three mathematics 83 three physics 93 Li four languages 74 Dick Mathematics 84 Dick Physics 94 want to become (get the following result): Name language Mathematics Physics-------
---------Lee 474 84 94 374-------------------* * CREATE TABLE TB (name varchar (10), course varchar (10), score int) INSERT into TB values (' John ', ' language ', ') ' INSERT into TB values (' John ', ' Math ', ') insert into TB values (' John ', ' Physics ', ') in

SERT into TB values (' Dick ', ' language ', ') ' INSERT into TB values (' Dick ', ' math ', ') insert into TB values (' Dick ', ' physics ', ') go --sql SERVER 2000 Static SQL, refers to the course only the three courses of language, mathematics, physics. Select name as name, Max (case course when ' language ' then score else 0 end) language, Max (when ' maths ' then fraction else 0 end) Math, Max (ca SE course when ' physical ' then score else 0 end ' physical from TB Group by name--sql SERVER 2000 dynamic SQL, which refers to courses more than the three courses of language, mathematics, physics. DECLARE @sql varchar (8000) Set @sql = ' Select name ' Select @sql = @sql + ', max (case course when ' + course + ') then score else 0 end) [' + Course + '] ' from (select DISTINCT course from TB) as a set @sql = @sql + ' from TB GROUP by name ' EXEC (@sql)--sQL SERVER 2005 Static SQL.
SELECT * FROM TB a pivot (max (fractions) for course in (language, math, physics)) b--sql SERVER 2005 dynamic SQL. DECLARE @sql varchar (8000) Select @sql = IsNull (@sql + '],[', ') + course from TB GROUP BY course set @sql = ' [' + @sql + '] ' ex EC (' select * from TB ') a pivot (max (score) for course in (' + @sql + '))---------------------------------/  * Question: On the basis of the above results, add the average score, and get the following results: The name of the Chinese mathematical Physics average score--------------------------Lee 474 84 94 84.00 252 374 83 93
 83.33 250 *

The above is the entire content of this article, I hope to help you learn.

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.