如何使用 SQL Server 2008 查询获得此结果?
我有两个表:
Product -< Category (1 product can have many categories)
Product
表有三列:
ProductId, Name, Type
P1,Alpha Product,ClassA
P2,Beta Product,ClassB
Category
表有四列:
CategoryId, Name, Value, ProductId
C1, Category1, 0.5, P1
C2, Category2, 0.75, P1
C3, Category1, 0.25, P2
C4, Category2, 0.15, P2
我想要实现的是每个产品一行,每个类别作为一列和值作为一行例如:
ProductId, Category1, Category2
P1, 0.5, 0.75
P2, 0.25, 0.15
任何帮助表示赞赏
I have two tables:
Product -< Category (1 product can have many categories)
Product
table has three columns:
ProductId, Name, Type
P1,Alpha Product,ClassA
P2,Beta Product,ClassB
Category
table has four columns:
CategoryId, Name, Value, ProductId
C1, Category1, 0.5, P1
C2, Category2, 0.75, P1
C3, Category1, 0.25, P2
C4, Category2, 0.15, P2
What I want to achieve is a single row per product with each category as a column and value as a row e.g.:
ProductId, Category1, Category2
P1, 0.5, 0.75
P2, 0.25, 0.15
Any help is appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
修改为演示静态和动态方法
Modified to demonstrate both static and dynamic methods