把相同ID记录生成到同一条记录,不是通常说的合并

wuxiaoqqqq 2008-08-06 10:07:45
数据如下
id type name price
1 1 tom 41
1 2 john 21
.....
要求生成
id type name price type name price
1 1 tom 41 2 john 21
有多少个type就生成多少.
不知道能不能用sql实现,各位帮忙看看。
...全文
93 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
muzhenxing013 2008-08-06
  • 打赏
  • 举报
回复
你说的这中是交叉报表,如果把基础数据弄出来,用EXCLE的透视表处理一下就行,如果是做报表平台的话,连表头给可以加上去的嘿嘿,这是我做的交叉报表可以参考一下
select '省','客户','清华北大学子高效读教材2008','','清华北大学子高效学习法2008','',
'清华北大学子高校训练方案2008','','搜题天下2008','','现用现查2008','','学习方法博览2008',
'','中学第一课堂2008',''
union all
select '','','批销','任务','批销','任务','批销','任务','批销','任务','批销','任务','批销','任务','批销','任务'
union all
select sf,class_name ,
[清华北大学子高效读教材2008]=cast (sum(case type_name when '清华北大学子高效读教材2008' then 批销码洋 else 0 end) as varchar(100)),
任务=cast(sum(case type_name when '清华北大学子高效读教材2008' then col006 else 0 end)as varchar(100)),
[清华北大学子高效学习法2008]=cast(sum(case type_name when '清华北大学子高效学习法2008' then 批销码洋 else 0 end) as varchar(100)),
任务=cast(sum(case type_name when '清华北大学子高效学习法2008' then col006 else 0 end)as varchar(100)),
[清华北大学子高校训练方案2008]=cast(sum(case type_name when '清华北大学子高校训练方案2008' then 批销码洋 else 0 end) as varchar(100)),
任务=cast (sum(case type_name when '清华北大学子高校训练方案2008' then col006 else 0 end) as varchar(100)),
[搜题天下2008]=cast(sum(case type_name when '搜题天下2008' then 批销码洋 else 0 end) as varchar(100)),
任务=cast(sum(case type_name when '搜题天下2008' then col006 else 0 end)as varchar(100)),
[现用现查2008]=cast(sum(case type_name when '现用现查2008' then 批销码洋 else 0 end) as varchar(100)),
任务=cast(sum(case type_name when '现用现查2008' then col006 else 0 end) as varchar(100)),
[学习方法博览2008]=cast(sum(case type_name when '学习方法博览2008' then 批销码洋 else 0 end) as varchar(100)),
任务=cast(sum(case type_name when '学习方法博览2008' then col006 else 0 end) as varchar(100)),
[中学第一课堂2008]=cast(sum(case type_name when '中学第一课堂2008' then 批销码洋 else 0 end) as varchar(100)),
任务=cast(sum(case type_name when '[中学第一课堂2008]' then col006 else 0 end) as varchar(100))
from (select sf, class_name,type_name , sum(sum_v) 批销码洋,col006
as col006 from (SELECT sf=dz_province.p_name, db_clients.c_id , type_name, class_name=
(select a.c_department FROM db_clients a WHERE a.c_id=db_clients.c_id),
cross_id=db_product.inside_id, cross_name=(select b.h_name from
db_product b where b.h_id = db_product.h_id ), h_price=db_product.h_input_price,
sum_values = sum(yw_px_item.h_amount), sum_v =sum(yw_px_item.h_amount*yw_px_item.h_price)
FROM db_product,yw_px_item,yw_px,
db_clients, dz_province, dz_type WHERE
( yw_px_item.h_id = db_product.h_id ) and
( yw_px_item.px_id = yw_px.px_id ) and
( db_clients.c_id = yw_px.c_id ) and dz_type.h_type=db_product.h_type and
( yw_px.is_destroy = '0' ) and pici<>'退货'and db_clients.p_id=dz_province.p_id and
(db_clients.a_id='0001' or db_clients.a_id='0002' )
and db_product.h_name in (select* from pubs..ggg)
GROUP BY db_clients.c_id, db_product.h_id, dz_province.p_name,
db_product.h_input_price, db_product.inside_id, type_name,
db_clients.c_id, db_clients.c_id ) b full join pubs..sssss c on b.c_id=c.col002
and substring(cross_id,5,2)=c.col004
group by type_name,class_name,c_id,col006,sf

) e
group by class_name,sf
godqal1 2008-08-06
  • 打赏
  • 举报
回复
可以实现:
命令: ALTER TABLE dbfname ADD fieldname fieldtype 可以往表dbfname增加表字段fieldname

关键是要用聚合函数select出max(count(id))最多重复ID那count数.这max(count(id))就是要增加字段的次数.


cl9132008 2008-08-06
  • 打赏
  • 举报
回复
不行啊!
Liyingyue_FFS 2008-08-06
  • 打赏
  • 举报
回复
不允许的吧,首先,就是列名重复的错误
-狙击手- 2008-08-06
  • 打赏
  • 举报
回复
if not object_id('Class') is null 
drop table Class
Go
Create table Class([Student] nvarchar(2),[Course] nvarchar(2),[Score] int)
Insert Class
select N'张三',N'语文',78 union all
select N'张三',N'数学',87 union all
select N'张三',N'英语',82 union all
select N'张三',N'物理',90 union all
select N'李四',N'语文',65 union all
select N'李四',N'数学',77 union all
select N'李四',N'英语',65 union all
select N'李四',N'物理',85
Go
--2000方法:
--动态:
create proc pr_test
as
begin
declare @s nvarchar(4000)
set @s=''
Select @s=@s+','+quotename([Course])+'=max(case when [Course]='+quotename([Course],'''')+' then [Score] else 0 end)'
from Class group by[Course]
exec('select [Student]'+@s+' from Class group by [Student]')
end
go
exec pr_test
drop proc pr_test

/*

Student 数学 物理 英语 语文
------- ----------- ----------- ----------- -----------
李四 77 85 65 65
张三 87 90 82 78

(所影响的行数为 2 行)

*/
-狙击手- 2008-08-06
  • 打赏
  • 举报
回复
[Quote=引用楼主 wuxiaoqqqq 的帖子:]
数据如下
id type name price
1 1 tom 41
1 2 john 21
.....
要求生成
id type name price type name price
1 1 tom 41 2 john 21
有多少个type就生成多少.
不知道能不能用sql实现,各位帮忙看看。
[/Quote]

这种动态sql 很多,你先找找
playwarcraft 2008-08-06
  • 打赏
  • 举报
回复
其實就是行轉列
-狙击手- 2008-08-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 playwarcraft 的回复:]
CSDN出毛病了?回復不了???
[/Quote]

哦? 我试试
playwarcraft 2008-08-06
  • 打赏
  • 举报
回复
CSDN出毛病了?回復不了???
stromboy007 2008-08-06
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 muzhenxing013 的回复:]
你说的这中是交叉报表,如果把基础数据弄出来,用EXCLE的透视表处理一下就行,如果是做报表平台的话,连表头给可以加上去的嘿嘿,这是我做的交叉报表可以参考一下
select '省','客户','清华北大学子高效读教材2008','','清华北大学子高效学习法2008','',
'清华北大学子高校训练方案2008','','搜题天下2008','','现用现查2008','','学习方法博览2008',
'','中学第一课堂2008',''
union all
select '','','批销','任务…
[/Quote]牛叉

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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