TSQL 问题:对多列进行分组

发布于 2024-10-15 01:52:41 字数 1159 浏览 9 评论 0原文

我有一个包含多个列的表,这些列代表操作的发生。

simpleTableExample

id  type $$$$$ value1 value2 value3 ... value15
--  ----  --   -----  -----  -----      -----
1    "I"   1    "a"    "b"    ""         ""
2    "O"   1    "a"    "d"    "f"        "z"
3    "I"   1    "d"    "b"    ""         ""
4    "O"   1    "g"    "l"    ""         ""
5    "I"   1    "z"    "g"    "a"         ""
6    "I"   1    "z"    "g"    "a"         "a"

不太确定如何表示数据,但上面应该显示它......每行,我有多个要分组的列。专注于“a”部分,我希望输出如下:

Type     Value    Count  Sum
-----    -----    -----  ---
"I"      "A"      "3"     3
"O"      "A"      "2"     1

只是不确定如何进行,甚至不确定如何对多列进行分组。如果它是一个带有序列号的“高”表,那就很容易了...

我担心的一件事...是如果我在多行中具有相同的值(它们在多个行中执行了相同的操作)次),然后我堆叠列并尝试对行求和。例如,第 6 行有 3 和 15,值为 a,我尝试 Sum() 该组的总数,我不希望 6 被计算两次(如上所示

编辑: 试图在这里澄清...我有一排有“行动”的顾客。这些操作被折叠到 value1 到 value15 中。我想获得“类型,值”组的计数。

我之前的计数是错误的...我想要的是进行过特定操作的不同客户,而不是特定操作的数量。因此,使用上面可见的表格,IA 的计数应该是 4。

最后说明代码审查我来的问题跟上。逆透视以构建唯一日期/值对的列表。

I have a table with multiple columns that represent occurrences of an action.

simpleTableExample

id  type $$$ value1 value2 value3 ... value15
--  ----  --   -----  -----  -----      -----
1    "I"   1    "a"    "b"    ""         ""
2    "O"   1    "a"    "d"    "f"        "z"
3    "I"   1    "d"    "b"    ""         ""
4    "O"   1    "g"    "l"    ""         ""
5    "I"   1    "z"    "g"    "a"         ""
6    "I"   1    "z"    "g"    "a"         "a"

not really sure how to represent the data, but the above should show it... Per row, I have multiple columns that I want to group by. Focusing on the "a" part, I'd like the output such that :

Type     Value    Count  Sum
-----    -----    -----  ---
"I"      "A"      "3"     3
"O"      "A"      "2"     1

Just not sure how to go about, or even the best way, to group by with multiple columns. If it was a "tall" table with a sequence number, it'd be a snap...

One thing I'm worried about... is if I have the same value in multiple rows (they have the same action done multiple times), and I stack the columns and try to sum rows. So for example row 6 has 3 and 15 with value of a, and I try to Sum() the totals of that group, I don't want 6 counted twice (shown above

Edit:
Trying to clarify here... I have a row of customers with "actions". The actions are collapsed into value1 thru value15. I want to get a count of "type, value" groups.

The count I had earlier was wrong... I want the distinct customers that have had a certain action, not the number of certain actions. So I-A should be a count of 4, using the above visible table.

Final NoteCode Review question for what I came up with. Unpivot to build a list of unique date/value pairs.

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

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

发布评论

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

评论(2

忆梦 2024-10-22 01:52:41

假设 SQL 2005 或更高版本,您可以使用 UNPIVOT 然后执行 GROUP BY和 COUNT

Assuming SQL 2005 or greater you can use UNPIVOT and then do your GROUP BYs and COUNTs

拥抱没勇气 2024-10-22 01:52:41

您是否可以选择将该表放入第一范式并将这些重复列分解为子表?因为,如果我们抛开 1NF 为什么好的理论,实际的现实是像这样的非标准化示例使得简单的查询变得困难。

话虽这么说,如果您坚持使用这个非标准化表,则必须编写如下代码:

select type
     , 'A' as Column2
     , sum(case when value1='A' or value2='A' or.....
               then 1 else 0 end) as theCount
  from theTable
 group by type

如果您想查询多个值,而不仅仅是“A”,则必须在查询中旋转该表,其中涉及哦大约15个工会:

select type,value,count(*) from (
    select type,value1 as value from theTable
    union all
    select type,value2 as value from theTable
    union all
    select type,value3 as value from theTable
    ...and so on...
) x
group by type,value

Do you have the option to put that table in First Normal Form and break those repeating columns out to a child table? Because, if we put aside the theory of why 1NF is good, the practical reality is that non-normalized examples like this make simple queries tough.

With that being said, if you are stuck with this non-normalized table, you have to code up something like this:

select type
     , 'A' as Column2
     , sum(case when value1='A' or value2='A' or.....
               then 1 else 0 end) as theCount
  from theTable
 group by type

If you want to query for multiple values, not just 'A', you have to pivot the table in the query, which involves oh about 15 unions:

select type,value,count(*) from (
    select type,value1 as value from theTable
    union all
    select type,value2 as value from theTable
    union all
    select type,value3 as value from theTable
    ...and so on...
) x
group by type,value
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文