SQL 查询统计(可能与函数有关) 的问题请求指点

zpcoder 2008-12-09 12:23:15

有一个按月统计的要求,我是这样写的(其中ordertime表示销售的时间)

sum(case month(OrderTime) when 1 then quantity else 0 end) AS 'Jan'
sum(case month(OrderTime) when 2 then quantity else 0 end) AS 'Feb'
……
sum(case month(OrderTime) when 12 then quantity else 0 end) AS 'Dec'

现在要求是: 当前月的时间是上月的26号到该月的25号为当前月.
即1月要统计的应是 去年12.26日到今年1.25的销售量,
2月就是今年1.26到2.25的销售量,
依些类推到今年12月就应统计11.26到12.25的销量了,
而今年月的12.26之后就是明年的了(也不用统计划到今年统计表里)。

我试着用了SQL里的函数最后没有成功(可能是我弄错了吧).
敬请各位高手指点,应如何实现才对?

另外,还有一问题我也没有成功(也是用的函数)
有一个字段fSize 里面的内容只有二种情况,第一种.为空;第二种为:x|y
如果里面的内容为空,则返回1,如果是第二种则返回x和y的乘积
...全文
102 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
子陌红尘 2008-12-09
  • 打赏
  • 举报
回复
select
case when
isnull(fSize,'')='' then null
else
cast(left(fSize,charindex('|',fSize)-1) as numeric(5,2))*cast(stuff(fSize,1,charindex('|',fSize),'') as numeric(5,2))
end
from
(select '1.5|2' as fSize) t
zpcoder 2008-12-09
  • 打赏
  • 举报
回复

怎么我测试 1.5|2 时在 szx2000 里报 : 将数据类型 varchar 转换为 numeric 时出错
在 zc_0101 的里显示:5 ?
zc_0101 2008-12-09
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 zc_0101 的回复:]
SQL code--自定义函数createfunctionf_xy(@strvarchar(3))returnsintasbegindeclare@numintiflen(@str)=0set@num=1elseset@num=cast(left(@str,1)asint)*cast(right(@str,1)asint)return@numend--查询selectdbo.f_xy(''),dbo.f_xy('3|5')--结果/*1 15*/
[/Quote]
仅限于数字是一位数的情况
zc_0101 2008-12-09
  • 打赏
  • 举报
回复

--自定义函数
create function f_xy(@str varchar(3))
returns int
as
begin
declare @num int
if len(@str)=0
set @num=1
else
set @num=cast(left(@str,1) as int)*cast(right(@str,1) as int)
return @num
end
--查询
select dbo.f_xy(''),dbo.f_xy('3|5')
--结果
/*
1 15
*/
csdyyr 2008-12-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 szx2000 的回复:]
--2.
create function haha(@fSize varchar(10))
returns decimal
as
begin
declare @result decimal
if isnull(@fSize,'')=''
set @result=1
else
begin
declare @x decimal,@y decimal
set @x=cast(left(@fSize,charindex('|',@fSize)-1) as decimal)
set @y=cast(stuff(@fSize,1,charindex('|',@fSize)+1,'') as decimal)
set @result=@x*@y
end
return @result
end
go
[/Quote]
蓉儿?
子陌红尘 2008-12-09
  • 打赏
  • 举报
回复
[Quote=引用楼主 zpcoder 的帖子:]
另外,还有一问题我也没有成功(也是用的函数)
有一个字段 fSize 里面的内容只有二种情况,第一种.为空;第二种为:x|y
如果里面的内容为空,则返回1,如果是第二种则返回x和y的乘积
[/Quote]


case when
isnull(fSize,'')='' then ''
else
left(fSize,charindex('|',fSize)-1)*stuff(fSize,1,charindex('|',fSize),'')
end
zpcoder 2008-12-09
  • 打赏
  • 举报
回复

很好,那第二个呢?

还有一问题我也没有成功(也是用的函数)
有一个字段fSize 里面的内容只有二种情况,第一种.为空;第二种为:x|y
如果里面的内容为空,则返回1,如果是第二种则返回x和y的乘积
szx2000 2008-12-09
  • 打赏
  • 举报
回复
--2.
create function haha(@fSize varchar(10))
returns decimal
as
begin
declare @result decimal
if isnull(@fSize,'')=''
set @result=1
else
begin
declare @x decimal,@y decimal
set @x=cast(left(@fSize,charindex('|',@fSize)-1) as decimal)
set @y=cast(stuff(@fSize,1,charindex('|',@fSize)+1,'') as decimal)
set @result=@x*@y
end
return @result
end
go
csdyyr 2008-12-09
  • 打赏
  • 举报
回复
学习
zc_0101 2008-12-09
  • 打赏
  • 举报
回复

--自定义函数
Create function f_getm(@date datetime)
returns int
as
begin
declare @monthid int
if day(@date)>=26
set @monthid=month(@date)+1
else
set @monthid=month(@date)
return @monthid
end

--测试
select dbo.f_getm('2008-4-26'),dbo.f_getm('2008-4-25')
--结果
/*
5 4
*/
子陌红尘 2008-12-09
  • 打赏
  • 举报
回复
Easy,对于日期大于25的时间,在原时间上加上7~28以内的天数,这样日期就自动换算到下一个自然月统计期内的任意天;然后再统计。


select
sum(case month(OrderTime) when 1 then quantity else 0 end) AS 'Jan',
sum(case month(OrderTime) when 2 then quantity else 0 end) AS 'Feb',
...
sum(case month(OrderTime) when 12 then quantity else 0 end) AS 'Dec'
from
(select (case when datepart(dd,OrderTime)>25 then dateadd(dd,8,OrderTime) else OrderTime end) as OrderTime,quantity from 表) t
group by
...
zc_0101 2008-12-09
  • 打赏
  • 举报
回复
楼主请贴数据
zc_0101 2008-12-09
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 zpcoder 的回复:]

怎么我测试  1.5|2 时在 szx2000 里报 :  将数据类型 varchar 转换为 numeric 时出错
在 zc_0101 的里显示:5 ?
[/Quote]

呵呵,这主要是你描述问题时不太具体,所以回答者各有各的想法所致,解决问题了就ok
zpcoder 2008-12-09
  • 打赏
  • 举报
回复

狂~~, 我真是佩服得五体投地。 多谢

34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧