Column and column conversion in SQL Server

Source: Internet
Author: User
Original table structure
Serial number Gender Department Salary
1 Male Department 800
2 Female Department B 900
3 Male Department 400
4 Female Department d 1400
5 Male Department e 1200
6 Male Department F 500
7 Female Department 300
8 Male Department d 1000
9 Female Department d 1230
10 Female Department B 2000
11 Male Department C 2000
12 Male Department B 1200

Final display

Department name Number of students Male Female Less than 800 yuan From 800 to 999 From RMB 1000 to RMB 1190 More than 1200 yuan
Department 3 2 1 2 1 0 0
Department B 3 1 2 0 1 0 2
Department c 1 1 0 0 0 0 1
Department d 3 1 2 0 0 1 2
Department e 1 1 0 0 0 0 1
Department f 1 1 0 1 0 0 0

SELECT Department name, COUNT (number) as number of people,
SUM (CASE gender WHEN 1 THEN 1 ELSE 0 END) as male,
SUM (CASE gender WHEN 2 THEN 1 ELSE 0 END) as female,
SUM (case sign (salary-800) WHEN-1 THEN 1 ELSE 0 END) as less than 800 yuan,
SUM (case sign (salary-800) * SIGN (salary-1000)/** // * use * to implement <and> Functions */
WHEN-1 THEN 1 ELSE 0 END) + (CASE salary
When 800 then 1 else 0 end) as from 800 to 999,/** // note that aliases cannot start with numbers */
Sum (case sign (salary-1000) * sign (salary-1200)
When-1 then 1 else 0 end) + (Case salary
When 1000 then 1 else 0 end) as from 1000 yuan to 1199 yuan,
Sum (case sign (salary-1200) when 1 then 1 else 0 end)
+ (Case salary when 1200 then 1 else 0 end) as more than 1200 yuan
From payroll
Group by department name

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.