zoukankan      html  css  js  c++  java
  • SQL 按月统计

    SQL 按月统计,DISTINCT去重 日统计

    SELECT SUM(psar_bank_pos_recon_money) AS total_amount,DATENAME(YEAR,psar_data) AS year, DATENAME(MONTH,psar_data) AS month 
           FROM PAY_SYSTEM_ACCOUNT_RECONCILIATION WHERE psar_data>=@begin_month AND psar_data<@end_month
           GROUP BY DATENAME(YEAR,psar_data),DATENAME(MONTH,psar_data) ORDER BY DATENAME(YEAR,psar_data) ASC,DATENAME(MONTH,psar_data) ASC

    注:DATENAME(YEAR,psar_data) 获取指定字段的年份,还有其他可选参数

    DISTINCT去重 日统计

    SELECT COUNT(*) AS count, COUNT(DISTINCT lci_erector_openid) AS lci_ere_count,
        COUNT(DISTINCT tc_cu_shop_id) AS shop_count,
        DATEPART(YEAR,lci_erector_code_scantime) AS lciYear,
        DATEPART(MONTH,lci_erector_code_scantime) AS lciMonth,
        DATEPART(DAY,lci_erector_code_scantime) AS lciDay
        FROM dbo.LOTTERY_CODE_INFO INNER JOIN dbo.TC_CUSTOMERS ON lci_erector_openid=tc_cu_weixin_open_id
        WHERE lci_erector_code_scantime>=@begin_date AND lci_erector_code_scantime<@end_date
        AND (tc_cu_address LIKE '%'+@address+'%' OR @address='全国')
        AND lci_type in (40,140)
        GROUP BY DATEPART(YEAR,lci_erector_code_scantime),
        DATEPART(MONTH,lci_erector_code_scantime),DATEPART(DAY,lci_erector_code_scantime)
        ORDER BY DATEPART(YEAR,lci_erector_code_scantime),
        DATEPART(MONTH,lci_erector_code_scantime),DATEPART(DAY,lci_erector_code_scantime)

     关键点:  

    COUNT(DISTINCT lci_erector_openid)
  • 相关阅读:
    (转)关于IBM小机P520的面板使用
    (转)mysql的sql_mode合理设置
    (转)Mysql技术内幕InnoDB存储引擎-事务&备份&性能调优
    杨辉三角
    异或的陷阱(转)
    通过数组看栈堆
    数组的一些知识点和插入选择冒泡排序
    运算符
    Java的变量相关
    有符号的数据表示法(原、反、补)
  • 原文地址:https://www.cnblogs.com/szfhquan/p/2920189.html
Copyright © 2011-2022 走看看