0

Im trying to trim this query down. So I am going to somewhat simplify it. Here is the data I want:

|person.id|badge.bid|person.first_name|person.last_name|person.type|person_user.description|

Every field is unique except for person_user.description. Each record could have up to 40 different person_user.description fields. Problem is, I am getting duplicates because records have multiple description entries. Can you help me put those additional duplicates on the same record row like:

|person.id|badge.bid|person.first_name|person.last_name|person.type|1|2|3|4|5|etc..|40|

Here is the query:

SELECT person.id, 
       badge.bid, 
       person.first_name, 
       person.last_name, 
       person.type, 
       person_user.description 
FROM   person, 
       badge, 
       person_user 
WHERE  person.id = badge.id 
       AND person.id = person_user.person_id 
       AND badge.bid NOT LIKE "111%" 
       AND badge.access_date >= 20130401 
GROUP  BY person.id, 
          badge.bid, 
          person.first_name, 
          person.last_name, 
          person.type, 
          person_user.description 
ORDER  BY person.id 

2 Answers2

0

You could create a scalar table, populate it using INSERT INTO SELECT and UNION, and then do SELECT DISTINCT from the scalar table.

For example:

DECLARE @temptable table (col1 int, col2 int, etc...)

INSERT INTO @temptable

SELECT col1, col2
FROM table1
WHERE condition=TRUE

UNION

SELECT col1, col2
FROM table2
WHERE condition=TRUE

SELECT DISTINCT col1, col2
FROM @temptable
Scott
  • 198
  • 6
0

If duplicates are your only problem a quick fix would be to just group by id.

T I
  • 9,785
  • 4
  • 29
  • 51
Kyle Copeland
  • 479
  • 1
  • 3
  • 14