0

How do I join a table with a distinct value in a SQL view? My code looks like below

Select a, b, c, d from TableA inner join TableB on TableA.account = TableB.account

I want TableB.account to have distinct values when I join the table.

The selected fields(a,b,c,d) do not have to be distinct values.

hana
  • 101
  • 10
  • 3
    I don't follow what you're asking here. Sample data and expected results, along with your attempts will help us help you. – Thom A Nov 16 '21 at 15:49
  • Does this answer your question? [sql server select first row from a group](https://stackoverflow.com/questions/7344731/sql-server-select-first-row-from-a-group) – SMor Nov 16 '21 at 15:51
  • Does this answer your question? [Selecting distinct values from joined results](https://stackoverflow.com/questions/19323577/selecting-distinct-values-from-joined-results) – Matt Andruff Nov 16 '21 at 16:01
  • what I want is to use is a distinct field when I join the table For example, Select a, b, c, d from Table A inner join tableB on tableA.account = TableB.account and I want the TableB.account field to be a distinct value, and the fields in the selected field do not have to be distinct values. – hana Nov 17 '21 at 07:56

1 Answers1

1
SELECT distinct_foo.thing
     , bar.other_things
FROM   (
        SELECT DISTINCT
               thing
        FROM   foo
       ) AS distinct_foo
 INNER
  JOIN bar
    ON bar.thing = distinct_foo.thing
;
gvee
  • 16,732
  • 35
  • 50