我可以使用不带括号的 PIVOT 吗?

发布于 2024-11-29 02:41:09 字数 434 浏览 5 评论 0原文

PIVOT 
(
    count(DueCount) FOR dueLibraries.s_folder IN ([Assembly Report-TUL],[Balance-TUL],[BOM-TUL],[Hydrostatic-TUL],[Inspection-TUL],[IOM Manual-TUL],[MTR-TUL],[NDT-TUL],[Performance-TUL],[Inputs - TUL],[Transmitted])
) as MonthlyTally 

我宁愿这样:

PIVOT 
(
    count(DueCount) FOR dueLibraries.s_folder IN (select * from dueLibraries)
) as MonthlyTally 

有办法做到这一点吗?

PIVOT 
(
    count(DueCount) FOR dueLibraries.s_folder IN ([Assembly Report-TUL],[Balance-TUL],[BOM-TUL],[Hydrostatic-TUL],[Inspection-TUL],[IOM Manual-TUL],[MTR-TUL],[NDT-TUL],[Performance-TUL],[Inputs - TUL],[Transmitted])
) as MonthlyTally 

I rather just have this:

PIVOT 
(
    count(DueCount) FOR dueLibraries.s_folder IN (select * from dueLibraries)
) as MonthlyTally 

Is there a way to do that?

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

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

发布评论

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

评论(1

后eg是否自 2024-12-06 02:41:09

看起来,你的问题有两个方面。

首先,第一个代码段的 IN 列表中的标识符是分隔标识符。它们必须用方括号分隔,因为它们 不遵守 Transact-SQL 中常规标识符的规则(特别是因为它们包含空格和连字符)。

您问题的第二部分是关于用掩码之类的内容替换显式列列表,以使列表动态化。现在,没有可用的语法,您唯一的选择似乎是带有 PIVOT 子句的动态查询。 这是一个示例 如何实施。

Your question is two-fold, as it seems.

First of all, the identifiers in your first snippet's IN list are delimited identifiers. They have to be delimited with square brackets because they do not obey the rules for regular identifiers in Transact-SQL (particularly, because they include spaces and hyphens).

The second part of your question is about replacing the explicit list of columns with something like a mask, to make the list dynamic. Now, there's no available syntax for that, and your only option seems to be a dynamic query with a PIVOT clause. Here's one example of how it can be implemented.

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