1

Example: On the phone table, someone has two phone number SQL would give me the a second row of the same person with different phone number instead of second column. What query do I use to check if person_id appears more than once insert second row of data in a separate column?

I hope this make sense. Thanks in advance!

chcha
  • 11
  • 2
  • If the same person has multiple values in a `Phone` table, but you want all the numbers associated with that person condensed into one row, you might have to use a dynamic sql `PIVOT` in order to accomodate any number of columns. If you are okay with all the results being put into a single column and delimited by a comma or something, you can use the `FOR XML()` feature. Providing sample data and expected output would help us help you. – EMUEVIL Jul 19 '17 at 20:41
  • @chcha welcome to SO - please see the following for how to post a question: https://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/ It would help to see sample data and what you would like to see. – Eli Jul 19 '17 at 21:07

1 Answers1

0

Try something like this:

SELECT person_id, COUNT(person_id) AS 'PersonIDCount' FROM phone_table
GROUP BY person_id
HAVING COUNT(person_id) > 1

The query will return all records where the same person_id key was inserted more than once.

MAlvarez
  • 86
  • 5
  • I think he/she wants to display each number associated with a person in another column, not just identify the people who have multiple numbers. – EMUEVIL Jul 19 '17 at 20:47