如何最好地通过 SQL 求和多个布尔值?

发布于 2024-07-19 04:55:59 字数 555 浏览 10 评论 0原文

我有一个表,其中包含大约 30 列表示特定属性的布尔标志。 我想将它们返回,按频率排序,作为记录集及其列名称,如下所示:

Attribute    Count
attrib9      43
attrib13     27 
attrib19     21
etc.

到目前为止,我的努力可以实现类似的目标,但我只能使用条件 SUM 获取列中的属性,如下所示:

SELECT SUM(IIF(a.attribIndex=-1,1,0)), SUM(IIF(a.attribWorkflow =-1,1,0))...

Plus ,对于所有 30 个 SUM/IIF,查询已经变得有点笨拙,并且在没有手动干预的情况下无法处理属性数量的任何更改。

属性列的前六个字符是相同的(attrib)并且在表中是唯一的,是否可以在列名称中使用通配符来选取所有适用的列?

另外,我可以旋转结果来给我一个排序的两列记录集吗?

我使用的是 Access 2003,查询最终将通过 Excel 中的 ADODB 进行。

I have a table that contains, among other things, about 30 columns of boolean flags that denote particular attributes. I'd like to return them, sorted by frequency, as a recordset along with their column names, like so:

Attribute    Count
attrib9      43
attrib13     27 
attrib19     21
etc.

My efforts thus far can achieve something similar, but I can only get the attributes in columns using conditional SUMs, like this:

SELECT SUM(IIF(a.attribIndex=-1,1,0)), SUM(IIF(a.attribWorkflow =-1,1,0))...

Plus, the query is already getting a bit unwieldy with all 30 SUM/IIFs and won't handle any changes in the number of attributes without manual intervention.

The first six characters of the attribute columns are the same (attrib) and unique in the table, is it possible to use wildcards in column names to pick up all the applicable columns?

Also, can I pivot the results to give me a sorted two-column recordset?

I'm using Access 2003 and the query will eventually be via ADODB from Excel.

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

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

发布评论

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

评论(3

蓝色星空 2024-07-26 04:56:00

在 Access 中,交叉表查询(转置数据集的传统工具)需要至少 3 个数字/日期字段才能工作。 但是,由于输出是到 Excel,您是否考虑过将数据输出到隐藏工作表,然后使用数据透视表?

In Access, Cross Tab queries (the traditional tool for transposing datasets) need at least 3 numeric/date fields to work. However since the output is to Excel, have you considered just outputting the data to a hidden sheet then using a pivot table?

往事随风而去 2024-07-26 04:55:59

这取决于数据中的任何位置是否有属性名称。 如果你这样做了,那么鸟唇的回答就可以解决问题。 然而,如果名称只是列名,那么您还有更多工作要做——而且恐怕您无法使用简单的 SQL 来完成此操作。

不可以,您不能在 SQL 中对列名称使用通配符。 您需要过程代码来执行此操作(即 Access 中的 VB 模块——如果您使用的是 SQL Server,则可以在存储过程中执行此操作)。 使用此代码构建 SQL 代码。

不会很漂亮。 我认为您需要一次执行一个属性:选择一个值为该属性名称和 count-where-True 的字符串,然后 A) 运行该字符串并将结果存储在临时表中的新行,或者 B) 在运行批处理之前将所有这些选择与它们之间的“Union”一起附加。

我的 Access VB 有点生疏了,所以我不相信自己能给你任何像可执行代码这样的东西......

This depends on whether or not you have the attribute names anywhere in data. If you do, then birdlips' answer will do the trick. However, if the names are only column names, you've got a bit more work to do--and I'm afriad you can't do it with simple SQL.

No, you can't use wildcards to column names in SQL. You'll need procedural code to do this (i.e., a VB Module in Access--you could do it within a Stored Procedure if you were on SQL Server). Use this code build the SQL code.

It won't be pretty. I think you'll need to do it one attribute at a time: select a string whose value is that attribute name and the count-where-True, then either A) run that and store the result in a new row in a scratch table, or B) append all those selects together with "Union" between them before running the batch.

My Access VB is more than a bit rusty, so I don't trust myself to give you anything like executable code....

还如梦归 2024-07-26 04:55:59

只需简单的计数和分组即可完成

  Select attribute_name
         , count(*)
      from attribute_table
    group by attribute_name

要回答您的评论,请使用分析函数:

 Select attribute_table.*
        , count(*) over(partition by attribute_name) cnt
   from attribute_table

Just a simple count and group by should do it

  Select attribute_name
         , count(*)
      from attribute_table
    group by attribute_name

To answer your comment use Analytic Functions for that:

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