sql 查询得出去年同期得分

HUOWEI 2013-07-25 03:16:19
数据库表结构
id hotelcd Score year month
1 01 100 2012 5
2 02 100 2012 7
3 01 90 2013 6
4 02 100 2013 7



怎么计算去年同共期平均得分及同比得分? 查询条件是 2013 01-01 -2013 07-25
...全文
383 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
看破世界 2013-07-25
  • 打赏
  • 举报
回复
create table hu
(id int, hotelcd varchar(5), Score int, year varchar(6), month varchar(6))
 
insert into hu
 select 1, '01', 100, '2012', '5' union all
 select 2, '02', 100, '2012', '7' union all
 select 3, '01', 90, '2013', '6' union all
 select 4, '02', 100, '2013', '7'
 
 
select a.hotelcd,a.Score,a.[year],a.[month],
       (select b.Score from hu b
        where datediff(m,
                       cast(b.[year]+'-'+b.[month]+'-01' as datetime),
                       cast(a.[year]+'-'+a.[month]+'-01' as datetime))=12
       ) '年同共期平均得分'
 from hu a
 where cast(a.[year]+'-'+a.[month]+'-01' as datetime) between '2013-01-01' and '2013-07-25'
 
 
/*
hotelcd Score       year   month  年同共期平均得分
------- ----------- ------ ------ -----------
01      90          2013   6      NULL
02      100         2013   7      100
 
(2 row(s) affected)
*/
HUOWEI 2013-07-25
  • 打赏
  • 举报
回复
引用 2 楼 ap0405140 的回复:

create table hu
(id int, hotelcd varchar(5), Score int, year varchar(6), month varchar(6))

insert into hu
 select 1, '01', 100, '2012', '5' union all
 select 2, '02', 100, '2012', '7' union all
 select 3, '01', 90, '2013', '6' union all
 select 4, '02', 100, '2013', '7'


select a.hotelcd,a.Score,a.[year],a.[month],
       (select b.Score from hu b
        where datediff(m,
                       cast(b.[year]+'-'+b.[month]+'-01' as datetime),
                       cast(a.[year]+'-'+a.[month]+'-01' as datetime))=12
       ) '年同共期平均得分'
 from hu a
 where cast(a.[year]+'-'+a.[month]+'-01' as datetime) between '2013-01-01' and '2013-07-25'


/*
hotelcd Score       year   month  年同共期平均得分
------- ----------- ------ ------ -----------
01      90          2013   6      NULL
02      100         2013   7      100

(2 row(s) affected)
*/
谢谢,明白了
唐诗三百首 2013-07-25
  • 打赏
  • 举报
回复

create table hu
(id int, hotelcd varchar(5), Score int, year varchar(6), month varchar(6))

insert into hu
 select 1, '01', 100, '2012', '5' union all
 select 2, '02', 100, '2012', '7' union all
 select 3, '01', 90, '2013', '6' union all
 select 4, '02', 100, '2013', '7'


select a.hotelcd,a.Score,a.[year],a.[month],
       (select b.Score from hu b
        where datediff(m,
                       cast(b.[year]+'-'+b.[month]+'-01' as datetime),
                       cast(a.[year]+'-'+a.[month]+'-01' as datetime))=12
       ) '年同共期平均得分'
 from hu a
 where cast(a.[year]+'-'+a.[month]+'-01' as datetime) between '2013-01-01' and '2013-07-25'


/*
hotelcd Score       year   month  年同共期平均得分
------- ----------- ------ ------ -----------
01      90          2013   6      NULL
02      100         2013   7      100

(2 row(s) affected)
*/
哥眼神纯洁不 2013-07-25
  • 打赏
  • 举报
回复

select AVG(score),
(select AVG(score) from 表名 where year=2012 and month between 1 and 7) from 表名
where year=2013 and month between 1 and 7

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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