按月份显示数据的值 查询某月的值

WoCao_CSD_N 2012-01-30 03:50:35
表 DSF

id Dtime VAL

1 2012-1-2 2.06

2 2012-1-2 3.00
.....

3 2012-1-31 1.00

要得到的结果是

M_Day val
1 0

2 5.06

3 0
....
31 1
...全文
148 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
WoCao_CSD_N 2012-01-30
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
引用 3 楼 wocao_csd_n 的回复:
引用 1 楼 maco_wang 的回复:
SQL code

declare @DSF table (id int,Dtime datetime,VAL numeric(3,2))
insert into @DSF
select 1,'2012-1-2',2.06 union all
select 2,'2012-1-2',3.00 ……
[/Quote]牛X
紫竹林畔 2012-01-30
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wocao_csd_n 的回复:]
引用 1 楼 maco_wang 的回复:
SQL code

declare @DSF table (id int,Dtime datetime,VAL numeric(3,2))
insert into @DSF
select 1,'2012-1-2',2.06 union all
select 2,'2012-1-2',3.00 union all
select 3,'2012……
[/Quote]
接的条件放到 on后面
不要放到where后面
看看
WoCao_CSD_N 2012-01-30
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 maco_wang 的回复:]
SQL code

declare @DSF table (id int,Dtime datetime,VAL numeric(3,2))
insert into @DSF
select 1,'2012-1-2',2.06 union all
select 2,'2012-1-2',3.00 union all
select 3,'2012-1-31',1.00

select b……
[/Quote]为什么我在后面借个条件他就只能查询到有数据的呢 没有的就不显示了
紫竹林畔 2012-01-30
  • 打赏
  • 举报
回复
递归个日期辅助表
然后left join下
叶子 2012-01-30
  • 打赏
  • 举报
回复

declare @DSF table (id int,Dtime datetime,VAL numeric(3,2))
insert into @DSF
select 1,'2012-1-2',2.06 union all
select 2,'2012-1-2',3.00 union all
select 3,'2012-1-31',1.00

select b.number,sum(isnull(a.VAL,0)) as VAL
from @DSF a right join master..spt_values b on day(a.Dtime)=b.number
where b.type='p' and b.number between 1 and 31 group by b.number
/*
number VAL
----------- ---------------------------------------
1 0.00
2 5.06
3 0.00
4 0.00
5 0.00
6 0.00
7 0.00
8 0.00
9 0.00
10 0.00
11 0.00
12 0.00
13 0.00
14 0.00
15 0.00
16 0.00
17 0.00
18 0.00
19 0.00
20 0.00
21 0.00
22 0.00
23 0.00
24 0.00
25 0.00
26 0.00
27 0.00
28 0.00
29 0.00
30 0.00
31 1.00

(31 row(s) affected)
*/

34,591

社区成员

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

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