locked
Count record using SQL RRS feed

  • Question

  • User2101416554 posted

    This is my table ...

    PONO	Year	Customer
    P1	2019	A
    P2	2019	B
    P3	2020	C
    P4	2020	D
    P5	2020	A
    P6	2020	A
    P7	2019	B
    P8	2020	B
    P9	2020	B
    

    I would like to count the record based on customer and year and the output will be like

    Output	
    	
    Count	Year	Customer
    1	2019	A
    2	2019	B
    2	2020	A
    2	2020	B
    1	2020	C
    1	2020	D
    

    Monday, January 27, 2020 6:54 AM

Answers

  • User452040443 posted

    Try something like this:

    select
        [Year],
        [Customer],
        count(1) as [Count]
    from MyTable
    group by
        [Year],
        [Customer]
    

    Hope this help

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 27, 2020 12:03 PM
  • User-719153870 posted

    Hi umerfaiz123,

    Output	
    	
    Count	Year	Customer
    1	2019	A
    2	2019	B
    2	2020	A
    2	2020	B
    1	2020	C
    1	2020	D

    This is actually a basic query in SQL Server, it needs to use COUNT( about count(1) and count(*) ), GROUP BY and ORDER BY

    select COUNT(*) as [Count],[Year],[Customer] from YourTable group by [Year],[Customer] order by [Year],[Customer]
    

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 28, 2020 2:59 AM

All replies

  • User452040443 posted

    Try something like this:

    select
        [Year],
        [Customer],
        count(1) as [Count]
    from MyTable
    group by
        [Year],
        [Customer]
    

    Hope this help

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 27, 2020 12:03 PM
  • User-719153870 posted

    Hi umerfaiz123,

    Output	
    	
    Count	Year	Customer
    1	2019	A
    2	2019	B
    2	2020	A
    2	2020	B
    1	2020	C
    1	2020	D

    This is actually a basic query in SQL Server, it needs to use COUNT( about count(1) and count(*) ), GROUP BY and ORDER BY

    select COUNT(*) as [Count],[Year],[Customer] from YourTable group by [Year],[Customer] order by [Year],[Customer]
    

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 28, 2020 2:59 AM