ich habe folgendes Probllem mit einem Join. Er basiert auf einer Tabelle md_unit, deren PK ein numerisches Feld unit_id ist. Der zweite Teil des Joins ist ein view, der verscheidene Felder hat u.a. auch ein Feld unit_id.
Wenn ich nun
SELECT opsc.md_unit.unit_id,
(select description from opsc.md_unit where unit_id = opsc.md_unit.unit_id)
FROM opsc.md_unit
JOIN
opsc.view_wd_tour_activity_sum
ON opsc.view_wd_tour_activity_sum.unit_id = opsc.md_unit.unit_id
ausführe erhalte ich den Fehler
“ERROR: more than one row returned by a subquery used as an expression”, obwohl opsc.md_unit.unit_id eindeutig sein müsste
Führe ich hingegen
SELECT opsc.md_unit.unit_id,
(select description from opsc.md_unit where unit_id = opsc.view_wd_tour_activity_sum.unit_id)
FROM opsc.md_unit
JOIN
opsc.view_wd_tour_activity_sum
ON opsc.view_wd_tour_activity_sum.unit_id = opsc.md_unit.unit_id
test=*# select * from b1;
id | val
----+-----
1 | 1
1 | 2
(2 rows)
Time: 0.870 ms
test=*# select * from b2;
id | t
----+---
1 | 1
(1 row)
test=*# select b1.id, (select b2.t from b2 where id = b1.id) from b1;
id | ?column?
----+----------
1 | 1
1 | 1
(2 rows)
Time: 1.643 ms
test=*# insert into b2 values(1,'2');
INSERT 0 1
Time: 1.083 ms
test=*# select b1.id, (select b2.t from b2 where id = b1.id) from b1;
ERROR: more than one row returned by a subquery used as an expression
Mit anderen Worte: ‘Müsste’ bedeutet: Du bist Dir nicht sicher.
Aber die Lösung ist nah:
(ich bin grad zu faul, meine Tabellen an Deine Bezeichnungen etc. anzupassen)
test=*# select b1.id, b2.t from b1 join b2 using (id);
id | t
----+---
1 | 1
1 | 1
1 | 2
1 | 2
(4 rows)
danke für deine Antwort. Ich habe mich wohl etwas “weich” ausgedrückt. Das Feld “opsc.md_unit.unit_id” IST eindeutig, da es der PK der Tabelle ist. Deshalb passt dein Beispiel leider nicht.
habe das Prob lösen können. Es wurde auf die Tabelle md_unit an zwei stellen zugegriffgen, ohne sie zu unterscheiden. Nach Nutzung eines Alias funktioniert alles bestens.
SELECT bla.unit_id,
(select description from opsc.md_unit where unit_id = bla.unit_id)
FROM opsc.md_unit as bla
JOIN
opsc.view_wd_tour_activity_sum
ON opsc.view_wd_tour_activity_sum.unit_id = bla.unit_id