单号 付款金额 付款日期
-------------------- --------------------- -----------------------
107883 1000.00 2010-09-10 15:40:59.250
107883 1000.00 2010-09-11 15:47:47.000
107883 118.69 2010-09-12 13:47:47.000我的是这样的
单号 付款金额 付款日期 付款金额 付款日期 付款金额 付款日期
107883 1000.00 2010-09-10 15:40:59.250 1000.00 2010-09-11 15:47:47.000 118.69 2010-09-12 13:47:47.000
-------------------- --------------------- -----------------------
107883 1000.00 2010-09-10 15:40:59.250
107883 1000.00 2010-09-11 15:47:47.000
107883 118.69 2010-09-12 13:47:47.000我的是这样的
单号 付款金额 付款日期 付款金额 付款日期 付款金额 付款日期
107883 1000.00 2010-09-10 15:40:59.250 1000.00 2010-09-11 15:47:47.000 118.69 2010-09-12 13:47:47.000
解决方案 »
- 请教,我现在的机器上已经装了SQL Express,想要保持现在的设定,升级到SQL2005,怎样做最简便?
- 对一个表的4个字段同时检索多个关键词的算法
- 这样的连接该怎么写呢?
- MSSQL 字符转换问题 难~
- 邹健邹大哥在吗 sql server2005评估版到期问题
- SQL查询语句
- 一个备注型的字段,怎么与一个字符串相加再重新赋值给自己啊
- SQL 语句的优化问题,想了n天没结果,有兴趣的请进
- SQL Server 2005 Enterprise Edition安装。。。。。。
- 导入数据同步问题
- sql server 2008 如何清除日志?
- 一条update语句中更新多个字段,但是字段之前有计算关系,同时更新时注意什么呢?
(
select row_number() over(partition by 单号 order by getdate()) id,*
from tb
)select 单号,max(case when id = 1 then 付款金额 else '' end) 付款金额,
max(case when id = 1 then 付款日期 else '' end) 付款日期,
max(case when id = 2 then 付款金额 else '' end) 付款金额1,
max(case when id = 2 then 付款日期 else '' end) 付款日期1,
max(case when id = 3 then 付款金额 else '' end) 付款金额2,
max(case when id = 3 then 付款日期 else '' end) 付款日期2
from cte
group by 单号
select 单号,
max(case px when 1 then 付款金额 end) 付款金额1,
max(case px when 1 then 付款日期 end) 付款日期1,
max(case px when 2 then 付款金额 end) 付款金额2,
max(case px when 2 then 付款日期 end) 付款日期2,
max(case px when 3 then 付款金额 end) 付款金额3,
max(case px when 3 then 付款日期 end) 付款日期3
from
(
select t.* , px = (select count(1) from tb where 单号 = t.单号 and 付款日期 < t.付款日期) + 1 from tb t
) m
group by 单号--sql 2005
select 单号,
max(case px when 1 then 付款金额 end) 付款金额1,
max(case px when 1 then 付款日期 end) 付款日期1,
max(case px when 2 then 付款金额 end) 付款金额2,
max(case px when 2 then 付款日期 end) 付款日期2,
max(case px when 3 then 付款金额 end) 付款金额3,
max(case px when 3 then 付款日期 end) 付款日期3
from
(
select t.* , px = row_number() over(partition by 单号 order by 付款日期) from tb t
) m
group by 单号
;with cte as
(
select row_number() over(partition by 单号 order by getdate()) id,*
from tb
)select 单号,max(case when id = 1 then 付款金额 else 0 end) 付款金额,
max(case when id = 1 then 付款日期 else 0 end) 付款日期,
max(case when id = 2 then 付款金额 else 0 end) 付款金额1,
max(case when id = 2 then 付款日期 else 0 end) 付款日期1,
max(case when id = 3 then 付款金额 else 0 end) 付款金额2,
max(case when id = 3 then 付款日期 else 0 end) 付款日期2
from cte
group by 单号