sql问题..如何动态地将列添加到sql查询结果集中?

发布于 2024-07-17 11:51:29 字数 650 浏览 4 评论 0原文

我正在编写一份报告以返回有关数据库中对象(“文件”)的详细信息。 我的应用程序允许用户创建自己的标志以用于文件对象。 标志基本上由一个名称组成,然后标志实例存储一个位值来指示是否为父文件对象设置它。

我想编写一个查询,该查询返回数据库中每个文件的一行,其中结果集中的前几列包含文件详细信息(id、名称、大小等),其余列是标志名称,返回位值指示是否为给定文件行设置了标志。

那有意义吗? 我该如何编写查询?

谢谢你的帮助。

编辑:澄清..

作为此查询的一部分,我需要运行一个子查询,返回用户创建的标志(我在设计时不知道这些),然后对每个标志值进行检查主要查询返回有关文件的详细信息。

简化架构如下:

  • 文件 {Id,名称}
  • 标志 {Id,名称}
  • FileFlags {FileId,FlagId} - 此表中的一行表示为文件设置了标志

我需要查询返回包含某些列的结果集像这样:

FileId FileName Flag1Name Flag2Name .... FlagNName

I am writing a report to return details about an object ('files') in my database. My application lets users create their own flags for use against file objects. Flags basically consist of a name, then flag instances store a bit value to indicate whether it is set for the parent file object.

I want to write a query that returns one row per file in the database, where the first few columns in the result set contain the file details (id, name, size etc) and the remaining columns are the flag names, with bit values returned to indicate whether the flag is set for the given file row.

Does that make sense? How do i go about writing the query?

Thanks for any help.

Edit: Clarification..

As part of this query, I need to run a sub query that returns the flags that have been created by the user (I do not know these at design time), then incorporate a check for each flag value in the main query to return details about the files.

Simplified schema as follows:

  • File {Id, Name}
  • Flag {Id, Name}
  • FileFlags {FileId, FlagId} - a row in this table indicates that the flag is set for the file

I need the query to return a result set with columns something like this:

FileId FileName Flag1Name Flag2Name .... FlagNName

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

靑春怀旧 2024-07-24 11:51:29

您可以首先查看 SQL Server 2005 中提供的 Pivot 函数+。 通过这个和一些字符串连接,您应该能够为任意数量的列组装查询

根据您的评论,您的选择将如下所示:

        SELECT <non-pivoted column>,

            [first pivoted column] AS <column name>,

            [second pivoted column] AS <column name>, ...
    From Table 
    PIVOT (
...
FOR 
...
)

You could start by looking at the Pivot function available in SQL Server 2005+. With that and some string concatenation you should be able to assemble a query for any number of columns

Based on your comment your select would look something like this:

        SELECT <non-pivoted column>,

            [first pivoted column] AS <column name>,

            [second pivoted column] AS <column name>, ...
    From Table 
    PIVOT (
...
FOR 
...
)
她比我温柔 2024-07-24 11:51:29

为什么不创建一个具有所需逻辑的存储过程 - 查询用户特定的标志等 - 并在此基础上动态构建一个包含所需变量的查询字符串?

然后,您可以使用 EXECUTE 将动态查询集返回给用户。 请参阅 http://msdn.microsoft.com/en-us/library/ms188332 .aspx

Why not create a stored procedure with the required logic - querying user-specific flags, etc - and on that basis dynamically build a query string with the variables you need?

You can then use EXECUTE to return the dynamic query set to the user. See http://msdn.microsoft.com/en-us/library/ms188332.aspx

荒芜了季节 2024-07-24 11:51:29

听起来也许您需要一个交叉表查询,您希望将行转换为列。 标志是否作为与父表的一对多关系存储在单独的表中?

Sounds like perhaps you need a crosstab query, where you want to turn rows into columns. Are the flags stored in a separate table as a one to many relationship to the parent table?

晒暮凉 2024-07-24 11:51:29

我认为你想要的是一个别名。 这是 http://www.sql-tutorial.net/SQL-Aliases 的示例.asp

SELECT Employee, SUM(Hours) AS SumHoursPerEmployee
FROM EmployeeHours
GROUP BY Employee 

我真的需要看看你的架构才能进一步提供帮助。

编辑
也许您需要嵌套的SELECT
http://sqlzoo.net/1a.htm

I think you want is an alias. Here is an example from http://www.sql-tutorial.net/SQL-Aliases.asp

SELECT Employee, SUM(Hours) AS SumHoursPerEmployee
FROM EmployeeHours
GROUP BY Employee 

I would really have to see your schema to help further.

Edit
Maybe you need nested SELECTs.
http://sqlzoo.net/1a.htm

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