I would appreciate some help on creating the proper SQL to retrieve only one value for matching records in my 3rd table “table C” as shown below. Table A lists customers ID, name and state. Table B lists foreign key customers ID and member card number. Table C lists sales for member card number.
select A.cifno, A.cfna1, A.cfstate, B.hbmast.cifno, B.hbmast.hbcid, C.cast(ewmast.ewcid as dec(12,0)) as "eWire ID"
from library.cfmast cfmast join library.hbmast hbmast
on cfmast.cfcif# = hbmast.cifno left join library.ewmast ewmast
on hbmast.hbcid = cast(ewmast.ewcid as dec (12,0))
Sample data:
table A
**A.cifno, A.cfna1, A.cfstate**
J00022, John, OH
B00019, Ben, TX
table B
**B.hbmast.cifno, B.hbmast.hbcid**
J00022, 555500001234
B00019, 555500007878
table C
**ewmast.ewcid**
555500001234
555500001234
555500001234
555500007878
Desired output is:
J00022, John, OH, J00022, 555500001234, 555500001234
B00019, Ben, TX, B00019, 555500007878, 555500007878
— NOT —
J00022, John, OH, J00022, 555500001234, 555500001234
J00022, John, OH, J00022, 555500001234, 555500001234
J00022, John, OH, J00022, 555500001234, 555500001234
B00019, Ben, TX, B00019, 555500007878, 555500007878
You should use the keyword
distinctin your select.I.e., something like: