2

I have a table look like this :

acronym | word
FCN     | FCN
FCN     | Fourth Corner Neurosurgical Associates
FHS     | FHS
HW      | HW

As you see, some acronyms have matching words and some don't. I want to keep the acronyms that have matching words. For the acronyms that don't have matching word, I'd like to keep the acronym itself. I expect the result table to look like:

acronym | word
FCN     | Fourth Corner Neurosurgical Associates
FHS     | FHS
HW      | HW

I cannot think of a way to accomplish this yet. Probably grouping by "acronym" and choosing "word", but what algorithm can decide to remove "FCN" or "Fourth Corner Neurosurgical Associates"

3 Answers 3

3

Assuming there can't be any 'bad' words:

DELETE myTable
  FROM myTable del
 WHERE [acronym] = [word]
   AND EXISTS ( SELECT *
                  FROM myTable lw -- Longer Word
                 WHERE lw.[acronym] = del.[acronym] 
                   AND Len(lw.[word]) >  Len(lw.[acronym]) )

Or do you want to avoid deleting SQL|SQL when there is a 'bad' other (longer) record that reads eg. SQL|Strange Things Happen ?

Rereading the question I'm now in doubt if you really want to DELETE those records, or simply want to SELECT from it with the bespoken records filtered out. In the latter case you'd have to use (including mellamokb's advice)

SELECT [acronym], [word]
  FROM myTable mt
 WHERE [acronym] <> [word]
    OR NOT EXISTS ( SELECT *
                      FROM myTable lw
                     WHERE lw.[acronym] = mt.[acronym] 
                       AND lw.[word] <> lw.[acronym] )
9
  • 4
    Technically speaking, Len(lw.[word]) > Len(lw.[acronym]) isn't what was requested, though it make common sense. Why not just use AND [word] <> [acronym]?
    – mellamokb
    Mar 31, 2014 at 18:00
  • I guess that would work too... I simply assumed that word would be the long version of acronym and didn't think further. Going for 'just different' would probably be OK too. My initial thought was to check if the word would fit the actual acronym, so Len() already is a major simplification =)
    – deroby
    Mar 31, 2014 at 18:04
  • @deroby - Is this okay if you only want to select and not delete ? I can't modify this query to do a delete select a1.acronym, a2.word from ( select acronym, COUNT(acronym) as cnt from acronyms group by acronym ) as a1 left join acronyms a2 on a1.acronym = a2.acronym where (cnt > 1 and a1.acronym = word) Mar 31, 2014 at 18:40
  • I want to select those records (not deleting some records). Thank you for rechecking the question
    – kaboom
    Mar 31, 2014 at 18:45
  • @BoratSagdiyev - As you can try here sqlfiddle.com/#!3/57781/3 your query doesn't quite return the requested records, in fact, it seems to return specifically those you want to exclude.
    – deroby
    Mar 31, 2014 at 19:03
0

Try this:

create table #t (acronym varchar(100), word varchar(100));
go
insert #t values
('FCN', 'FCN'),
('FCN', 'Fourth Corner Neurosurgical Associates'),
('FHS', 'FHS'),
('HW', 'HW');
go

;with x as (
    select *,
    row_number() over(partition by acronym order by case when acronym=word then 1 else 0 end) as rn
    from #t
)
delete x where rn <> 1;
0

Depending on your data, you may be able to use a combination of Distinct and a Case statement

select distinct acronym, 
(case acronym when word then word else acronym end) as Abbr 
from acronyms

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.