0
select DISTINCT first_name, last_name, picture, last_active, id_participante1, id_participante2, id_user, [message], dataHora from chat_b_users inner join utilizadores
on chat_b_users.id_participante2 = utilizadores.id_user
left join chat_talks on chat_b_users.id_chat = chat_talks.id_chat
where id_participante1 = 1
union all
select DISTINCT first_name, last_name, picture, last_active, id_participante1, id_participante2, id_user, [message], dataHora from chat_b_users inner join utilizadores 
on chat_b_users.id_participante1 = utilizadores.id_user
left join chat_talks on chat_b_users.id_chat = chat_talks.id_chat
where id_participante2 = 1 order by last_active DESC

how can i select a distinct value?

I need to return all this data even nulls but on of each user, how can i make this?

results:

enter image description here

as you can see in the image, i've two chats from the same user, i only want one of each.

Severiano
  • 1,083
  • 3
  • 25
  • 54

2 Answers2

2

try this : add your column list(s) in place of in the code to identify which row you want to display.

 SELECT first_name, last_name, picture, last_active, id_participante1, id_participante2, id_user, [message], dataHora
(
select DISTINCT first_name, last_name, picture, last_active, id_participante1, id_participante2, id_user, [message], dataHora ,
ROW_NUMBER (PARTITION BY <add_yr_colist> ORDER BY Last_Avtive DESC) AS RNUM 
from chat_b_users inner join utilizadores
on chat_b_users.id_participante2 = utilizadores.id_user
left join chat_talks on chat_b_users.id_chat = chat_talks.id_chat
where id_participante1 = 1 OR id_participante2 = 1
)TVC WHERE RNUM = 1
Rishabh
  • 78
  • 7
  • what is this? – Severiano Sep 10 '14 at 10:08
  • 1
    add a list of the columns which will identify a one particular window.say in your example you need to display message OK for user_Id = 40 , then replace with user_id which will partition the rows in different windows based on user_id values. Hope this explains. – Rishabh Sep 10 '14 at 10:11
  • ROW_NUMBER (PARTITION BY User_id ORDER BY Last_Active DESC) – Rishabh Sep 10 '14 at 11:24
  • SELECT * ( select DISTINCT first_name, last_name, picture, last_active, id_participante1, id_participante2, id_user, [message], dataHora, ROW_NUMBER (PARTITION BY id_user ORDER BY last_active DESC) AS RNUM from chat_b_users inner join utilizadores on chat_b_users.id_participante2 = utilizadores.id_user left join chat_talks on chat_b_users.id_chat = chat_talks.id_chat where id_participante1 = 1 OR id_participante2 = 1 )TVC WHERE RNUM = 1 ||| still has error – Severiano Sep 10 '14 at 11:28
  • my mistake! but the erro continues in BY – Severiano Sep 10 '14 at 11:37
  • 1
    well my mistake this time : ROW_NUMBER Should be like this ROW_NUMBER() OVER (PARTITION BY id_user ORDER BY last_active DESC) – Rishabh Sep 10 '14 at 11:41
0

You need to use UNIION instead of UNION ALL as below:

select DISTINCT first_name, last_name, picture, last_active, id_participante1, id_participante2, id_user, [message], dataHora from chat_b_users inner join utilizadores
on chat_b_users.id_participante2 = utilizadores.id_user
left join chat_talks on chat_b_users.id_chat = chat_talks.id_chat
where id_participante1 = 1
union 
select DISTINCT first_name, last_name, picture, last_active, id_participante1, id_participante2, id_user, [message], dataHora from chat_b_users inner join utilizadores 
on chat_b_users.id_participante1 = utilizadores.id_user
left join chat_talks on chat_b_users.id_chat = chat_talks.id_chat
where id_participante2 = 1 order by last_active DESC

UNION removes duplicate records (where all columns in the results are the same), UNION ALL does not.

Difference betwwen UNION & UNION ALL

Community
  • 1
  • 1
Amit
  • 15,217
  • 8
  • 46
  • 68