如何创建一个视图,其中列名是根据查询返回值定义的
首先,我的表格布局:
tblEquippedItems: <-(表格名称)
slotid (FK)、itemid (FK)、charid (FK) <-(属性)
所以我有这个表来描述角色所装备的物品。 Slot id 指的是物品所在的物品槽位,用 itemid 表示。 Charid 描述了装备这些物品的角色。
我想做的是以某种格式呈现数据,如下所示:
角色名称|肩部单品 |头条 |腿部项目 | ... | <- 列名称
Zalbar |苦修肩|苦修头盔 |苦修护腿| ... | <- 数据值
^- 这是我表示查询结果集的无力尝试。
我想将其表示为一个视图,这样我就不必在数据库中拥有不必要且不稳定的表。列名称必须来自查询存储项目槽名称的查找表。有没有办法以编程方式定义这样的视图?也许是动态 SQL?我很想避免这种情况...
First, my table layout:
tblEquippedItems: <-(table name)
slotid (FK), itemid (FK), charid (FK) <-(attributes)
So I have this table that describes the items a character has equipped. Slot id refers to the item slot where the item, denoted by itemid resides. Charid describes the character that has these items equipped.
What I'd like to do is present the data in some format like so:
Character Name | Shoulder Item | Head Item | Leg Item | ... | <- Column Names
Zalbar | Shoulders of Penance | Helm of Penance | Leggings of Penance | ... | <- Data values
^- This was my feeble attempt to represent a query result set.
I'd like to represent this as a view so I don't have to have an unnecessary and unstable table in the database. The column names will have to come from querying the lookup table that stores the item slot names. Is there a way to programmatically define such a view? Dynamic SQL maybe? I'd love to avoid that...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您将会受到一些关于数据库设计的批评……由于多种原因,像这样的对象属性值表被认为是“邪恶的”。
也就是说,当数据库模式需要稳定但最终属性列表不断变化时(以及当我可以忍受性能后果时),我也会时不时地使用它们。
通过使用视图,您至少走在正确的轨道上,这将允许您在尘埃落定后使架构更加坚实。
坏消息:虽然您可以使用 PIVOT 使 SQL 方面的工作更轻松,但查询本身需要使用您想要的实际列名进行编程。
有三种方法可以做到这一点:
You're going to get some flack about the database design... a object-attribute-value table like this is considered "evil" for a variety of reasons.
That said, I use them to now and then too when database schemas need to be stable but the final list of attributes is in flux (and when I can live with the performance consequences).
You're at least on the right track by using VIEWs, which will allow you to make the schema more solid as the dust settles.
The bad news: although you can use PIVOT to make your life easier on the SQL side, but the query itself will need to be programmed with the actual column names you want.
There are three ways to do this:
我认为很多自连接都是有序的
除此之外,你可以尝试一个枢轴
Lots of self joins are in order i think
Other than this, you could try a pivot
您在 SQL Server 2005/2008 中寻找的是数据透视表/交叉表。您不能由此定义视图,因为视图必须是预定义的列。不过,您可以在存储过程中执行此操作。
编辑:对于 SQL Server 2000,请查看这篇有关交叉表的文章。尽管您将主要看到交叉表/数据透视表使用 Count() 或 Sum() 聚合,但您也可以轻松地对文本值使用 Max() 和 Min()。
What you're looking for in SQL Server 2005/2008 is a Pivot/Cross Tab. You cannot define a View off of this as Views have to be predefined columns. You can do it within a Stored Procedure though.
EDIT: For SQL Server 2000, look at this article about Cross Tabs. Although you'll predominantly see Cross Tabs/Pivots use the Count() or Sum() aggregates, you can just as easily use Max() and Min() on text values.
当我需要进行交叉表或数据透视时,我通常在数据库之外进行。
我使用 MS Excel,其中包含一个查询,以表格形式提取我需要的数据。然后,我在 MS Excel 中进行旋转以获得带有“动态列标题”的交叉表视图。
Excel 远非唯一的选择。在最高端,您可以使用 Cognos 数据立方体之类的东西。
When I need to do cross-tabs or pivots, I generally do them outside the database.
I use MS Excel, with a query in it that extracts the data I need, in tabular form. I then pivot in MS Excel to obtain a crosstabulated view with "dynamic column headers".
Excel is far from the only choice. At the top end, you could use something like Cognos data cubes.