如何在多列上对我的 cfquery 进行分组?

5

我需要将查询结果按多列匹配进行分组。例如,按日期、类别和描述进行分组。我知道如何在一个列上使用cfoutput分组,例如:

<cfoutput query="myQry" group="date">
  #date#
  <cfoutput>
    #detail#
  </cfoutput>
</cfoutput>

但是,我希望能够根据多个列匹配来分组,例如:

<cfoutput query="myQry" group="date,category,description">
  #date# #category# #description#
  <cfoutput>
    #detail#
  </cfoutput>
</cfoutput>

我知道cfoutput分组不像上述那样工作。那么,我如何在多列上进行分组?
2个回答

16

您添加了额外的<cfoutput group="">标签。

<cfoutput query="myQry" group="date">
 <cfoutput group="category">
  <cfoutput group="description">
   #date# #category# #description#
   <cfoutput>
    #detail#
   </cfoutput>
  </cfoutput>
 </cfoutput>
</cfoutput>

谢谢。之前我无法让它工作,但显然是我的错误,因为现在它对我起作用了。 - Jason M
4
如果您发现您的分组不正确或者看起来不稳定,请检查您 SQL 中的 ORDER BY 子句。确保您分组的列也在 ORDER BY 中,并且按照相同的嵌套顺序进行排序,例如:SELECT column(s) FROM table(s) ORDER BY date, category, description。 - genericHCU

4

看起来你已经获得了Matt的答案,但是如果你想了解一个纯SQL的方法:这将创建一个“虚拟”列来执行“单一”分组,将结果再次连接回原始表,并使用distinct来消除重复项。有点丑陋,但我认为仍然很不错 :)

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=#

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接