SQL month summary query statement

Source: Internet
Author: User
SQL month summary query statement select pencil as [month], sum (casemonth (date) when1then pencil else0end) as [January], sum (casemonth (date) when2then pencil else0end) as [February], sum (casemonth (date) when3then pencil else0end) a SQL month summary query statement

Select 'pencil 'as [month],
Sum (case month (date) when 1 then pencil else 0 end) as [January],
Sum (case month (date) when 2 then pencil else 0 end) as [February],
Sum (case month (date) when 3 then pencil else 0 end) as [March],
Sum (case month (date) when 4 then pencil else 0 end) as [April],
Sum (case month (date) when 5 then pencil else 0 end) as [May],
Sum (pencil) as total price
From table

Union all

Select 'pen 'as [month],
Sum (case month (date) when 1 then pen else 0 end) as [January],
Sum (case month (date) when 2 then pen else 0 end) as [February],
Sum (case month (date) when 3 then pen else 0 end) as [March],
Sum (case month (date) when 4 then pen else 0 end) as [April],
Sum (case month (date) when 5 then pen else 0 end) as [May],
Sum (PEN) as total price
From table


Result

The total price of a pencil from January 1, to January 1
Pencil 200 500 600 700 500 2500
Pen 200 300 250 400 300 1450
Brush set 300 400 500 300 600
Total price 700 1200 1350 1400 1400

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.