select sum(收入) 合计,周,开始日期,结束日期 from
(
select 日期,收入,datepart(wk,日期) 周,
(select dateadd(day,-datepart(weekday,日期)+1,日期)) 开始日期,
(select dateadd(day,7-datepart(weekday,日期),日期)) 结束日期
from table1
) b
group by 周,开始日期,结束日期
select from ShopOrder where datediff(week,ordTime,getdate()-1)=0 //查询当天日期在一周年的数据
select from ShopOrder where datediff(day,ordTime,getdate()-1)=0 //查询当天的所有数据
--查询当天:
select from info where DateDiff(dd,datetime,getdate())=0
--查询24小时内的:
select from info where DateDiff(hh,datetime,getDate())<=24
--info为表名,datetime为数据库中的字段值
--查询当天:
select from info where DateDiff(dd,datetime,getdate())=0
--查询24小时内的:
select from info where DateDiff(hh,datetime,getDate())<=24
--info为表名,datetime为数据库中的字段值
楼上的只统计了一个月的吧
WITH t AS(SELECT ROWNUM
FROM DUAL
CONNECT BY ROWNUM <= 12)
SELECT TO_CHAR (LAST_DAY (TO_DATE ( TO_CHAR (SYSDATE, 'YYYY')
|| LPAD (TO_CHAR (ROWNUM), 2, 0),
'YYYYMM'
)
),
'YYYY-MM'
)
|| '月',
TO_CHAR (LAST_DAY (TO_DATE ( TO_CHAR (SYSDATE, 'YYYY')
|| LPAD (TO_CHAR (ROWNUM), 2, 0),
'YYYYMM'
)
),
'DD'
)
|| '天'
FROM t
直接全选复制粘贴运行
DECLARE @year int,@month int,@tempdata datetime,@sumday moneySELECT @year=2015,@month=11--先拼接成参数月份的第一天SELECT @tempdata=convert(varchar(4),@year)+'-'+convert(varchar(2),@month)+'-01'--统计参数月份总天数SELECT @sumday=datediff(day,@tempdata,dateadd(month,1,@tempdata))--计算参数月份一共几周SEELCT ceiling((@sumday-(case when datename(weekday,@tempdata)='星期一' then 70 when datename(weekday,@tempdata)='星期二' then 60 when datename(weekday,@tempdata)='星期三' then 50 when datename(weekday,@tempdata)='星期四' then 40 when datename(weekday,@tempdata)='星期五' then 30 when datename(weekday,@tempdata)='星期六' then 20 else 10 end))/70)+1
SELECT FROM (--需要排序则要加本层B
SELECT 客户分类,SUM(总数) AS 总数,SUM(今日) AS 今日,SUM(本周) AS 本周,SUM(本月) AS 本月 FROM (
SELECT ouUserLevel AS 客户分类,COUNT() AS 总数,0 as 今日,0 as 本周,0 as 本月 FROM OK_User AS ou
GROUP BY ouUserLevel
UNION ALL
SELECT ouUserLevel AS 客户分类,0 AS 总数,COUNT() as 今日,0 as 本周,0 as 本月 FROM OK_User AS ou
WHERE ouUserTrueDate BETWEEN DATEADD(dd, DATEDIFF(dd,0,getdate()), 0) AND GETDATE()
GROUP BY ouUserLevel
UNION ALL
SELECT ouUserLevel AS 客户分类,0 AS 总数,0 as 今日,COUNT() as 本周,0 as 本月 FROM OK_User AS ou
WHERE ouUserTrueDate BETWEEN DATEADD(wk, DATEDIFF(wk,0,getdate()), 0) AND GETDATE()
GROUP BY ouUserLevel
UNION ALL
SELECT ouUserLevel AS 客户分类,0 AS 总数,0 as 今日,0 as 本周,COUNT() as 本月 FROM OK_User AS ou
WHERE ouUserTrueDate BETWEEN CONVERT(datetime,CONVERT(char(8),GETDATE(),120)+'1') AND GETDATE()
GROUP BY ouUserLevel
) A
GROUP BY A客户分类
) B
ORDER BY B[总数]--自行替换 B[今日]……
主要原因是因为你时间字段是字符串
所以要进行几个转换
给你写了下
select from 表名 where datediff(week,cast(substring(时间,1,charindex(' 星',(时间))-1) as datetime),getdate()) =0直接复制,运行,表名换一下就行
其实你可以这样
1选一个特定时间(一般是周一凌晨,这个不重要,以为统计之前,大家的score变化时同等机会的,),从score所在的表中select出所有好友的分数,外按score从大到小排。
2讲上面选出的内容,按顺序插到另一个表中(关系到你的排名)。比如TempScoreLastWeek[ID,好友ID,统计时间]
3这样。。你就可以为所欲为了,比如什么上周排名了。上上周排名了。你还能加sorce到TempScoreLastWeek表中,你按年排名都没问题。
如果只要上周,那么在统计之前,清空TempScoreLastWeek就行了
不知道你是什么库,,,
给出sqlsever的方法
SELECT CONVERT(VARCHAR(10),time,120),COUNT(1)FROM dbotest
GROUP BY CONVERT(VARCHAR(10),time,120)
以上就是关于SQL 按周统计销售全部的内容,包括:SQL 按周统计销售、sql 语句怎么查询一周的数据、oracle数据库中怎么查询当前年每月的天数,按自然月统计,谢谢!等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)