zoukankan      html  css  js  c++  java
  • sql 查询每月的销售金额

    sql 数据分月统计,表中只有每天的数据,现在要求求一年中每个月的统计数据(一条sql

    SELECT
      MONTH (  那个日期的字段  ),
      SUM(  需要统计的字段, 比如销售额什么的 )
    FROM
      
    WHERE
      YEAR (  那个日期的字段  ) = 2010   -- 这里假设你要查 2010年的每月的统计。
    GROUP BY
     MONTH (  那个日期的字段  )

    2 .求每个月的记录
    例:

    Create table Counter(
    CounterID int identity(1,1)not null,
    IP varchar(20),
    AccessDateTime datetime
    ,AccessCount int)

    select * from Counter

    insert into Counter
    select '127.0.0.1',GETDATE(),1 union all
    select '127.0.0.2',GETDATE(),1 union all
    select '127.0.0.3',GETDATE(),1

    declare @Year int
    set @Year=2015
    select m as[Date],
    SUM(
    case when DATEPART(month,AccessDateTime)=m
    then AccessCount else 0 end
    ) as [Count]
    from
    Counter c,
    (
    select 1 m
    union all select 2
    union all select 3
    union all select 4
    union all select 5
    union all select 6
    union all select 7
    union all select 8
    union all select 9
    union all select 10
    union all select 11
    union all select 12
    )aa
    where @Year=YEAR(AccessDateTime)
    group by m


    ------------------------------------------------------------

    1、每年
    select year(ordertime) 年,
    sum(Total) 销售合计
    from 订单表
    group by year(ordertime)

    2、每月
    select year(ordertime) 年,
    month(ordertime) 月,
    sum(Total) 销售合计
    from 订单表
    group by year(ordertime),
    month(ordertime

    3、每日
    select year(ordertime) 年,
    month(ordertime) 月,
    day(ordertime) 日,
    sum(Total) 销售合计
    from 订单表
    group by year(ordertime),
    month(ordertime),
    day(ordertime)

    另外每日也可以这样:
    select convert(char(8),ordertime,112) dt,
    sum(Total) 销售合计
    from 订单表
    group by convert(char(8),ordertime,112)

     
  • 相关阅读:
    入门金融学(1)
    递归之八皇后
    新手Oracle安装及使用入门
    RegExp正则校验之Java及R测试
    Mysql实现行列转换
    R语言之RCurl实现文件批量下载
    Consumer clientId=consumer-1, groupId=console-consumer-950] Connection to node -1 could not be
    线程池拒绝策略
    spring较为常用注解
    idea springboot启动报SLF4J:Failed to load class “org.slf4j.impl.StaticLoggerBinder”
  • 原文地址:https://www.cnblogs.com/yangpeng-jingjing/p/5045056.html
Copyright © 2011-2022 走看看