Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2012
    Posts
    3

    Unanswered: count of distinct rows of only selected columns

    Hi, I'm a fairly new SQL developer and am trying to just get a count of the number of distinct rows are in a table --- not looking at the entire row of fields, but only selecting a few.

    i don't want to see the distinct rows, i just want a count of how many are in the table.

    Thank you.

  2. #2
    Join Date
    Oct 2009
    Location
    221B Baker St.
    Posts
    486
    Please post some sample rows and the result you want from a query when processing this sample data.

  3. #3
    Join Date
    Aug 2012
    Posts
    3

    count of distinct rows of only selected columns - data sample

    sorry, guess that would help

    input data:

    col1 col2 col3 col4 col5 col6
    source1 name1 company1 addr1 csz1 keya
    source2 name1 company1 addr1 csz1 keya
    source3 name1 company1 addr1 csz1 keyb
    source4 name2 company2 addr2 csz2 keyc
    source5 name3 company3 addr3 csz3 keyd
    source6 name3 company3 addr3 csz3 keyd
    source7 name3 company3 addr3 csz3 keyd
    source8 name4 company4 addr4 csz4 keye
    source9 name4 company4 addr4 csz4 keye


    i only want to see how many rows there are based on distinct col2, col3, col4, col5. the output result should just be a count of "4'"

    thank you.

  4. #4
    Join Date
    Apr 2012
    Posts
    213
    Hi,

    Try:

    Code:
    with CTE as
    (    
        select distinct col2, col3, col4, col5 from MyTable
    )
    
    select COUNT(1) as Cnt FROM CTE
    Hope this helps.

  5. #5
    Join Date
    Aug 2012
    Posts
    3

    Talking count of distinct rows of only selected columns - data sample

    thank you, thank you, thank you --- that worked.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •