我正在尝试组合多个表中的多个列,但我的一些数据似乎是重复出现的。我的查询应该如何避免这种情况?
我有以下表格:
LEGE_ROM
+----------+------------+-----+
| ansattnr | dag | rom |
+----------+------------+-----+
| L102 | 2012-09-23 | b01 |
+----------+------------+-----+
| L100 | 2012-09-12 | k10 |
+----------+------------+-----+
| L100 | 2013-03-05 | k10 |
+----------+------------+-----+
| L100 | 2014-03-02 | k10 |
+----------+------------+-----+
| L100 | 2014-10-15 | K10 |
+----------+------------+-----+
| L100 | 2014-11-03 | k10 |
+----------+------------+-----+
| L102 | 2012-09-12 | k11 |
+----------+------------+-----+
| L100 | 2014-11-10 | k12 |
+----------+------------+-----+
| L110 | 2012-09-13 | k12 |
+----------+------------+-----+
ROM_BEHANDLING
+-----+--------------+
| rom | behandling |
+-----+--------------+
| b01 | kirurgisk |
+-----+--------------+
| k10 | konsultasjon |
+-----+--------------+
| k11 | konsultasjon |
+-----+--------------+
| k12 | konsultasjon |
+-----+--------------+
所需的输出表是(表格已被编辑):
+----------+-----+--------------+
| ansattnr | rom | behandling |
+----------+-----+--------------+
| L100 | k10 | konsultasjon |
+----------+-----+--------------+
| L102 | k11 | konsultasjon |
+----------+-----+--------------+
| L110 | k12 | konsultasjon |
+----------+-----+--------------+
并且应该使用join来实现所需的输出表。
我尝试使用以下查询:
SELECT lr.ansattnr, lr.rom, rb.behandling
FROM LEGE_ROM lr JOIN ROM_BEHANDLING rb
WHERE rb.behandling='konsultasjon';
答案 0 :(得分:1)
我认为这就是你要找的东西。
它使用LEFT JOIN
,因此即使ROM_BEHANDLING
中没有相应的行,它也会从LEGE_ROM
返回行。
SELECT lr.ansattnr, lr.rom, rb.behandling
FROM ROM_BEHANDLING rb
INNER JOIN LEGE_ROM lr on lr.rom = rb.rom
GROUP BY lr.rom
HAVING rb.behandling = 'konsultasjon';
工作示例here