SQL回炉系列(五) 为显示曲线图获取不同时间颗粒的数据

本文主要是介绍SQL回炉系列(五) 为显示曲线图获取不同时间颗粒的数据,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在项目中,需要给用户展现某栋建筑的能耗曲线,用户可以选择单个电表,也可以选择总表,可以选择按年逐月、按月逐日或按日逐时。这是典型的数据分析和计算过程。闲话少说,直接上代码:

USE [PRODMS]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GOALTER proc [dbo].[getConsumptionChartData](@DateBegin  varchar(20),@DateEnd Varchar(20),@ty varchar(2), --y代表年,m代表月,d代表日逐时,b代表区间@backid varchar(3) -- 0 代表总能耗,其它数字代表各个电表)asbeginif @ty='y' -- 按年逐月beginif(@backid='0') -- 总能耗begin               select sum(consumption) ep , sum(price) price, convert(varchar(7),getTime,120) getTime from HourCost   where convert(varchar(4),getTime,120)=convert(varchar(4),@DateBegin,120)    group by convert(varchar(7),getTime,120)order by gettimeendelse -- 单个房间的能耗beginselect sum(consumption) ep , sum(price) price, convert(varchar(7),getTime,120) getTime from HourCost   where back=@backidand convert(varchar(4),getTime,120)=convert(varchar(4),@DateBegin,120)  group by convert(varchar(7),getTime,120)order by gettimeendendif @ty='m' -- 按月逐日beginif(@backid='0') -- 总能耗begin select sum(consumption) ep , sum(price) price, convert(varchar(10),getTime,120) getTime from HourCost where convert(varchar(7),getTime,120)=convert(varchar(7),@DateBegin,120)    group by convert(varchar(10),getTime,120)order by gettime    endelse -- 单个房间的能耗beginselect sum(consumption) ep , sum(price) price, convert(varchar(10),getTime,120) getTime from HourCost   where back=@backidand convert(varchar(7),getTime,120)=convert(varchar(7),@DateBegin,120)  group by convert(varchar(10),getTime,120) order by gettime    endendif @ty='d' -- 按日逐时begin   if(@backid='0') -- 总能耗beginselect sum(consumption) ep , sum(price) price, convert(varchar(13),getTime,120)+':00:00' getTime from HourCost where convert(varchar(10),getTime,120)=convert(varchar(10),@DateBegin,120)  group by convert(varchar(13),getTime,120)order by gettime    endelse -- 单个房间的能耗beginselect back, consumption ep , convert(varchar(13),getTime,120)+':00:00' gettime, price from HourCost where back=@backidand convert(varchar(10),getTime,120)=convert(varchar(10),@DateBegin,120)    order by gettime            endend if @ty='b' -- 时间区间beginif(@backid='0') -- 总能耗begin select convert(varchar(13),getTime,120)+':00:00' gettime, sum(consumption) ep ,sum(price) price from HourCost where  convert(varchar(13),getTime,120)>=convert(varchar(13),@DateBegin,120)and convert(varchar(13),getTime,120)<=convert(varchar(13),@DateEnd,120)group by convert(varchar(13),getTime,120)   order by gettime    endelse -- 单个房间beginselect back,  gettime, consumption ep ,price from HourCost where back=@backidand convert(varchar(13),getTime,120)>=convert(varchar(13),@DateBegin,120)and convert(varchar(13),getTime,120)<=convert(varchar(13),@DateEnd,120)order by gettime    endENDend--execute getConsumptionChartData '2014-08','2014-02-15 23:00:00','m','0'--execute getConsumptionChartData '2014','2014-02-15 23:00:00','y','14'--execute getConsumptionChartData '2015-03-26','2014-02-15 23:00:00','d','7'--execute getConsumptionChartData '2015-03-20 02:00:00','2015-03-20 12:00:00','b','6'  
  • SQL回炉系列(一) 序言
  • SQL回炉系列(二) 多表联合查询和排序
  • SQL回炉系列(三) 拆分原始数据到对应的月份表中
  • SQL回炉系列(四) 删除重复数据
  • SQL回炉系列(五) 为显示曲线图获取不同时间颗粒的数据

这篇关于SQL回炉系列(五) 为显示曲线图获取不同时间颗粒的数据的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/384211

相关文章

Java使用SLF4J记录不同级别日志的示例详解

《Java使用SLF4J记录不同级别日志的示例详解》SLF4J是一个简单的日志门面,它允许在运行时选择不同的日志实现,这篇文章主要为大家详细介绍了如何使用SLF4J记录不同级别日志,感兴趣的可以了解下... 目录一、SLF4J简介二、添加依赖三、配置Logback四、记录不同级别的日志五、总结一、SLF4J

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

数据库面试必备之MySQL中的乐观锁与悲观锁

《数据库面试必备之MySQL中的乐观锁与悲观锁》:本文主要介绍数据库面试必备之MySQL中乐观锁与悲观锁的相关资料,乐观锁适用于读多写少的场景,通过版本号检查避免冲突,而悲观锁适用于写多读少且对数... 目录一、引言二、乐观锁(一)原理(二)应用场景(三)示例代码三、悲观锁(一)原理(二)应用场景(三)示例

SpringBoot集成Milvus实现数据增删改查功能

《SpringBoot集成Milvus实现数据增删改查功能》milvus支持的语言比较多,支持python,Java,Go,node等开发语言,本文主要介绍如何使用Java语言,采用springboo... 目录1、Milvus基本概念2、添加maven依赖3、配置yml文件4、创建MilvusClient

SQL表间关联查询实例详解

《SQL表间关联查询实例详解》本文主要讲解SQL语句中常用的表间关联查询方式,包括:左连接(leftjoin)、右连接(rightjoin)、全连接(fulljoin)、内连接(innerjoin)、... 目录简介样例准备左外连接右外连接全外连接内连接交叉连接自然连接简介本文主要讲解SQL语句中常用的表

SQL server配置管理器找不到如何打开它

《SQLserver配置管理器找不到如何打开它》最近遇到了SQLserver配置管理器打不开的问题,尝试在开始菜单栏搜SQLServerManager无果,于是将自己找到的方法总结分享给大家,对SQ... 目录方法一:桌面图标进入方法二:运行窗口进入方法三:查找文件路径方法四:检查 SQL Server 安

SpringMVC获取请求参数的方法

《SpringMVC获取请求参数的方法》:本文主要介绍SpringMVC获取请求参数的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下... 目录1、通过ServletAPI获取2、通过控制器方法的形参获取请求参数3、@RequestParam4、@

MySQL 中的 LIMIT 语句及基本用法

《MySQL中的LIMIT语句及基本用法》LIMIT语句用于限制查询返回的行数,常用于分页查询或取部分数据,提高查询效率,:本文主要介绍MySQL中的LIMIT语句,需要的朋友可以参考下... 目录mysql 中的 LIMIT 语句1. LIMIT 语法2. LIMIT 基本用法(1) 获取前 N 行数据(

SpringValidation数据校验之约束注解与分组校验方式

《SpringValidation数据校验之约束注解与分组校验方式》本文将深入探讨SpringValidation的核心功能,帮助开发者掌握约束注解的使用技巧和分组校验的高级应用,从而构建更加健壮和可... 目录引言一、Spring Validation基础架构1.1 jsR-380标准与Spring整合1

MySQL 分区与分库分表策略应用小结

《MySQL分区与分库分表策略应用小结》在大数据量、复杂查询和高并发的应用场景下,单一数据库往往难以满足性能和扩展性的要求,本文将详细介绍这两种策略的基本概念、实现方法及优缺点,并通过实际案例展示如... 目录mysql 分区与分库分表策略1. 数据库水平拆分的背景2. MySQL 分区策略2.1 分区概念