sql - Select Distinct records (with a total count) that also may have similarities -
not sure if possible have table has 2 columns: id , category. sample records this:
1 accounting 2 accounting 3 accounting & tax preparation 4 banks 5 banks & financial services 6 real estate 7 real estate - commercial 8 real estate - commercial & residential 9 real estate
what want produce list selects category once counts how many occurrences of each 1 , can eliminate duplicates similar. result should like:
real estate - 4 accounting - 3 banks - 2
what have is:
sql = "select distinct(category), count(*) c " & _ "from mytable " & _ "group category" & _ "order c desc, category"
so has of similar ones listed instead of grouped in other similar ones. know can use "like" keyword wildcards when dont know possible categories be, how write it?
any advice appreciated, thanks!
except real estate
how many groups have space? use charindex
locate space (assuming ms sql) , group characters left of that.
select left(category, len(category) - charindex(' ', category)), count(*) mytable group left(category, len(category) - charindex(' ', category))
Comments
Post a Comment