sql server code week14

-------------------------------------------------------------------------------------------------
-- 使用数据库
use AdventureWorks
---------如何使用排名函数------- 出现在 select 后面---------
-- row_number()-- 返回有序数字
-- rank() -- 出现了并列的现象可是生成的不是有序数字
-- dense_rank() -- 不但出现了并列而且生成了有序数字
-- ntile(n) -- 给表的数据进行分组 ntile(组的个数)
--------------------  over  ------------------
-- row_number()  over(order by desc|asc)
select  * from [HumanResources].[EmployeePayHistory]
go
-- 检索 那个员工的工资最高
select  EmployeeID ,Rate 
from [HumanResources].[EmployeePayHistory]
order by Rate desc
go
-- 检索后 给这些员工进行 排名 
-- 使用排名ROW_NUMBER()函数生成了连续的数字 
select EmployeeID,rate,
ROW_NUMBER() over(order by rate desc ) as 'ROW_NUMBER_排名'
from [HumanResources].[EmployeePayHistory]
go
select EmployeeID,Rate,
rank()       over(order by rate desc ) as 'RANK_排名' -- 出现了并列的现象可是生成的不是有序数字
from [HumanResources].[EmployeePayHistory]
go
select EmployeeID,Rate,
dense_rank() over(order by rate desc ) as 'DENSE_RANK_排名'
from [HumanResources].[EmployeePayHistory]
go
--  实例:
-- 条件 : 根据销量值    where
-- 目标:销售人员
-- 特殊点:每个销售区  partition by
select * from [Sales].[SalesPerson] -- 销售部销售人员表
go
select SalesPersonID,TerritoryID,SalesQuota,SalesYTD
from [Sales].[SalesPerson] -- 销售部销售人员表
---------------------这个函数 是表中有区域 ,然后我们在区域中进行排名
-- TerritoryID 表示的是区域号  
select distinct TerritoryID from [Sales].[SalesPerson]
/*以 青岛为例
 市南区 1
 市北区 2
 崂山区 3
 城阳区 4
 即墨区 5
 平度区 6
 黄岛区 7
 .....  ...
*/
-----SalesYTD  Sales YTD  year to date 年初到目前为止 
-- 查看 每个区域的销售排名 partition by TerritoryID
-- over(partition by TerritoryID order by SalesYTD desc )
select SalesPersonID,TerritoryID,SalesYTD ,
dense_rank() over(partition by TerritoryID order by SalesYTD desc ) as N'区域的销售排名'
from [Sales].[SalesPerson]
where TerritoryID is not null
go
----- 
select * from [HumanResources].[Employee]
select EmployeeID,BirthDate from [HumanResources].[Employee]
---这个表中没有区域,我们使用ntile(组的个数) 函数 按照特定条件 ,进行分组
select EmployeeID,BirthDate,HireDate ,
ntile(4) over(order by BirthDate) as 'ntile'
from [HumanResources].[Employee]
where datepart(mm,HireDate)>=4 and datepart(yy,HireDate) >=2001
go

select EmployeeID,BirthDate,HireDate ,
ntile(6) over(order by BirthDate) as 'ntile'
from [HumanResources].[Employee]
where datepart(mm,HireDate)>=4 and datepart(yy,HireDate) >=2001

go
select datepart(mm,HireDate) from [HumanResources].[Employee]
select datepart(yy,HireDate) from [HumanResources].[Employee]
-------------------------------------------------------------------
create table Emp_SalesData(
 [year] int ,
 [month] int,
 productId varchar(20) ,
 amount int
)
/*
 P1: 苹果手机
 P2:华为手机
 P3:8848太空手机
*/
insert into Emp_SalesData values(2012,2,'P1',20000)
,(2012,2,'P1',20000)
,(2012,2,'P2',12000)
,(2012,3,'P1',15000)
,(2012,3,'P2',8000)
,(2012,4,'P1',12000)
,(2012,4,'P2',14000)
,(2012,5,'P3',14000)
go
select * from Emp_SalesData  
go
-- 聚合函数 在数据库中 经常与分组函数 在一起 group by 
-- 同一个表中出现了 不同的月份
-- 1 我想 查每一个月份的销售总额
select [month] ,sum(amount) as N'统计的每月销售总量' 
from Emp_SalesData 
group by [month] -- 分组查询
-- 2 想要看看 哪个月份销售的最高
select productId, avg(amount) -- 聚合函数不会随意的和无关列搭配
from Emp_SalesData 
where productId like 'p%' -- where 在 group by 关键字前 作为 构成分组查询的条件出现
group by productId  -- 是按照商品编号 进行分组
having avg(amount) <40000  -- 分组查询时 注意 使用的条件查询关键字时 having
order by avg(amount) -- order by 可以和聚合函数一起使用
go
select productId,sum(amount) as N'统计的每件商品的销售总量'
from Emp_SalesData
group by productId
go
select productId,sum(amount) as N'Tatol'
from Emp_SalesData
group by productId
go

-- 分析函数和排名函数都属于逻辑函数 
-- 分析函数可以和聚合函数相配合  主要分析数据
--  分析函数 也 需要 over子句的配合
-- 3 分析函数 下月的销售量
select [month] ,sum(amount) as 'total',
lead(sum(amount),3) over (order by [month]) as N'下月的销售量'
from Emp_SalesData
group by [month]
order by [month]
-- 4 分析函数 上月的销售量
select [month] ,sum(amount) as 'total',
lag(sum(amount),2) over (order by [month]) as N'上月的销售量'
from Emp_SalesData
group by [month]
order by [month]
-- 5  目前 返回有序记录集中的第一个值 
--原表:select * from Emp_SalesData  
-- 检索 一年中的第一个月
select distinct  productId ,
first_value([month]) over(partition by productId order by [month] )
as N'一年中的第一个月'
from Emp_SalesData
go
-- 6 检索 一年中的最近一个月
select distinct  productId ,
last_value([month]) 
over(partition by productId order by [month] rows between current row
and unbounded following)
as N'一年中的最近一个月'
from Emp_SalesData
go
--原表:select * from Emp_SalesData  
-- 系统函数
select host_id()
select host_name()
select SUSER_SID('sa')
select DB_NAME(1)
select DB_ID('master')
select user_name(1) as UserName
select cast([NationalIDNumber] as char(20)) as IDNUMBER
from [HumanResources].[Employee]
go
select [NationalIDNumber] from [HumanResources].[Employee]

### 如何使用InfluxDB管理时间序列数据 #### 安装与配置InfluxDB 为了有效地利用InfluxDB来处理时间序列数据,安装过程至关重要。通常情况下,在官方文档中可以找到详细的安装指南[^2]。对于大多数Linux发行版而言,可以通过包管理器轻松完成安装;而对于Windows用户,则建议下载并运行可执行文件。 #### 数据写入方式 向InfluxDB写入数据主要通过两种途径实现:HTTP API 和 Telegraf插件。其中最常用的是基于Line Protocol协议的HTTP POST请求方法。下面是一个简单的Python脚本例子用于发送POST请求给InfluxDB: ```python import requests from datetime import datetime def write_data_to_influxdb(host='localhost', port=8086, db_name="test", measurement="cpu_load_short"): url = f"http://{host}:{port}/write?db={db_name}" current_time = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ') body = [ f"{measurement},host=server01 value=0.64 {int(datetime.timestamp(current_time)*1e9)}" ] response = requests.post(url=url, data='\n'.join(body)) return response.status_code == 204 ``` 此函数`write_data_to_influxdb()`接受几个参数作为输入,并构建了一个符合Line Protocol格式的数据字符串,最后将其提交到指定数据库中。 #### 查询功能介绍 查询语句采用SQL风格编写,但具有针对时间戳优化的特点。例如要获取过去一天内CPU负载超过75%的所有记录,可以用如下命令: ```sql SELECT * FROM cpu WHERE time >= now() - 1d AND "value" > 0.75; ``` 这使得开发者能够方便快捷地检索所需的时间范围内的特定条件下的历史数据。 #### 高效存储策略设定 考虑到长时间保存大量细粒度的时间序列可能会占用过多磁盘空间,因此合理规划保留政策(Retention Policy)非常重要。创建一个新的RP只需一条简单指令即可完成: ```bash CREATE RETENTION POLICY "one_week_only" ON "mydb" DURATION 7d REPLICATION 1 DEFAULT ``` 上述命令将在名为`mydb`的数据库上建立一个为期一周的数据保持期限,并设为默认选项。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值