Sorting a grouped report

牧云@^-^@ 提交于 2019-12-24 17:10:07

问题


My schema looks a bit like this:

SEQ  GROUP1  GROUP2  GROUP3  VALUE

Where SEQ is roughly equivalent to the row number -- it's actually sort order, from an external table -- and the GROUPx fields delineate the data further. So, for example:

SEQ  GROUP1  GROUP2  GROUP3  VALUE
---- ------- ------- ------- ------
1    A       A       A       123
2    A       A       A       456
3    A       B       C       foo
5    X       Y       Z       bar
4    A       B       D       baz

I would like this data grouped in this hierarchy, but ordered by SEQ. So, the above data would produce a report like:

A
  A
    A
      123
      456
  B
    C
      foo
    D
      baz
X
  Y
    Z
      bar

I've been playing with the "grouping and sorting" options of the report and I seem to be able to have it ordered but not grouped, or grouped but not ordered!

For example, if I specify:

  • Group by GROUP1
    • Group by GROUP2
      • Group by GROUP3
        • Order by SEQ

The values in the grouping level fields determine the order in which the data comes out. (In this case, the names I've chosen are in alphabetical order anyway, but if I changed the name of X in GROUP1 to 123, it would appear right at the top of the report.)

If I move the "Order by SEQ" to the highest priority, the order is correct, but the grouping levels are broken: Access basically treats the SEQ field as a grouping level and then the subsequent hierarchy is repeated for each value in that group.

Is there any way I can achieve what I'm looking for without having a SEQx field for each GROUPx field (rather than my current "global" SEQ)?


回答1:


I think you're going to have to do a Left Join on this. First, create a query using only SEQ, and order it by SEQ. Then take your group-by query and Left Join it to your first query, so that it keeps the proper order. Make sense?

Your query's code should more or less be:

SELECT 
First(B.SEQ) AS FirstOfSEQ, tblTest.Group1, tblTest.Group2, tblTest.Group3, tblTest.Val AS Val2 FROM (SELECT tblTest.SEQ FROM tblTest ORDER BY tblTest.SEQ) AS B 
LEFT JOIN tblTest ON B.SEQ = tblTest.SEQ 
GROUP BY tblTest.Group1, tblTest.Group2, tblTest.Group3, tblTest.Val 
ORDER BY First(B.SEQ); 

In a report layout, order it by SEQ, then group it by the other values and you should get what you need.




回答2:


I resolved this on my own

Select TimeOffRequestID, yourname AS "Employee Name", Replace(forwardcallsto, 'Select One', 'Voicemail') AS "Forward Calls To", appointment AS "Comments" FROM dbo.nei_TimeOffRequest WHERE CAST(CONVERT(VARCHAR,GETDATE(),101) AS DATETIME) BETWEEN startingon AND endingon ORDER BY yourname ASC



来源:https://stackoverflow.com/questions/17171267/sorting-a-grouped-report

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!