3

I have a table that can store multiple descriptions for each code. However there is a flag in that table that is to indicate which of those is the main or primary description. In some instances, we have codes that have more than one with this flag set to Y which is not correct.

I am having trouble coming up with the SQL to get all the rows in that table that have more than one description set to Y.

I've used this SQL to identify rows that do not have ANY dsp_fg = 'Y'

select *
from table A
where dsp_fg = 'N'
and not exists (select 1 FROM table where cod_int_id = A.cod_int_id AND dsp_fg = 'Y')

But I am having trouble writing the SQL to get me the cod_int_ids that have more than one Y record, can someone help?

JoSSte
  • 2,953
  • 6
  • 34
  • 54
  • Hello Jason, welcome to StackOverflow. What queries have you tried so far and why are they failing? Consider adding this information to your question so that it helps other users answer your question. :) – goncalotomas Mar 07 '18 at 20:03

1 Answers1

0
SELECT int_id FROM A
WHERE dsp_fg = 'Y'
GROUP BY int_id
HAVING count(1) > 1 

This is not perfect, but it identifies what I need.

JoSSte
  • 2,953
  • 6
  • 34
  • 54