A表如下
UserName UserDate UserNum UserNum1
张三 2009-11-1 10 11
张三 2009-11-2 2 22
张三 2009-11-3 7 3
李四 2009-11-5 6 4
李四 2009-11-11 8 6
王五 2009-11-1 8 55
王五 2009-11-24 10 12
-----------------------------------------------
想实现的效果是
张三 李四 王五
1 10 11 - 8 55
2 2 22 - -
3 7 3 - -
.
.
.
30原帖:
http://topic.csdn.net/u/20091111/21/8731379e-ddea-4800-99e2-769e42c1d614.html?seed=192051405&r=61104191#r_61104191
感谢bancxc的回答解决了问题
现在想获取增加的这一列的值应该怎么做呢?
UserName UserDate UserNum UserNum1
张三 2009-11-1 10 11
张三 2009-11-2 2 22
张三 2009-11-3 7 3
李四 2009-11-5 6 4
李四 2009-11-11 8 6
王五 2009-11-1 8 55
王五 2009-11-24 10 12
-----------------------------------------------
想实现的效果是
张三 李四 王五
1 10 11 - 8 55
2 2 22 - -
3 7 3 - -
.
.
.
30原帖:
http://topic.csdn.net/u/20091111/21/8731379e-ddea-4800-99e2-769e42c1d614.html?seed=192051405&r=61104191#r_61104191
感谢bancxc的回答解决了问题
现在想获取增加的这一列的值应该怎么做呢?
比如张三一号的这行记录 我想知道UserNum UserNum1 它们的值 10 11
能够知道就OK了 我在页面显示的时候想显示张三的两个字段内容 这样子
select day(createtime) from life_unite_product --取时间字段的天值select month(createtime) from life_unite_product --取时间字段的月值select year(createtime) from life_unite_product --取时间字段的年值 select datepart(yy,createtime) from life_unite_product --取时间字段的年值select datepart(qq,createtime) from life_unite_product --取时间字段的季度值select datepart(mm,createtime) from life_unite_product --取时间字段的月值select datepart(dy,createtime) from life_unite_product --取时间字段是那年的第几天select datepart(dd,createtime) from life_unite_product --取时间字段的天值select datepart(wk,createtime) from life_unite_product --取时间字段是那年的第几个星期select datepart(dw,createtime) from life_unite_product --取时间字段是那年的那个星期的第几个工作日(工作日从星期日开算)select datepart(hh,createtime) from life_unite_product --取时间字段的小时值select datepart(mi,createtime) from life_unite_product --取时间字段的分钟值select datepart(ss,createtime) from life_unite_product --取时间字段的秒值select datepart(ms,createtime) from life_unite_product --取时间字段的毫秒值 select dateadd(yy,-1,createtime) from life_unite_product ----取时间字段(年份被减1了)select dateadd(mm,3,createtime) from life_unite_product ----取时间字段(月份被加3了)select dateadd(dd,1,createtime) from life_unite_product ----取时间字段(日被加1了) select DATEDIFF(yy,createtime,getdate()) from life_unite_product --与当前日期的年份差select DATEDIFF(mm,createtime,getdate()) from life_unite_product --与当前日期的月份差select DATEDIFF(dd,createtime,getdate()) from life_unite_product --与当前日期的日数差select DATEDIFF(mi,createtime,getdate()) from life_unite_product --与当前日期的分钟数差 select datename(yy,createtime) from life_unite_product --取时间字段的年值select datename(mm,createtime) from life_unite_product --取时间字段的月值select datename(dd,createtime) from life_unite_product --取时间字段的天值 select getdate() --取当前时间 日期函数1、day(date_expression)返回date_expression中的日期值 2、month(date_expression)返回date_expression中的月份值 3、year(date_expression)返回date_expression中的年份值 4、DATEADD()DATEADD (, , )返回指定日期date 加上指定的额外日期间隔number 产生的新日期。参数“datepart” 取值如下: 5、DATEDIFF()DATEDIFF (, , )
返回两个指定日期在datepart 方面的不同之处,即date2 超过date1的差距值,其结果值是一个带有正负号的整数值。 6、DATENAME()DATENAME (, )
以字符串的形式返回日期的指定部分此部分。由datepart 来指定。 7、DATEPART()DATEPART ( datepart , date )以整数值的形式返回日期的指定部分。此部分由datepart 来指定。DATEPART (dd, date) 等同于DAY (date)DATEPART (mm, date) 等同于MONTH (date)DATEPART (yy, date) 等同于YEAR (date) 下表列出了 datepart 选项以及 SQL Server Compact Edition 所识别的缩写:日期部分 缩写
年份 yy、yyyy
季度 qq、q
月份 mm、m
每年的某一日 dy、y
日期 dd、d
星期 wk、ww
工作日* dw
小时 hh
分钟 mi、n
秒 ss、s
毫秒 ms
8、GETDATE()
以DATETIME 的缺省格式返回系统当前的日期和时间
现在的问题是可以知道UserNum ,那怎么知道UserNum1 呢?
SELECT USERNUM,USERNUM1 FROM A WHERE USERNUM=10
你可以直接这样查呀,不过不只是一行的,因为不能唯一确定
SELECT USERNUM,USERNUM1 FROM A WHERE USERDATE='2009-11-12' AND NAME='FDAFD'
drop table A
Create table A(UserName nvarchar(10), UserDate DateTime, UserNum int)
goinsert into A select
'张三' ,'2009-11-1', 10 union all select
'张三' ,'2009-11-2', 2 union all select
'张三' ,'2009-11-3', 7 union all select
'李四' ,'2009-11-5', 6 union all select
'李四' ,'2009-11-11', 8 union all select
'王五' ,'2009-11-1', 8 union all select
'王五' ,'2009-11-24', 10
declare @str nvarchar(1000) select @str='Select datepart(dd,UserDate) as [day]'
select @str=@str+','+quotename(UserName)+'=max(case UserName When '''+UserName+''' then UserNum else null end)'
from (Select distinct UserName from A) T
select @str=@str+ ' from A Group By datepart(dd,UserDate)'select @str='
select p.number,v.*
from
(
select number from
master..spt_values where type=''p'' and number between 1 and 31
) p left join
(' + @str + ') v on p.number=[day]'--print @str
exec(@str)
/*
number day 李四 王五 张三
----------- ----------- ----------- ----------- -----------
1 1 NULL 8 10
2 2 NULL NULL 2
3 3 NULL NULL 7
4 NULL NULL NULL NULL
5 5 6 NULL NULL
6 NULL NULL NULL NULL
7 NULL NULL NULL NULL
8 NULL NULL NULL NULL
9 NULL NULL NULL NULL
10 NULL NULL NULL NULL
11 11 8 NULL NULL
12 NULL NULL NULL NULL
13 NULL NULL NULL NULL
14 NULL NULL NULL NULL
15 NULL NULL NULL NULL
16 NULL NULL NULL NULL
17 NULL NULL NULL NULL
18 NULL NULL NULL NULL
19 NULL NULL NULL NULL
20 NULL NULL NULL NULL
21 NULL NULL NULL NULL
22 NULL NULL NULL NULL
23 NULL NULL NULL NULL
24 24 NULL 10 NULL
25 NULL NULL NULL NULL
26 NULL NULL NULL NULL
27 NULL NULL NULL NULL
28 NULL NULL NULL NULL
29 NULL NULL NULL NULL
30 NULL NULL NULL NULL
31 NULL NULL NULL NULL*/
我想它那表里有日期这样我好去查询,怎么修改呢?
----------- ----------- ----------- ----------- -----------
1 1 NULL 8 10
2 2 NULL NULL 2
3 3 NULL NULL 7就是加一列2009-11-1到2009-11-30这样的日期 我好做查询
number day 李四 王五 张三
----------- ----------- ----------- ----------- -----------
2009-1-1 1 NULL 8 10
2009-1-2 2 NULL NULL 2
2009-1-3 3 NULL NULL 7
或者就新增一列日期这样的
张三 2009-11-1 10 11
张三 2009-11-2 2 22
张三 2009-11-3 7 3
李四 2009-11-5 6 4
李四 2009-11-11 8 6
王五 2009-11-1 8 55
王五 2009-11-24 10 12
-----------------------------------------------
想实现的效果是
张三 李四 王五
1 10 11 - 8 55
2 2 22 - -
3 7 3 - -
比如这样,我想知道UserNum1的值
现在我的做法是需要知道日期和名字,名字的话可以通过reader.GetName(i)循环得到,日期的话希望是2009-11-1这样的,我就查询2009-11-1张三,这样我就知道了 UserNum1的值
而declare @str nvarchar(1000) select @str='Select datepart(dd,UserDate) as [day]'
select @str=@str+','+quotename(UserName)+'=max(case UserName When '''+UserName+''' then UserNum else null end)'
from (Select distinct UserName from A) T
select @str=@str+ ' from A Group By datepart(dd,UserDate)'select @str='
select p.number,v.*
from
(
select number from
master..spt_values where type=''p'' and number between 1 and 31
) p left join
(' + @str + ') v on p.number=[day]'--print @str
exec(@str)
这个我应该怎么去查询10月份的或其他月份的呢?表名又是什么呢?
declare @str nvarchar(1000) select @str='Select UserDate as [day]'
select @str=@str+','+quotename(UserName)+'=isnull(max(case UserName When '''+UserName+''' then UserNum else null end),0)'
from (Select distinct UserName from A) T
select @str=@str+ ' from A Group By UserDate'exec(@str)
select UserDate,
day(createtime) 'day'
max(case when '张三' then UserNum end) '张三',
max(case when '李四' then UserNum end) '李四',
max(case when '王五' then UserNum end) '王五'
from t
group by UserDate
order by UserDate
declare @Date nvarchar(1000)
set @Date='2009-10'
set @where='where 1=1 and CONVERT(varchar(7), UserDate,120)='''+@Date+''''
declare @str nvarchar(1000) select @str='Select UserDate as [day]'
select @str=@str+','+quotename(UserName)+'=isnull(max(case UserName When '''+UserName+''' then UserNum else null end),0)'
from (Select distinct UserName from A) T
select @str=@str+ ' from A '+@where+' Group By UserDate'exec(@str)
set @Date='2009-10'
需要的页面效果是
张三 李四 王五
1 1 0 1 1 1 0
2 0 1 - 1 - -
3 - - 1 0 0 -
...
月底
已经实现了
张三 李四 王五
1 1 1 1
2 0 - -
3 - 1 0
...
月底这样的效果
drop table A
Create table A(UserName nvarchar(10), UserDate DateTime, UserNum int)
goinsert into A select
'张三' ,'2009-11-1', 10 union all select
'张三' ,'2009-11-2', 2 union all select
'张三' ,'2009-11-3', 7 union all select
'李四' ,'2009-11-5', 6 union all select
'李四' ,'2009-11-11', 8 union all select
'王五' ,'2009-11-1', 8 union all select
'王五' ,'2009-11-24', 10
declare @str nvarchar(1000) select @str='Select datepart(dd,UserDate) as [day]'
select @str=@str+','+quotename(UserName)+'=max(case UserName When '''+UserName+''' then UserNum else null end)'
from (Select distinct UserName from A) T
select @str=@str+ ' from A Group By datepart(dd,UserDate)'select @str='
select ''2009-11-''+LTRIM(p.number)AS NEWTIME,P.NUMBER ,V.*
from
(
select number from
master..spt_values where type=''p'' and number between 1 and 31
) p left join
(' + @str + ') v on p.number=[day]'print @str
exec(@str)
declare @Date nvarchar(1000)
set @Date='2009-10'
set @where='where 1=1 and CONVERT(varchar(7), UserDate,120)='''+@Date+''''
declare @str nvarchar(1000) select @str='Select datepart(dd,UserDate) [day],UserDate as [date] '
select @str=@str+','+quotename(UserName)+'=isnull(max(case UserName When '''+UserName+''' then UserNum else null end),0)'
from (Select distinct UserName from A) T
select @str=@str+ ' from A '+@where+' Group By UserDate'exec(@str)
set @Date='2009-10'
declare @where nvarchar(1000)
declare @str nvarchar(1000)
set @where='where 1=1 and CONVERT(varchar(7), UserDate,120)='''+@Date+''''
select @str='Select datepart(dd,UserDate) as [day]'
select @str=@str+','+quotename(UserName)+'=max(case UserName When '''+UserName+''' then UserNum else null end)'
from (Select distinct UserName from A) T
select @str=@str+ ' from A '+@where+' Group By datepart(dd,UserDate)'select @str='
select ''2009-10-''+LTRIM(p.number)AS NEWTIME,P.NUMBER ,V.*
from
(
select number from
master..spt_values where type=''p'' and number between 1 and datediff(day,getdate(),dateadd(month,1,getdate()))
) p left join
(' + @str + ') v on p.number=[day]'exec(@str)
暂时这样 呵呵 谢谢大家
我在做日期查询的时候,为什么出现
从char 数据类型到datetime 数据类型的转换导致datetime 值越界
这样的情况呢?》
在查询器里面正常
查询器里正常,为什么呢
程序里却报错呢?
drop table A
Create table A(UserName nvarchar(10), UserDate DateTime, UserNum int)
goinsert into A select
'张三' ,'2009-11-1', 10 union all select
'张三' ,'2009-11-2', 2 union all select
'张三' ,'2009-11-3', 7 union all select
'李四' ,'2009-11-5', 6 union all select
'李四' ,'2009-11-11', 8 union all select
'王五' ,'2009-11-1', 8 union all select
'王五' ,'2009-11-24', 10
declare @str nvarchar(1000) select @str='Select datepart(dd,UserDate) as [day]'
select @str=@str+','+quotename(UserName)+'=max(case UserName When '''+UserName+''' then UserNum else null end)'
from (Select distinct UserName from A) T
select @str=@str+ ' from A Group By datepart(dd,UserDate)'select @str='
select dt=convert(nvarchar(10),dateadd(day,p.number,''2009-11-1''),120),number=p.number+1,v.*
from
(
select number from
master..spt_values where type=''p'' and number between 0 and 30
) p left join
(' + @str + ') v on p.number=v.day
where datepart(month,dateadd(day,p.number,''2009-11-1''))=11'--print @str
exec(@str)
/*dt number day 李四 王五 张三
---------- ----------- ----------- ----------- ----------- -----------
2009-11-01 1 NULL NULL NULL NULL
2009-11-02 2 1 NULL 8 10
2009-11-03 3 2 NULL NULL 2
2009-11-04 4 3 NULL NULL 7
2009-11-05 5 NULL NULL NULL NULL
2009-11-06 6 5 6 NULL NULL
2009-11-07 7 NULL NULL NULL NULL
2009-11-08 8 NULL NULL NULL NULL
2009-11-09 9 NULL NULL NULL NULL
2009-11-10 10 NULL NULL NULL NULL
2009-11-11 11 NULL NULL NULL NULL
2009-11-12 12 11 8 NULL NULL
2009-11-13 13 NULL NULL NULL NULL
2009-11-14 14 NULL NULL NULL NULL
2009-11-15 15 NULL NULL NULL NULL
2009-11-16 16 NULL NULL NULL NULL
2009-11-17 17 NULL NULL NULL NULL
2009-11-18 18 NULL NULL NULL NULL
2009-11-19 19 NULL NULL NULL NULL
2009-11-20 20 NULL NULL NULL NULL
2009-11-21 21 NULL NULL NULL NULL
2009-11-22 22 NULL NULL NULL NULL
2009-11-23 23 NULL NULL NULL NULL
2009-11-24 24 NULL NULL NULL NULL
2009-11-25 25 24 NULL 10 NULL
2009-11-26 26 NULL NULL NULL NULL
2009-11-27 27 NULL NULL NULL NULL
2009-11-28 28 NULL NULL NULL NULL
2009-11-29 29 NULL NULL NULL NULL
2009-11-30 30 NULL NULL NULL NULL*/