locked
OleDbCommand Order ASC within a group RRS feed

  • Question

  • User-1885804455 posted

    How can I order things within a group?

    Let's say I have this table in Access:

    Grp|Nr |
    --------+
    A | 2 |
    A | 3 |
    A | 1 |
    --------+
    B | 2 |
    B | 3 |
    B | 1 |
    --------+
    C | 2 |
    C | 3 |
    C | 1 |

    How do I write tha comand so that I get this result?

    Grp |Nr |
    --------+
    A | 1 |
    A | 2 |
    A | 3 |
    --------+
    B | 1 |
    B | 2 |
    B | 3 |
    --------+
    C | 1 |
    C | 2 |
    C | 3 |

    I have tried this, but it doesn't work:

    "SELECT [Grp], [Nr] FROM [MyTable] GROUP BY [Grp] ORDER BY [Nr] ASC"

    As for now the output is:
    1, 1, 1, 2, 2, 2, 3, 3, 3

    But I want:
    1, 2, 3, 1, 2, 3, 1, 2, 3

    Saturday, April 9, 2016 5:35 PM

Answers

  • User753101303 posted

    Hi,

    You can sort on multiple columns. My understanding is that you just want ORDER BY [Grp],[Nr]

    The weird thing here is that if you group, you'll get a single row for each group so what you shown shouldn't work (and it doesn't seems what you want. Mybe you are confgusiing sorting on a column and then on another or grouping multiple rows into one

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 9, 2016 6:14 PM
  • User-1885804455 posted

    Yes, I did confuse things. Actually I want an order within an order:

    "SELECT [Grp], [Nr] FROM [MyTable] ORDER BY [Grp] ASC, [Nr] ASC"

    That did the trick.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 9, 2016 6:24 PM

All replies

  • User753101303 posted

    Hi,

    You can sort on multiple columns. My understanding is that you just want ORDER BY [Grp],[Nr]

    The weird thing here is that if you group, you'll get a single row for each group so what you shown shouldn't work (and it doesn't seems what you want. Mybe you are confgusiing sorting on a column and then on another or grouping multiple rows into one

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 9, 2016 6:14 PM
  • User-1885804455 posted

    Yes, I did confuse things. Actually I want an order within an order:

    "SELECT [Grp], [Nr] FROM [MyTable] ORDER BY [Grp] ASC, [Nr] ASC"

    That did the trick.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 9, 2016 6:24 PM