• sqlserver按年、季度、月份、周统计订单销量


    按周统计
    SELECT TOP 10
    DATENAME(year,AddDate) 年,
    DATENAME(week,AddDate) 周,
    COUNT(1) 单量,
    SUM(total) 总金额,
    AVG(total) 客单价
    FROM orderinfo t(NOLOCK)
    WHERE AddDate BETWEEN '2013-1-1' AND '2014-1-1'
    GROUP BY DATENAME(year,AddDate),DATENAME(week,AddDate)
    ORDER BY CONVERT(INT,DATENAME(week,AddDate));

    ----------------------------------------------------------------
    --按月统计
    SELECT TOP 10
    DATENAME(year,AddDate) 年,
    DATENAME(month,AddDate) 月,
    COUNT(1) 单量,
    SUM(total) 总金额,
    AVG(total) 客单价
    FROM orderinfo t(NOLOCK)
    WHERE AddDate BETWEEN '2013-1-1' AND '2014-1-1'
    GROUP BY DATENAME(year,AddDate),DATENAME(month,AddDate)
    ORDER BY CONVERT(INT,DATENAME(month,AddDate));

    ----------------------------------------------------------------
    --按季度统计
    SELECT TOP 10
    DATENAME(year,AddDate) 年,
    DATENAME(quarter,AddDate) 季度,
    COUNT(1) 单量,
    SUM(total) 总金额,
    AVG(total) 客单价
    FROM orderinfo t(NOLOCK)
    WHERE AddDate BETWEEN '2013-1-1' AND '2014-1-1'
    GROUP BY DATENAME(year,AddDate),DATENAME(quarter,AddDate)
    ORDER BY CONVERT(INT,DATENAME(quarter,AddDate));

    ----------------------------------------------------------------
    --按年统计
    SELECT TOP 10
    DATENAME(year,AddDate) 年,
    COUNT(1) 单量,
    SUM(total) 总金额,
    AVG(total) 客单价
    FROM orderinfo t(NOLOCK)
    WHERE AddDate BETWEEN '2012-1-1' AND '2014-1-1'
    GROUP BY DATENAME(year,AddDate)
    ORDER BY CONVERT(INT,DATENAME(year,AddDate));

    转自:https://www.etuan.com/zx/116-151613.html

  • 相关阅读:
    遇见SQL(2)
    遇见SQL(1)
    JAVA自学笔记(10)—Stream流、方法引用
    JAVA自学笔记(9)——网络通信、函数式接口
    JAVA自学笔记(7)—文件
    JAVA自学笔记(6)—异常、线程、函数式编程
    Python--模块Module
    Python--软件目录结构
    Python--迭代器
    Python--生成器
  • 原文地址:https://www.cnblogs.com/azhqiang/p/15123961.html
Copyright © 2020-2023  润新知