One SQL Problem

Source: Internet
Author: User

F1 f2 f3
---------------------
001 01 100
002 01 200
001 02 300
002 02 400
003 01 500
004 02 600

If you want to display it in the following format, should you write an SQL statement?

F1 f2 f3
---------------------
001 01 100
001 02 300
001 total 400
002 01 200
002 02 400
002 total 600
003 01 500
003 total 500
004 02 600
004 total 600
========================================================== ========================================================
Create table test
(
[ID] bigint Identity (1, 1) primary key,
F1 varchar (50 ),
F2 varchar (50 ),
F3 int
)

Drop table test

Select * from test

Insert into test values ('001', '01', 100)
Insert into test values ('002 ', '01', 200)

Insert into test values ('001', '02', 300)
Insert into test values ('002 ', '02', 400)
Insert into test values ('003 ', '01', 500)
Insert into test values ('004 ', '02', 600)

Select f1, 'Total' as f2, sum (f3) as f3 from test group by f1
Union
Select f1, f2, f3 from test

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.