0

I have a table looking like the following and I need a table with the unique values of Id and another one with the Id values repeated.

My problem is that I can't use a "select distinct" statement because it will include the repeated Id values once.

Id letter

80004 A

80008 B

80019 C

80086 A

80086 C

80086 B

80066 A

80099 C

80100 A

80087 C

80087 A

What I need is to divide the previous table into two tables looking like:

Id letter

80004 A

80008 B

80019 C

80066 A

80099 C

80100 A

and

Id letter

80086 A

80086 C

80086 B

80087 C

80087 A

4
  • I don't understand the logic of this split. Please expand. Commented Jun 21, 2016 at 11:13
  • I just want a table with those Id that only have one group (like 80004, 80008...) and their group. I also need another table with those Id that can have different groups (80086 and 80087) and their groups (A,C and B for 80086, and C and A for 80087). Commented Jun 21, 2016 at 11:19
  • But why is 80004,80008 is a group and not 80004,80086 ?? What's the logic behind this Commented Jun 21, 2016 at 11:20
  • instead of saying that 80004 and 80008 I'd say that is a letter (I edit the question) Commented Jun 21, 2016 at 11:24

1 Answer 1

1

By using a sub query:

select A.id , id_value from ( select id from mytable group by id having count(id_value) >1 ) A inner join mytable using(id) select A.id , id_value from ( select id from mytable group by id having count(id_value) <=1 ) A inner join mytable using(id) 

Here is the SQLFIDDLE

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.