客户去商店买东西,每个商品都有一个Type_ID表结构如下
table1(表名)
ID(客户) type_ID(商品)
1 1
2 1
2 2
1 2
3 1
1 3
。 。
。 。
实现查询结果如下
ID type_id type_id type_ID
1 1 2 3
2 1 2
3 3
实现在每个人都买啦什么产品
table1(表名)
ID(客户) type_ID(商品)
1 1
2 1
2 2
1 2
3 1
1 3
。 。
。 。
实现查询结果如下
ID type_id type_id type_ID
1 1 2 3
2 1 2
3 3
实现在每个人都买啦什么产品
解决方案 »
- 寻求思路“论坛自动回帖”
- 生成数字,左边补0,效率问题,大家来看
- C# 编写的windows service 中访问注册表问题
- 请教一个调用类的问题
- OleDbDataAdapter 怎么更新不了数据库?
- c#的问题
- 用EF获取mysql数据时报:Unable to convert MySQL date/time value to System.DateTime
- 请问怎样根据一个文件夹创建一个包含它里面所有内容(子文件夹和文件名)的XML文件?
- 将DataGrid中显示的数据直接打印出来的问题
- Winxp+Visual Studio.net能不能装上SqlServer2000的个人版?
- 求助如何用 textbox控件接收查询出来的结果集?
- C# 中 用sql数据库时使用 SqlDbType.VarChar access中使用出错
Name Subject Result
张三 语文 80
张三 数学 90
张三 物理 85
李四 语文 85
李四 数学 92
李四 物理 82想变成
姓名 语文 数学 物理
张三 80 90 85
李四 85 92 82declare @sql varchar(4000)
set @sql = 'select Name'
select @sql = @sql + ',sum(case Subject when '''+Subject+''' then Result end) ['+Subject+']'
from (select distinct Subject from CJ) as a
select @sql = @sql+' from test group by name'
exec(@sql)
select id, sum(CTypeID3),sum(CTypeID3),sum(CTypeID3)
from
(
(select id,count(TypeID1) as CTypeID1,CTypeID2=0,CTypeID3=0
from Table1
where TypeID=1
group by id)
union
(
select id,CTypeID1=0,count(TypeID2) as CTypeID2,CTypeID3=0
from Table1
where TypeID=2
group by id
)
union
(
select id,CTypeID1=0,CTypeID2=0,count(TypeID3) as CTypeID3
from Table1
where TypeID=3
group by id
)
) as TempTT
group by id
一、表结构:
ID type_ID
u1 p1
u1 p2
u2 p3
u3 p3
u4 p4
u5 p4
u6 p4
u7 p5
u8 p5
u8 p4
u5 p3
二、SQL语句:
SELECT id
,max(case when type_id='p1' then type_id else '' end) as p1
,max(case when type_id='p2' then type_id else '' end) as p2
,max(case when type_id='p3' then type_id else '' end) as p3
,max(case when type_id='p4' then type_id else '' end) as p4
,max(case when type_id='p5' then type_id else '' end) as p5
FROM products
group by id三、结果:
id p1 p2 p3 p4 p5
----------------------------------------------
u1 p1 p2
u2 p3
u3 p3
u4 p4
u5 p3 p4
u6 p4
u7 p5
u8 p4 p5