select distinct count

arkiboys 9,706 Reputation points
2022-08-17T02:59:27.08+00:00

hello,
how is it possible to get the distinct count?
please see screen-shot which shows the data and what I would like the result to show as
Thank you

231836-image.png

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Wilko van de Velde 2,241 Reputation points
    2022-08-17T06:23:57.57+00:00
    DROP TABLE IF EXISTS #temp  
      
    CREATE TABLE #temp  
    (  
    ID int,  
    sec nvarchar(5)  
    )  
      
    insert into #Temp (ID,sec) VALUES (999,'XXX')  
    insert into #Temp (ID,sec) VALUES (999,'XXX')  
    insert into #Temp (ID,sec) VALUES (888,'XXX')  
    insert into #Temp (ID,sec) VALUES (888,'XXX')  
    insert into #Temp (ID,sec) VALUES (432,'XXX')  
    insert into #Temp (ID,sec) VALUES (432,'XXX')  
    insert into #Temp (ID,sec) VALUES (678,'XXX')  
    insert into #Temp (ID,sec) VALUES (678,'XXX')  
      
      
    SELECT	ID,  
    		sec,  
    		COUNT(distinct sec)   
    FROM	#temp   
    GROUP BY ID,  
    		 sec  
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Isabellaz-1451 3,616 Reputation points
    2022-08-17T05:57:09.41+00:00

    Hi @arkiboys
    It is a easy question.

    SELECT ID ,MAX(sec),COUNT(*) FROM TABLE GROUP BY ID  
    

    Best Regard,
    Isabella


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.