如何按月并补充没数据的月显示内容,谢谢!!!

coffee_cn 2007-05-25 05:29:24
表a
id
value
month

表a数据
id  value  month
1   2    1
2   2    2
3   3    3

希望得到的结果
id  value  month
1   2    1
2   2    2
3   3    3
null null   4
null null   5
null null   6
null null   7
null null   8
null null   9
null null   10
null null   11
null null   12
...全文
308 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
coffee_cn 2007-05-25
  • 打赏
  • 举报
回复
好拉,谢谢各位!!

bill024(咖啡熊)
的方法还是有点小问题的,就是如果月份不连续会有问题

dssw(易腾软件--开发软件的软件)
的方法已经解决了我的问题
我是在创建临时表的时候
SELECT * INTO #t FROM 表a WHERE kind=2

马上接分,就是不知道这个方法的效率怎么样,是否还有更好的办法!!
dssw 2007-05-25
  • 打赏
  • 举报
回复
再上面的基础上加一个where条件就可
coffee_cn 2007-05-25
  • 打赏
  • 举报
回复
问题变复杂了,各位!!再次感谢各位!!

表a
id
value
month
kind

表a数据
id  value  month  kind
1   2    1    1
2   2    2    1
3   3    3    2

希望得到的结果 kind=2时
id  value  month
null null   1
null null   2
3   3    3
null null   4
null null   5
null null   6
null null   7
null null   8
null null   9
null null   10
null null   11
null null   12

dssw 2007-05-25
  • 打赏
  • 举报
回复
declare @t table (id int ,value int ,month int )
insert into @t values(1,100,1)
insert into @t values(2,200,2)
insert into @t values(3,300,3)
select a.id,a.value,b.month from @t A right join
(select 1 as month
union all
select 2
union all
select 3
union all
select 4
union all
select 5
union all
select 6
union all
select 7
union all
select 8
union all
select 9
union all
select 10
union all
select 11
union all
select 12
)B on a.month =b.month
fangzhouyu 2007-05-25
  • 打赏
  • 举报
回复
建个临时表,一个字段,内容为12个月,用这个表跟表a left join 条件是=a.month
dssw 2007-05-25
  • 打赏
  • 举报
回复
--错了,更正。
select A.id,A.value,B.月份
from test A right join
(select 1 as 月份
union all
select 2
union all
select 3
union all
select 4
union all
select 5
union all
select 6
union all
select 7
union all
select 8
union all
select 9
union all
select 10
union all
select 11
union all
select 12
) B on B.月份=A.A.month
dssw 2007-05-25
  • 打赏
  • 举报
回复
select A.id,A.value,A.month
from test A right join
(select 1 as 月份
union all
select 2
union all
select 3
union all
select 4
union all
select 5
union all
select 6
union all
select 7
union all
select 8
union all
select 9
union all
select 10
union all
select 11
union all
select 12
) B on B.月份=A.A.month


coffee_cn 2007-05-25
  • 打赏
  • 举报
回复
谢谢,不知道还有更好的方法吗??
等待10分钟结贴!!!
bill024 2007-05-25
  • 打赏
  • 举报
回复
create table test(id int,value int,month int)
insert test select 1,2,1
union all select 2,2,2
union all select 3,3,3
union all select 3,3,4

declare @s varchar(1000)
declare @i int

select @i=max(month) from test
set @s='select id,value,month from test'

while (12-@i)>0
begin
select @s=@s+' union all select null,null,'+rtrim(@i)+'+1 '
set @i=@i+1
end
exec(@s)

drop table test

(所影响的行数为 4 行)

id value month
----------- ----------- -----------
1 2 1
2 2 2
3 3 3
3 3 4
NULL NULL 5
NULL NULL 6
NULL NULL 7
NULL NULL 8
NULL NULL 9
NULL NULL 10
NULL NULL 11
NULL NULL 12
coffee_cn 2007-05-25
  • 打赏
  • 举报
回复
谢谢你的帮助!!

但是你这样好象就写死了!!
表a数据的数据行是不定的,现在假定是3个月的3行数据,
也可能是n个月的n行数据!!
bill024 2007-05-25
  • 打赏
  • 举报
回复
id value month
----------- ----------- -----------
1 2 1
2 2 2
3 3 3
NULL NULL 4
NULL NULL 5
NULL NULL 6
NULL NULL 7
NULL NULL 8
NULL NULL 9
NULL NULL 10
NULL NULL 11
NULL NULL 12

(所影响的行数为 12 行)
bill024 2007-05-25
  • 打赏
  • 举报
回复
一年12个月,直接union all
bill024 2007-05-25
  • 打赏
  • 举报
回复
select id,value,month from test
union all select null,null,4
union all select null,null,5
union all select null,null,6
union all select null,null,7
union all select null,null,8
union all select null,null,9
union all select null,null,10
union all select null,null,11
union all select null,null,12

27,581

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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