zoukankan      html  css  js  c++  java
  • sql 时间截取 分组 获得小时数据常用的方法

           switch (buttondate)
                        {
                            case "hours":
                                where += " and DataCreateOn>DATEADD(HOUR,-24,GETDATE()) ";//" and TO_DAYS(now()) - TO_DAYS(DataCreateOn) <= 7 ";
                                break;
                            case "twodays":
                                where += " and DataCreateOn>DATEADD(HOUR,-48,GETDATE()) ";//" and TO_DAYS(now()) - TO_DAYS(DataCreateOn) <= 7 ";
                                break;
                            case "week":
                                where += " and datediff(day,datacreateon,getdate())<=7 ";//" and TO_DAYS(now()) - TO_DAYS(DataCreateOn) <= 7 ";
                                break;
                            case "month":
                                where += "and datediff(day,datacreateon,getdate())<=30 ";
                                break;
                            case "qq":
                                where += " and datediff(qq,DataCreateOn,getdate())=1 ";
                                break;
                            case "year":
                                where += " and  datediff(year,DataCreateOn,getdate()-1)=0 ";
                                break;
                        
                        }

    查询中 24小时,两天,一周,一月.季度,年

    下边是查询时间 查询字符串只保留到日例如:2020-12-12,与数据库时间字段不统一问题

      where += " and  convert(char(10),DataCreateOn,120) >=  '" + strStartDate + "' and  convert(char(10),DataCreateOn,120)<='" + strEndDate +"' ";
     string columns = "  Position,datepart(YY,DataCreateOn) as year,datepart(mm,DataCreateOn) as month,datepart(dd,DataCreateOn) as day,datepart(hh,DataCreateOn) as hour , avg(Datavalue)as Datavalue ";
                    string groupBy = " group by Position,datepart(YY,DataCreateOn),datepart(mm,DataCreateOn),datepart(dd,DataCreateOn), datepart(hh,DataCreateOn) ";
                    string orderby = " order by year,month,day,hour ";

    上边是 sql 按小时分组之后,为了显示出列字段

     select getdate()

    DataCreateOn='2020-12-05 08:34:18.843'

    四季

    当前时间季度 datediff(qq,DataCreateOn,getdate())=0  //冬季 10,11,12  月时间范围内

    上一季度datediff(qq,DataCreateOn,getdate())=1 //秋季 7,8 ,9

                  datediff(qq,DataCreateOn,getdate())=2 //夏季4,5,6,

                  datediff(qq,DataCreateOn,getdate())=3 //春季1,2,3,

  • 相关阅读:
    ajax中文乱码问题的总结
    JQuery中$.ajax()方法参数详解
    Jquery的parent和parents(找到某一特定的祖先元素)
    div节点的操作(添加,删除,替换,克隆)
    js 刷新页面
    ADO.NET 体系结构
    数据访问技术介绍
    WebForm页面数据绑定总结
    sql 智能提示
    用TTTAttributedLabel创建变化丰富的UILabel
  • 原文地址:https://www.cnblogs.com/zuochanzi/p/14080845.html
Copyright © 2011-2022 走看看