zoukankan      html  css  js  c++  java
  • Is there any difference between GROUP BY and DISTINCT

    Is there any difference between GROUP BY and DISTINCT

    问题

    I learned something simple about SQL the other day:

    SELECT c FROM myTbl GROUP BY C
    

    Has the same result as:

    SELECT DISTINCT C FROM myTbl
    

    What I am curious of, is there anything different in the way an SQL engine processes the command, or are they truly the same thing?

    I personally prefer the distinct syntax, but I am sure it's more out of habit than anything else.

    EDIT: This is not a question about aggregates. The use of GROUP BY with aggregate functions is understood.

    回答1:

    MusiGenesis' response is functionally the correct one with regard to your question as stated; the SQL Server is smart enough to realize that if you are using "Group By" and not using any aggregate functions, then what you actually mean is "Distinct" - and therefore it generates an execution plan as if you'd simply used "Distinct."

    However, I think it's important to note Hank's response as well - cavalier treatment of "Group By" and "Distinct" could lead to some pernicious gotchas down the line if you're not careful. It's not entirely correct to say that this is "not a question about aggregates" because you're asking about the functional difference between two SQL query keywords, one of which is meant to be used with aggregates and one of which is not.

    A hammer can work to drive in a screw sometimes, but if you've got a screwdriver handy, why bother?

    (for the purposes of this analogy, Hammer : Screwdriver :: GroupBy : Distinct and screw => get list of unique values in a table column)

    回答2:

    GROUP BY lets you use aggregate functions, like AVG, MAX, MIN, SUM, and COUNT. On the other hand DISTINCT just removes duplicates.

    For example, if you have a bunch of purchase records, and you want to know how much was spent by each department, you might do something like:

    SELECT department, SUM(amount) FROM purchases GROUP BY department
    

    This will give you one row per department, containing the department name and the sum of all of the amount values in all rows for that department.

  • 相关阅读:
    POJ 1659 Frogs' Neighborhood (贪心)
    HDU 2544 最短路 (Floyd)
    CodeForces 632C Grandma Laura and Apples (模拟)
    CodeForces 731F Video Cards (数论+暴力)
    CodeForces 731C Socks (DFS或并查集)
    CodeForces 731B Coupons and Discounts (水题模拟)
    CodeForces 731A Night at the Museum (水题)
    UVaLive 6834 Shopping (贪心)
    zzuli 1484 继续双线
    zzuli 1875多线DP
  • 原文地址:https://www.cnblogs.com/chucklu/p/14338277.html
Copyright © 2011-2022 走看看