百分求一表

For_suzhen 2006-10-30 09:04:43

---------------------------------------------
早上 上午 中午 下午 晚上
俯卧撑 2 5 0 0 0

仰卧起 3 0 0 0 0

杠铃 4 3 0 0 0
------------------------------------------------
如上所示,这个表记录了我一天的锻炼情况,我想写入数据库该怎么建一个表呢。
如果我想统计到月份中有两个这样的表
----------------------------------------------
日子 早上 上午 中午 下午 晚上
1号 9 8 0 0 0
日子 俯卧撑 仰卧起 杠铃
1号 7 3 7
-----------------------------------------------
就这样,可是我怎么做一个表,当我的数据统计进去后,我再通过月份的表能够变回到每一天的具体情况
就是说-----------------------------------------------
---------------------------------------------------是两个画面的话,我怎么来回的转换
大哥们给个意见
...全文
188 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
For_suzhen 2006-10-31
  • 打赏
  • 举报
回复
dawugui(潇洒老乌龟) ( ) 信誉:100 Blog 2006-10-30 22:10:48 得分: 0



一个表足以.

根据不同的条件获取不同的数据,可以每天,每月或任何日期.就象我在上面给你建立的表.



///////////////////////////////////////////////////////////////
感谢你的表
gahade 2006-10-30
  • 打赏
  • 举报
回复
一个表就能满足需求的,为什么要用两个表呢?
dawugui 2006-10-30
  • 打赏
  • 举报
回复
一个表足以.

根据不同的条件获取不同的数据,可以每天,每月或任何日期.就象我在上面给你建立的表.
For_suzhen 2006-10-30
  • 打赏
  • 举报
回复
不好意思,看得不太明白
当我的数据统计进去后,我再通过月份的表能够变回到每一天的具体情况
------------------------------------------------------------------这是什么意思?

数据都可以存在一个表中,楼主你的意思是要两份统计报表么?

/////////////////////////////////////////////////////////////////////////
对,就是想让两个画面来回的转换
gahade 2006-10-30
  • 打赏
  • 举报
回复
create table anneal(type varchar(20),time varchar(10),qty int,dt datetime)
insert into anneal
select '俯卧撑','早上',2,getdate()
union all select '俯卧撑','上午',5,getdate()
union all select '俯卧撑','中午',0,getdate()
union all select '俯卧撑','下午',0,getdate()
union all select '俯卧撑','晚上',0,getdate()
union all select '仰卧起','早上',3,getdate()
union all select '仰卧起','上午',0,getdate()
union all select '仰卧起','中午',0,getdate()
union all select '仰卧起','下午',0,getdate()
union all select '仰卧起','晚上',0,getdate()
union all select '杠铃','早上',4,getdate()
union all select '杠铃','上午',3,getdate()
union all select '杠铃','中午',0,getdate()
union all select '杠铃','下午',0,getdate()
union all select '杠铃','晚上',0,getdate()

--1.
declare @sql varchar(8000)
set @sql=''
select @sql=@sql+',sum(case when time ='''+time+''' then qty else 0 end) as '+time
from (select distinct time from anneal)t
exec('select type '+@sql+' from anneal group by type')

--2.
declare @sql varchar(8000)
set @sql=''
select @sql=@sql+',sum(case when time ='''+time+''' then qty else 0 end) as '+time
from (select distinct time from anneal)t
exec('select convert(char(10),dt,120) '+@sql+' from anneal group by convert(char(10),dt,120)')

--3.
declare @sql varchar(8000)
set @sql=''
select @sql=@sql+',sum(case when type ='''+type+''' then qty else 0 end) as '+type
from (select distinct type from anneal)t
exec('select convert(char(10),dt,120) '+@sql+' from anneal group by convert(char(10),dt,120)')

drop table anneal
dawugui 2006-10-30
  • 打赏
  • 举报
回复
查某个月的数据
select 名称,sum(早上),sum(上午),sum(中午),sum(下午),sum(晚上) from t where 日期>='2006-10-01' and 日期<='2006-10-31' group by 名称
dawugui 2006-10-30
  • 打赏
  • 举报
回复
表结构
(
名称 varchar(20)
早上 int
上午 int
中午 int
下午 int
晚上 int
日期 datatime
)

数据
名称 早上 上午 中午 下午 晚上 日期
俯卧撑 2 5 0 0 0 2006-10-30
仰卧起 3 0 0 0 0 2006-10-30
杠铃 4 3 0 0 0 2006-10-30
俯卧撑 2 5 0 0 0 2006-10-31
仰卧起 3 0 0 0 0 2006-10-31
杠铃 4 3 0 0 0 2006-10-31

查某天的数据
select * from tb where 日期 = '2006-10-30'
查某个月的数据
select 名称,sum(早上),sum(上午),sum(中午),sum(下午),sum(晚上) from t where 日期>='2006-10-01' and 日期<='2006-10-31'


lovcal 2006-10-30
  • 打赏
  • 举报
回复
不好意思,看得不太明白
当我的数据统计进去后,我再通过月份的表能够变回到每一天的具体情况
------------------------------------------------------------------这是什么意思?

数据都可以存在一个表中,楼主你的意思是要两份统计报表么?

34,593

社区成员

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

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