SQL语句,取数据库中前五条数据,并且把5条数据转成行的形式显示出来
时间:2011-12-22
来源:互联网

SQL语句怎么写啊?
作者: buqingle 发布时间: 2011-12-22
SQL code
select torquevalue,createdate from( select row_number()over(partition by createdate order by (select 1))rn from tb )t where rn<=5
你说把5条以"行"的形式显示出来是什么意思?它本来不是行吗?
作者: qianjin036a 发布时间: 2011-12-22
SQL code
select createdate, sum(case when rn=1 then torquevalue else 0 end) as [1], sum(case when rn=2 then torquevalue else 0 end) as [2], sum(case when rn=3 then torquevalue else 0 end) as [3], sum(case when rn=4 then torquevalue else 0 end) as [4], sum(case when rn=5 then torquevalue else 0 end) as [5] from( select torquevalue,createdate,rn from( select row_number()over(partition by createdate order by (select 1))rn from tb )t where rn<=5 )t1 group by createdate
作者: qianjin036a 发布时间: 2011-12-22
;with f as ( select id=row_number()over(order by getdate()),* from tb ) select distinct b * from f a cross apply (select top 5 * from f where create=a.create order by id)b
作者: fredrickhu 发布时间: 2011-12-22
;with f as ( select id=row_number()over(order by getdate()),* from tb ) , f2 as ( select distinct b * from f a cross apply (select top 5 * from f where createdate=a.createdate order by id)b ) select max(case id when 1 then torquevalue else 0 end) as [1], max(case id when 2 then torquevalue else 0 end) as [2], max(case id when 3 then torquevalue else 0 end) as [3], max(case id when 4 then torquevalue else 0 end) as [4], max(case id when 5 then torquevalue else 0 end) as [5] from f2
作者: fredrickhu 发布时间: 2011-12-22
2005及以上:
SQL code
select torquevalue,createdate from(
select row_number()over(partition by createdate order by (select 1))rn from tb
)t where rn<=5
你说把5条以"行"的形式显示出来是什么意思?它本来不是行吗?
我想显示出来这种效果:根据图
日期 第一个值 第二个值 第三个值 第四个值 第五个值
2011-09-14 9.55 空 空 空 空
2011-09-15 9.57 9.56 9.54 9.58 9.54
2011-09-18 1 1 1 1 1
2011-09-20 9.56 9.57 9.52 9.57 9.53
作者: buqingle 发布时间: 2011-12-22
如果是说要把它们显示在一行中,则:
SQL code
select createdate,
sum(case when rn=1 then torquevalue else 0 end) as [1],
sum(case when rn=2 then torquevalue else 0 end) as [2],
sum(case when rn=3 then torquevalue……
torquevalue是nvarchar类型,提示我:
在将 nvarchar 值 '9.50' 转换成数据类型 int 时失败。
应该怎么改呢?
作者: buqingle 发布时间: 2011-12-22
;with f as ( select id=row_number()over(order by getdate()),* from tb ) , f2 as ( select distinct b * from f a cross apply (select top 5 * from f where createdate=a.createdate order by id)b ) select max(case id when 1 then torquevalue else 0.0 end) as [1], max(case id when 2 then torquevalue else 0.0 end) as [2], max(case id when 3 then torquevalue else 0.0 end) as [3], max(case id when 4 then torquevalue else 0.0 end) as [4], max(case id when 5 then torquevalue else 0.0 end) as [5] from f2
作者: fredrickhu 发布时间: 2011-12-22
SQL code
;with f as
(
select id=row_number()over(order by getdate()),* from tb
)
, f2 as
(
select
distinct b *
from
f a
cross apply
(select top 5 * from f where createdate=a.creat……
提示:将 nvarchar 转换为数据类型 numeric 时出现算术溢出错误。
作者: buqingle 发布时间: 2011-12-22
create table buqingle(x int, y date) insert into buqingle select 151,'2011-09-15' union all select 152,'2011-09-15' union all select 153,'2011-09-15' union all select 154,'2011-09-15' union all select 155,'2011-09-15' union all select 156,'2011-09-15' union all select 157,'2011-09-15' union all select 181,'2011-09-18' union all select 182,'2011-09-18' union all select 183,'2011-09-18' union all select 184,'2011-09-18' union all select 185,'2011-09-18' union all select 201,'2011-09-20' union all select 202,'2011-09-20' union all select 203,'2011-09-20' union all select 204,'2011-09-20' union all select 205,'2011-09-20' with t2 as ( select y,x,rn from (select row_number() over(partition by y order by getdate()) rn,x,y from buqingle) t where t.rn<=5) select y '日期',[1] '第一个值', [2] '第二个值',[3] '第三个值', [4] '第四个值',[5] '第五个值' from t2 pivot(sum(x) for rn IN ([1],[2],[3],[4],[5])) t3 日期 第一个值 第二个值 第三个值 第四个值 第五个值 ---------- ----------- ----------- ----------- ----------- ----------- 2011-09-15 151 152 153 154 155 2011-09-18 181 182 183 184 185 2011-09-20 201 202 203 204 205 (3 row(s) affected)
作者: ap0405140 发布时间: 2011-12-22
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28