작작이
132
2019-10-14 08:42:52
5
532

MSSQL 쿼리 질문 입니다.. (컬럼1 동일 항목 대한 컬럼2의 종류 수)


안녕하세요.. SQL 쿼리 질문 입니다.

컬럼 1 동일 항목에 대한 컬럼2의 종류 수를 찾는 쿼리문을 짤려고 하는데 잘 안됩니다.

GROUP BY 로 될줄 알았는데 안되는거 같습니다.

도통 모르겠습니다.

도와주세요..

0
  • 답변 5

  • taleming
    31
    2019-10-14 08:55:39

    Select  컬럼1, Count(Distinct 컬럼2)

    From    테이블명

    Group by 컬럼1


    안될까요?


  • 자라선
    1k
    2019-10-14 09:04:09

    윗분 말씀대로 하셔도 되고

    group by에 컬럼1, 컬럼2 넣으시고 Count(*) 쓰셔도 될겁니다

  • 작작이
    132
    2019-10-14 09:10:51

    헐 됩니다.. 감사합니다..

    하 몇시간을 못찾았는데 ㅜㅜ

    감사합니다..

  • 그만물어봐
    672
    2019-10-14 09:11:55

    taleming 님의 예시

    WITH T_TEST AS
    (
    	SELECT 'A' AS C_TYPE
    		  , 'AAA' AS C_GUBUN
    	UNION ALL
    	SELECT 'A' AS C_TYPE
    		  , 'BBB' AS C_GUBUN
    	UNION ALL
    	SELECT 'A' AS C_TYPE
    		  , 'CCC' AS C_GUBUN
    	UNION ALL
    	SELECT 'A' AS C_TYPE
    		  , 'DDD' AS C_GUBUN
    	UNION ALL
    	SELECT 'A' AS C_TYPE
    		  , 'AAA' AS C_GUBUN
    	UNION ALL
    	SELECT 'B' AS C_TYPE
    		  , 'EEE' AS C_GUBUN
    	UNION ALL
    	SELECT 'B' AS C_TYPE
    		  , 'FFF' AS C_GUBUN
    	UNION ALL
    	SELECT 'B' AS C_TYPE
    		  , 'GGG' AS C_GUBUN
    	UNION ALL
    	SELECT 'B' AS C_TYPE
    		  , 'HHH' AS C_GUBUN
    	UNION ALL
    	SELECT 'C' AS C_TYPE
    		  , 'JJJ' AS C_GUBUN
    	UNION ALL
    	SELECT 'C' AS C_TYPE
    		  , 'JJJ' AS C_GUBUN
    	UNION ALL
    	SELECT 'C' AS C_TYPE
    		  , 'JJJ' AS C_GUBUN
    	UNION ALL
    	SELECT 'D' AS C_TYPE
    		  , 'KKK' AS C_GUBUN
    	UNION ALL
    	SELECT 'D' AS C_TYPE
    		  , 'LLL' AS C_GUBUN
    )
    SELECT C_TYPE, COUNT(DISTINCT C_GUBUN) AS CNT
      FROM T_TEST
     GROUP BY C_TYPE


    자라선 님 예시


    WITH T_TEST AS
    (
    	SELECT 'A' AS C_TYPE
    		  , 'AAA' AS C_GUBUN
    	UNION ALL
    	SELECT 'A' AS C_TYPE
    		  , 'BBB' AS C_GUBUN
    	UNION ALL
    	SELECT 'A' AS C_TYPE
    		  , 'CCC' AS C_GUBUN
    	UNION ALL
    	SELECT 'A' AS C_TYPE
    		  , 'DDD' AS C_GUBUN
    	UNION ALL
    	SELECT 'A' AS C_TYPE
    		  , 'AAA' AS C_GUBUN
    	UNION ALL
    	SELECT 'B' AS C_TYPE
    		  , 'EEE' AS C_GUBUN
    	UNION ALL
    	SELECT 'B' AS C_TYPE
    		  , 'FFF' AS C_GUBUN
    	UNION ALL
    	SELECT 'B' AS C_TYPE
    		  , 'GGG' AS C_GUBUN
    	UNION ALL
    	SELECT 'B' AS C_TYPE
    		  , 'HHH' AS C_GUBUN
    	UNION ALL
    	SELECT 'C' AS C_TYPE
    		  , 'JJJ' AS C_GUBUN
    	UNION ALL
    	SELECT 'C' AS C_TYPE
    		  , 'JJJ' AS C_GUBUN
    	UNION ALL
    	SELECT 'C' AS C_TYPE
    		  , 'JJJ' AS C_GUBUN
    	UNION ALL
    	SELECT 'D' AS C_TYPE
    		  , 'KKK' AS C_GUBUN
    	UNION ALL
    	SELECT 'D' AS C_TYPE
    		  , 'LLL' AS C_GUBUN
    )
    SELECT A.C_TYPE, COUNT(1) AS CNT
      FROM (
    			SELECT C_TYPE, C_GUBUN
    			  FROM T_TEST
    			 GROUP BY C_TYPE, C_GUBUN
    		 ) A
     GROUP BY A.C_TYPE


  • 작작이
    132
    2019-10-14 09:39:37

    그만물어봐 님 

    예시 감사합니다.

    자라선 님 방법은 해봐도 안되서 아닌가보다 했는데

    제가 잘못 이해하고 쿼리를 만들었네요..

    둘다 됩니다. 

    다들 감사합니다.

  • 로그인을 하시면 답변을 등록할 수 있습니다.