Need to query distinct combination of two fields, along with a count that distinct combination occurs

前端 未结 2 1663
眼角桃花
眼角桃花 2021-01-21 11:45

What I need is a query on a table that would return distinct combinations of columns A and B, along with the count of how many times each combination occurs in the table. This w

相关标签:
2条回答
  • 2021-01-21 12:10

    GROUP BY is your friend here:

    select a,b,count(*) from test
    group by a,b
    order by a
    

    SQLFiddle: http://sqlfiddle.com/#!9/062b0e/5

    0 讨论(0)
  • 2021-01-21 12:19

    Use GROUP BY like this

    SELECT
        `A`,
        `B`,
        COUNT(*) AS `Count`
    FROM
        `table`
    GROUP BY
        `A`, `B`
    ORDER BY
        `A`
    
    0 讨论(0)
提交回复
热议问题