当前位置:首页 » 编程语言 » sql每日一句
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

sql每日一句

发布时间: 2022-07-03 19:14:25

sql语句统计每天、每月、每年的销售总额

1、每年
select
year(ordertime)
年,
sum(Total)
销售合计
from
订单表
group
by
year(ordertime)
2、每月
select
year(ordertime)
年,
month(ordertime)
月,
sum(Total)
销售合计
from
订单表
group
by
year(ordertime),
month(ordertime
3、每日
select
year(ordertime)
年,
month(ordertime)
月,
day(ordertime)
日,
sum(Total)
销售合计
from
订单表
group
by
year(ordertime),
month(ordertime),
day(ordertime)
另外每日也可以这样:
select
convert(char(8),ordertime,112)
dt,
sum(Total)
销售合计
from
订单表
group
by
convert(char(8),ordertime,112)
如果需要增加查询条件,在from后加where
即可。

② SQL 统计每日上班打卡和下班打卡语句

这是在oracle数据库下做的,不知道你是什么数据库,给你提供一下思路
select
card_no,
attr_date,
to_char(atte_time,'HH24:MI')tim,
doorinout,
rn
from (
select
card_no,
attr_date,
atte_time,
doorinout,
ROW_NUMBER() OVER(PARTITION BY card_no,attr_date,doorinout ORDER BY atte_time asc) rn
from(
select
card_no,
(
case
when doorinout=1
then to_date(to_char(atte_time,'yyyy-MM-dd'),'yyyy-MM-dd')
when (doorinout=0 and to_number(to_char(atte_time,'HH24'))<3)
then (to_date(to_char(atte_time,'yyyy-MM-dd'),'yyyy-MM-dd')-1)
else to_date(to_char(atte_time,'yyyy-MM-dd'),'yyyy-MM-dd')
end

)attr_date,
atte_time,
doorinout
from AtteTime
) tmp order by card_no,atte_time asc,doorinout desc
) where rn < 5

③ 如何让sql每天执行一下一个写好的sql语句

可以设置定时任务,比较常用的是用sqlserver 代理新建作业。有时候数据库自动备份也是用的这个。
还有一种比较复杂自己写个程序,使用系统定时任务 定时执行。
创建sql server定时任务作业步骤如下:
1.要先安装了sqlserver 代理服务
--2.鼠标右击【SQL Server 代理】,选择【启动(S)】,如已启动,可以省略此步骤;
--3.展开【SQL Server 代理】列表,右击【作业】--【新建作业】;
--3.1 在【常规】选项卡中:
-- 输入作业名称,如My Job;
--3.2 在【步骤】选项卡中:
--3.2.1 点击【新建】,输入【步骤名称】,如步骤1,类型默认T-SQL脚本,也可以选择SSIS包等;
--3.2.2 在【数据库】一栏选择要作业处理的数据库,在【命令】的右边空白编辑栏输入要执行的SQL代码,
EXEC p_Name --如:执行一个P_Name的存储过程
-- 也可以点击命令下面的【打开】,打开.sql脚本;
--3.2.3 输入运行脚本后,建议点击【分析】,确保脚本语法正确,然后点击下面的【确定】按钮;
--3.3 在【计划】选项卡中:
--3.3.1 点击【新建】,输入【计划名称】,如计划1,计划类型默认是重复执行,也可以选择执行一次等;
--3.3.2 在【频率】--【执行】处选择每天、每周或每月,以每天为例,间隔时间输入间隔几天执行一次,
-- 下面还可以选择每天一次性执行或间隔一定的时间重复执行
--3.3.3 在【持续时间】中选择计划开始执行的【起始日期】和【截止日期】,然后点击【确定】按钮;
--注意要将服务设置为自动启动,否则在重启服务器后作业就不运行了。
--启动代理服务的方法:
--开始--运行--输入services.msc--找到【SQL Server 代理】的服务并双击--【启动类型】选择自动--【确定】
--至此,定时作业已创建完毕。

④ SQL语句,怎样可以实现比如每日均值的查询

SELECT
时间,
AVG(体温)
FROM

WHERE
时间>起始时间
and
时间
<截至时间
AND
姓名
in
('李四','张三')
GROUP
BY
时间
ORDER
BY
时间
其中
姓名
in
('李四','张三')
对于复杂的筛选
可以用姓名
in
(select
姓名
from

where
删选条件)
来实现

⑤ 请用一句SQL语言(注意是一句)得出上述表中实际每天的销售额笔数,退货金额笔数,

select decode(t.salemny,0,0,1),decode(t.retmny,0,0,1),t.workdate
from salerept t

⑥ 可以不可以 让sql 2008 每天自动执行一条语句

C:\Documents and Settings\Wang>osql /?
用法: osql [-U login id] [-P password]
[-S server] [-H hostname] [-E trusted connection]
[-d use database name] [-l login timeout] [-t query timeout]
[-h headers] [-s colseparator] [-w columnwidth]
[-a packetsize] [-e echo input] [-I Enable Quoted Identifiers]
[-L list servers] [-c cmdend] [-D ODBC DSN name]
[-q "cmdline query"] [-Q "cmdline query" and exit]
[-n remove numbering] [-m errorlevel]
[-r msgs to stderr] [-V severitylevel]
[-i inputfile] [-o outputfile]
[-p print statistics] [-b On error batch abort]
[-X[1] disable commands [and exit with warning]]
[-O use Old ISQL behavior disables the following]
<EOF> 批处理
控制台宽度自动调整
宽消息
默认错误级别为 — 1 对 1
[-? show syntax summary]
osql -u 用户名 -p 密码 -s 服务器 -d 数据库名 -i sql文件
把你要执行的sql语句写好保存到一个文件里面,在把上句复制到一个文本文件里面,改成bat扩展名变成批处理文件,然后系统控制面板里面有个计划任务,把批处理文件加到计划任务里面,这个比较麻烦,简单点的是sql企业管理器里面有个作业管理,在sql agent里面,启用sqlagent服务,里面的作业就可以自动按时间调度了

⑦ 怎么才能只通过一句sql查出表中最近一周每天最晚时间

不知道你的time是什么格式的,暂且就默认成date格式,然后oracle数据库
select to_char(time,'yyyymmdd'),max(time) from table group by to_char(time,'yyyymmdd');

⑧ 我想在sql2008 r2中每天晚上自动运行一段SQL语句,请问如何配置

登录进数据库里,展开节点,你可以看到任务计划之类的,自己研究下。同时要把Sql Agent服务设置为自动启动。

⑨ 求一SQL查询语句,查询每天某一时间段记录。

举例如下:
select
*
from
timetable
where
datediff(mi,'2008-10-10
8:00:00',timet)>=1
and
datediff
(mi,'2008-10-10
9:00:00',timet)<=30

⑩ 怎么使某一条sql语句每天定时执行

在linux or Unix系统中有一个叫crontag的东西。
你可以设定执行的时间,它就是每天循环执行了。
格式:(second hour date month week)
00 10 * * * /u01/app/cronjob/exe_select.sh
每天十点整执行一次