简体   繁体   English

我如何创建一个视图,其中从查询返回值定义了列名

[英]How do I create a view where column names are defined from query return values

First, my table layout: 首先,我的表布局:

tblEquippedItems: <-(table name) tblEquippedItems: <-(表名)
slotid (FK), itemid (FK), charid (FK) <-(attributes) slotid(FK),itemid(FK),charid(FK)<-(属性)

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. 插槽ID是指由itemid表示的项目所在的项目插槽。 Charid describes the character that has these items equipped. 查理德(Charid)描述了装备这些物品的角色。

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 | Zalbar | Shoulders of Penance | Pen悔之肩| 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? 动态SQL也许? I'd love to avoid that... 我希望避免这种情况...

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

What you're looking for in SQL Server 2005/2008 is a Pivot/Cross Tab. 在SQL Server 2005/2008中寻找的是“数据透视/交叉”选项卡。 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 . 编辑:对于SQL Server 2000,请参阅有关交叉表的本文 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. 尽管您将主要看到交叉表/数据透视图使用Count()或Sum()聚合,但是您也可以轻松地在文本值上使用Max()和Min()。

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. 使用VIEW,至少可以使您处于正确的轨道,这将使您在尘埃落定时使架构更牢固。

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. 坏消息:尽管您可以使用PIVOT简化SQL方面的工作,但是查询本身将需要使用所需的实际列名进行编程。

There are three ways to do this: 有三种方法可以做到这一点:

  1. Manually modify the VIEWs when you add/remove attributes (headache) 添加/删除属性(头痛)时手动修改VIEW
  2. Dynamic queries (messy, slower, can't use a VIEW, need a PROCEDURE instead) 动态查询(混乱,较慢,无法使用VIEW,而是需要PROCEDURE)
  3. Create a stored procedure that uses dynamic SQL to drop/create all views after you modify the attributes (most complex) 修改属性后,创建一个使用动态SQL删除/创建所有视图的存储过程(最复杂)

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 除此之外,您可以尝试枢轴

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. 我使用MS Excel,其中有一个查询,以表格形式提取所需的数据。 I then pivot in MS Excel to obtain a crosstabulated view with "dynamic column headers". 然后,我在MS Excel中进行透视,以获取具有“动态列标题”的交叉表视图。

Excel is far from the only choice. Excel远非唯一选择。 At the top end, you could use something like Cognos data cubes. 在高端,您可以使用Cognos数据多维数据集之类的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在查询结果中返回列名? - How do I return column names in my query results? 如何在 Postgres 中将 pivot 值从行转换为列名? - How do I pivot values from rows into column names in Postgres? 如何在SQL中查询表以产生根据条件垂直堆叠列名称的结果? - How do I query a table in SQL to produce a result where column names are stacked vertically based on a condition? 我如何 select 一个 SQL 数据集,其中第一行中的值是列名? - How do I select a SQL dataset where values in the first row are the column names? 如何使用列中的不同值创建表作为新表的列名? - How can I create a table using distinct values from a column as the column names for the new table? 如何使用另一个表的行值作为列名(动态)在SQL中创建新表? - How do I create a new table in SQL using the row values of another table as column names (dynamic)? 如何 select 列名来自 SQL 中另一个表的值? - How to select values where column names are from another table in SQL? 如何从派生日期列中创建求和值? - How do I create summed values from a derived date column? 如何从 SQL 查询的结果创建值的字典? - How do I create a dict of values from the results of an SQL query? 如何返回LINQ实体的列名 - How do I return the column names of a LINQ entity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM