sql : select 查询无重复数据

sql : select query without duplicate data

select ft.id, ft.qty, st.data from first_table ft
left outer join second_table st on ft.id = st.id
where ft.id = '1abc'

我有两个 table。这些 table 的架构如下

无 1 table 架构

Id Desc Qty
1abc One 3
1abc two 6

无 2 table 架构

Id Data
1abc 12ab
1abc 23ab
1abc z99c

加入查询后数据显示错误

Id Desc Qty Data
1abc One 3 12ab
1abc two 6 12ab
1abc One 3 23ab
1abc two 6 23ab
1abc One 3 z99c
1abc two 6 z99c

以下是它应该在 crystal 报告中显示的正确方式。请建议我该怎么做。

Id Qty Data
1abc 3 12ab
6 23ab
z99c

您似乎想在单独的列中列出值。这通常最好在应用层完成,但您也可以使用 SQL:

select min(id), min(qty), min(data)
from ((select id, null as qty, null as data,
              row_number() over (order by id) as seqnum
       from table1
       group by id
      ) union all
      (select null as id, qty, null as data,
              row_number() over (order by qty) as seqnum
       from table1
       group by qty
      ) union all
      (select null as id, null as qty, data,
              row_number() over (order by data) as seqnum
       from table2
       group by data
      )
     ) x
group by seqnum;