首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在多列上对cfquery进行分组?

问如何在多列上对cfquery进行分组?
EN

Stack Overflow用户
提问于 2012-10-18 09:24:15
回答 2查看 6.6K关注 0票数 4

我需要在多个列匹配的地方对查询进行分组。例如,对日期、类别和说明匹配的所有行进行分组。我知道如何在对一列进行分组时使用cfoutput分组,例如:

代码语言:javascript
复制
<cfoutput query="myQry" group="date">
  #date#
  <cfoutput>
    #detail#
  </cfoutput>
</cfoutput>

但是,我想对多个列匹配的地方进行分组,如下所示:

代码语言:javascript
复制
<cfoutput query="myQry" group="date,category,description">
  #date# #category# #description#
  <cfoutput>
    #detail#
  </cfoutput>
</cfoutput>

我知道cfoutput分组不像上面那样工作。那么如何对多个列进行分组呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-10-18 09:48:48

您可以添加额外的<cfoutput group="">标记。

代码语言:javascript
复制
<cfoutput query="myQry" group="date">
 <cfoutput group="category">
  <cfoutput group="description">
   #date# #category# #description#
   <cfoutput>
    #detail#
   </cfoutput>
  </cfoutput>
 </cfoutput>
</cfoutput>
票数 14
EN

Stack Overflow用户

发布于 2012-10-18 10:02:06

看起来您对Matt有了答案,但如果您对纯sql方法感兴趣:这将创建一个“虚拟”列来执行“单个”group by,将结果连接回原始表,并使用distinct来消除重复项。丑陋,但仍然是一种整洁,我认为:)

代码语言:javascript
复制
postgres=# create table myTable(col1 int, col2 int, val int);
CREATE TABLE
postgres=#
postgres=# insert into myTable values(1, 1, 1);
INSERT 0 1
postgres=# insert into myTable values(1, 2, 2);
INSERT 0 1
postgres=# insert into myTable values(1, 2, 3);
INSERT 0 1
postgres=# insert into myTable values(2, 1, 4);
INSERT 0 1
postgres=# insert into myTable values(2, 1, 5);
INSERT 0 1
postgres=# insert into myTable values(2, 1, 6);
INSERT 0 1
postgres=# insert into myTable values(2, 2, 7);
INSERT 0 1
postgres=# insert into myTable values(2, 3, 8);
INSERT 0 1
postgres=# insert into myTable values(2, 3, 9);
INSERT 0 1
postgres=# insert into myTable values(2, 3, 10);
INSERT 0 1
postgres=# insert into myTable values(2, 3, 11);
INSERT 0 1
postgres=#
postgres=# select col1, col2, count(*)\
Invalid command \. Try \? for help.
postgres-#   from myTable
postgres-#   group by col1, col2
postgres-#   order by 1, 2;
 col1 | col2 | count
------+------+-------
    1 |    1 |     1
    1 |    2 |     2
    2 |    1 |     3
    2 |    2 |     1
    2 |    3 |     4
(5 rows)


postgres=#
postgres=#
postgres=# select col1 || ',' || col2 AS theGroup
postgres-#       ,count(*) AS theCount
postgres-#   from myTable
postgres-#   group by col1 || ',' || col2
postgres-#   order by 1;
 thegroup | thecount
----------+----------
 1,1      |        1
 1,2      |        2
 2,1      |        3
 2,2      |        1
 2,3      |        4
(5 rows)


postgres=#
postgres=#
postgres=# select distinct a.col1, a.col2, b.theCount
postgres-#   from myTable a
postgres-#       ,( select col1 || ',' || col2 AS theGroup
postgres(#                ,count(*) theCount
postgres(#            from myTable
postgres(#            group by col1 || ',' || col2 ) b
postgres-#   where a.col1 || ',' || a.col2 = b.theGroup
postgres-#   order by 1, 2;
 col1 | col2 | thecount
------+------+----------
    1 |    1 |        1
    1 |    2 |        2
    2 |    1 |        3
    2 |    2 |        1
    2 |    3 |        4
(5 rows)


postgres=#
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12945795

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档