如何创建一个视图,其中列名是根据查询返回值定义的

发布于 2024-08-14 05:00:09 字数 468 浏览 8 评论 0原文

首先,我的表格布局:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

他夏了夏天 2024-08-21 05:00:09
SELECT
    [character].name                    AS [character_name],
    ISNULL([shoulder].name,'Nothing')   AS [shoulder_item_name],
    ISNULL([head].name,'Nothing')       AS [head_item_name],
    etc...
FROM
   [character]
LEFT JOIN
   [tblEquippedItems] AS [shoulder_item]
      ON [shoulder_item].charid = [character].id
      AND [shoulder_item].slotid = 1
LEFT JOIN
   [item]             AS [shoulder]
      ON [shoulder].id = [shoulder_item].itemid
LEFT JOIN
   [tblEquippedItems] AS [head_item]
      ON [head_item].charid = [character].id
      AND [head_item].slotid = 2
LEFT JOIN
   [item]             AS [head]
      ON [head].id = [head_item].itemid

etc...
SELECT
    [character].name                    AS [character_name],
    ISNULL([shoulder].name,'Nothing')   AS [shoulder_item_name],
    ISNULL([head].name,'Nothing')       AS [head_item_name],
    etc...
FROM
   [character]
LEFT JOIN
   [tblEquippedItems] AS [shoulder_item]
      ON [shoulder_item].charid = [character].id
      AND [shoulder_item].slotid = 1
LEFT JOIN
   [item]             AS [shoulder]
      ON [shoulder].id = [shoulder_item].itemid
LEFT JOIN
   [tblEquippedItems] AS [head_item]
      ON [head_item].charid = [character].id
      AND [head_item].slotid = 2
LEFT JOIN
   [item]             AS [head]
      ON [head].id = [head_item].itemid

etc...
从此见与不见 2024-08-21 05:00:09

您将会受到一些关于数据库设计的批评……由于多种原因,像这样的对象属性值表被认为是“邪恶的”。

也就是说,当数据库模式需要稳定但最终属性列表不断变化时(以及当我可以忍受性能后果时),我也会时不时地使用它们。

通过使用视图,您至少走在正确的轨道上,这将允许您在尘埃落定后使架构更加坚实。

坏消息:虽然您可以使用 PIVOT 使 SQL 方面的工作更轻松,但查询本身需要使用您想要的实际列名进行编程。

有三种方法可以做到这一点:

  1. 添加/删除属性时手动修改 VIEW(令人头疼)
  2. 动态查询(混乱、速度较慢、无法使用 VIEW,需要 PROCEDURE)
  3. 创建一个使用动态 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:

  1. Manually modify the VIEWs when you add/remove attributes (headache)
  2. Dynamic queries (messy, slower, can't use a VIEW, need a PROCEDURE instead)
  3. Create a stored procedure that uses dynamic SQL to drop/create all views after you modify the attributes (most complex)
朦胧时间 2024-08-21 05:00:09

我认为很多自连接都是有序的

    SELECT t1.charid,t1.itemid as shoulders,t2.itemid as helm,t3.itemid as legs
FROM (SELECT charid,itemid from tblEquippedItems where slotid = 1) t1 //slotid = shuolders
INNER JOIN (SELECT itemid from tblEquippedItems where slotid = 2) t2 //slotid = Helm
ON t1.charid = t2.charid
INNER JOIN (SELECT itemid from tblEquippedItems where slotid = 3) t3 //slotid = Legs
ON t3.charid = t2.charid

除此之外,你可以尝试一个枢轴

Lots of self joins are in order i think

    SELECT t1.charid,t1.itemid as shoulders,t2.itemid as helm,t3.itemid as legs
FROM (SELECT charid,itemid from tblEquippedItems where slotid = 1) t1 //slotid = shuolders
INNER JOIN (SELECT itemid from tblEquippedItems where slotid = 2) t2 //slotid = Helm
ON t1.charid = t2.charid
INNER JOIN (SELECT itemid from tblEquippedItems where slotid = 3) t3 //slotid = Legs
ON t3.charid = t2.charid

Other than this, you could try a pivot

余生一个溪 2024-08-21 05:00:09

您在 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.

情深缘浅 2024-08-21 05:00:09

当我需要进行交叉表或数据透视时,我通常在数据库之外进行。

我使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文