No se pueden obtener valores únicos uniendo dos tablas

I have 2 tables that I need to join and select the unique rows from. Here is a sample of my data: (there are more columns)

MB# MBName  PCCNo_PRI   Primary_IP       PCCNo_SEC          Secondary_IP    ID
100  name    0            30           1 
103  name3   17           47           4
403  name13  17           47           14


15  0           30          6
52  12          42          1
53* 17          47          1
54  18          48          1
63  9           39          2
69* 17          47          2

I need to join the two tables and get the unique RTU(s) in tbl2 for a given MB# in tbl1. Query =

SELECT t1.MB#,t2.RTU,t2.[Device Manufacturer],t2.PCC#_PRI,t2.PCC#_SEC,t2.[STATION ADDRESS] 
FROM mbm_PCDIMasterBlk_tbl as t1, dbo.WOA_PCC_Conn_tbl as t2

I am getting duplicate rows for tbl2 53 and 69 (* above). 53 ends up with 2 entries; one to 103 and one 403 (69 gets same). How can I query this for unique RTU(s) to MB#?

preguntado el 09 de marzo de 12 a las 15:03

Can you post what your results should be? How do you decide which row you need. for ex for pccNo_pri = 17, which one you need 53 or 69? what is the logic? -

If you want the complete set of combos of RTU y MB#, then your results are correct. Your results would be (RTU, `PCCNo_PRI', 'MB#') : (53, 17, 103), (53, 17, 403), (69, 17, 103), (69, 17, 403). Since your data clearly contains combos where the same RTU applies to mulitple MB#, what is your actual problem and how would you identify which rows are "wrong"? -

Good question. I think you have exposed my problem. I know what it should be but couldn't see my flaw until you guys pointed it out. Let me see if I can redo one of these tables. -

The results should be: 53 ->103 (only) & 69 -> 403 (only) -

1 Respuestas

The duplicate rows appears because you join on "17" which gives 2 rows on each side

Then, as it stands, you can't with that SELECT list.
How do you decide which t1.MB# you want for the t2 columns?

There is no secondary JOIN column that I can see.
So the best you can get is use MAX (or MIN) to pick either 403 or 103.

   MAX(t1.MB#) AS MB#,
   t2.RTU,t2.[Device Manufacturer],t2.PCC#_PRI,t2.PCC#_SEC,t2.[STATION ADDRESS] 
   dbombm_PCDIMasterBlk_tbl as t1
   dbo.WOA_PCC_Conn_tbl as t2 ON t1.PCCNo_PRI = t2.PCC#_PRI
   t2.RTU,t2.[Device Manufacturer],t2.PCC#_PRI,t2.PCC#_SEC,t2.[STATION ADDRESS] 

respondido 09 mar '12, 16:03

Good code, it eliminates the duplicates but does not give the correct result. 53 & 69 get assigned to MB#403. 53 should go to 103 and 69 to 403. I see the question about how to determine what the results should be. In trying to answer that I realize that really 53 would only get assigned to 103 because of the order of the row processing not because of any logical assignment. - cjjeeper

This is the answer. I went back and fixed tbl1 (found an error in how I was populating the data) and then this query worked. Thanks gbn. - cjjeeper

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.