Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

SQL GROUP BY Clause

by lex
on Sep 26, 2005 at 09:18 UTC ( #495038=perlquestion: print w/ replies, xml ) Need Help??
lex has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

Does the order of columns in a SQL Group By clause matter? If not, should it? Discuss.
Code from other languages (esp. Perl) can be used in the discussion.

P.S. This is not a school essay that I want you to write for me.

Comment on SQL GROUP BY Clause
Re: SQL GROUP BY Clause
by planetscape (Canon) on Sep 26, 2005 at 09:30 UTC
    P.S. This is not a school essay that I want you to write for me.

    Say, that is convincing... ;-) Please see this.

    planetscape
      No, really it is not homework. I wrote it in that style because I want to understand better the way SQL was designed. And a general interest requires an open question.
Re: SQL GROUP BY Clause
by rnahi (Curate) on Sep 26, 2005 at 09:44 UTC

    You are desperately OT in this forum.

    Aside from that, let me analyze your question.

    Does the order of columns in a SQL Group By clause matter?

    Why would anyone ask somathing like that? If you were doing something practical with SQL, you could just throw two queries, changing the order of columns in the GROUP BY, and seeing for yourself if they differ or not (I am not going to give you the answer indirectly :-P).

    So youy were not doing anything practical. Then why would you be curious about this? The only plausible answer is because somebody asked you. Hence, it's homework.

    One more indication of homework is the "discuss" clause. You don't come to a forum and say "discuss" for something you don't know. You can come here, post something in Meditations, say what youy think about that, and then ask for discussion. But discussing some unknown thing that your teacher asks you to find out is hardly something that can tickle our willingness to help.

Re: SQL GROUP BY Clause
by rinceWind (Monsignor) on Sep 26, 2005 at 09:46 UTC

    lex: what do YOU think? What's your application? Whether is does matter or not depends on what you are wanting to achieve.

    Also, you have not said which database platform you are using, which would significantly affect the answer.

    P.S. This is not a school essay that I want you to write for me.

    What are you looking for then?

    --

    Oh Lord, won’t you burn me a Knoppix CD ?
    My friends all rate Windows, I must disagree.
    Your powers of persuasion will set them all free,
    So oh Lord, won’t you burn me a Knoppix CD ?
    (Missquoting Janis Joplin)

      So for which platforms would the answer be yes and which would it be no.
        The order of the grouping should be alphabetical, as this is how SQL databases store their data internally.

        However, this is not always true for multi-processor systems, as they can group things simultaneously, so the order is not as important.

        hope this helps.

        UPDATE:

        Sorry guys and galls. The above to statements are untrue (well, I made them up, I guess they’re untrue).

        lex opened his first post with the salutation "Dear Monks", and felt the need to post that his question wasn't homework. To my mind, this tells me that he's not a perlmonks newbie, so he knew how this question was going to be received, but he still asked it. He even followed it up with another request for information, without showing he’s done anything to help himself.

        My response was supposed to be believable to any one who read it, but didn’t actually _read_ it. I have to admit I enjoyed a moment of guilty pleasure thinking he might copy it down verbatim on his homework sheet.

        You might say that's not in the spirit of this site, but neither is the question, or his disregard of the time other people put in to answering it. Anyway, votes count, and I'm minus 6 for this post (my first-ever down voted node) so it shall avoid attempts at comedy in the future.

        ---
        my name's not Keith, and I'm not reasonable.
Re: SQL GROUP BY Clause
by jZed (Prior) on Sep 27, 2005 at 01:32 UTC
    As you can see from people's (lack of) response, if you want discussion, you might be better off giving your own thoughts to kick the discussion into gear. That said, my guess is that no, the order should not matter. If you group SUM(sales) by "year, region", you are asking to see totals for each year and totals for each region and if you group them by "region, year", you are still asking for totals for each region and for each year (in other words, the same two totals). Is that what you'd expect? What about it merits discussion?
Re: SQL GROUP BY Clause
by chrism01 (Friar) on Sep 27, 2005 at 06:53 UTC
    I agree it sounds a lot like homework....
    however, .. yes it matters.
    The reason is SQL presents the results grouped (nested) in the order you asked, so SALES grouped by yr, mth would group sales by yr, then by mth within that yr. Evidently the other way round would make no sense unless you were looking for seasonal patterns...
    Because it's easier to code group by internally by first sorting the data as per request, group by normally implies order by the same values will occur in the output.
Re: SQL GROUP BY Clause
by xorl (Deacon) on Oct 05, 2005 at 16:22 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://495038]
Approved by monkfan
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2014-12-21 17:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (106 votes), past polls