SQL statements by day, by week, by month, by quarterly statistic amount

Source: Internet
Author: User

such as:  
table: consume_record 
Field:consume  (money type)  
date  (datetime type)  

How to write four SQL statements by day, weekly, monthly, quarterly statistics consumption totals.  
such as: January  1200  
February  3400 Yuan  
March  2800 yuan &NBSP

-  by day,
Select sum (consume), days ([date])  from consume_record where  year ([date])  =  ' 2006 '  group by day ([date])  

--by week quarter 
Select sum (Consume), Datename (Week,[date])  from consume_record where year ([date])  =  ' 2006 '  group by datename (Week,[date])  

-monthly  
Select sum ( Consume), month ([date])  from consume_record where year ([date])  =  ' 2006 '   Group by month ([date])  

-Quarterly  
Select sum (Consume), Datename (Quarter,[date])  from consume_record where year ([date])  =  ' 2006 '  group by  Datename (Quarter,[date])  

-By year
Select sum (Consume), year ([date]) from Consume_record Group by year ([date])



--Specify a date you just look at the example above and work it out, it's just an aggregate function and GROUP by

Select [Date],sum (consume) from Consume_record where [date] between ' 2006-06-01 ' and ' 2006-07-10 ' group by [date]

SQL statements by day, by week, by month, by quarterly statistic amount

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.