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)
  • 相关阅读:
    Gitlab邮箱配置
    Zabbix邮件告警提示Couldn't resolve host name解决办法
    Gitlab备份和恢复操作
    Gitlab权限管理
    编译安装Nginx
    [0] 数据库读写分离
    [0] C# & MongoDB
    [0] 平衡二叉树
    [0] 分布式存储 Memcached
    [0] MSSQL 分库查询
  • 原文地址:https://www.cnblogs.com/szfhquan/p/2920189.html
Copyright © 2011-2022 走看看