Hey All,
ich habe eine Tabelle my_parts für Ersatzteile und Dienstleistungen.
Ich habe eine weitere Tabelle my_orderitems für die Positionen in der Rechnung.
In my_orderitems gibt es qty (Quantity).
Ich möchte nun wenn ich einen Artikel abfrage eine Spalte hinzufügen in wecher steht:
wie hoch die häufigste Quantity ist. (zweite SELECT Query)
Ich möchte also die beiden SELECT-Queries kombinieren.
Das Ergebnis soll so:
Motor wechseln | 5674 | 1 | service | true | 5.00000
aussehen.
DROP TABLE IF EXISTS my_parts;
DROP TABLE IF EXISTS my_orderitems;
CREATE TABLE my_parts (
id SERIAL,
description text,
partnumber text,
part_type text,
instruction bool,
obsolete bool
);
CREATE TABLE my_orderitems (
id SERIAL,
parts_id int,
qty numeric(25,5)
);
INSERT INTO my_parts (description, partnumber, part_type, instruction, obsolete) VALUES ('Motor wechseln', 5674, 'service', true, false);
INSERT INTO my_parts (description, partnumber, part_type, instruction, obsolete) VALUES ('Öl wechseln', 5677, 'service', true, false);
INSERT INTO my_orderitems (parts_id, qty) VALUES (1, 1.00000);
INSERT INTO my_orderitems (parts_id, qty) VALUES (1, 5.00000);
INSERT INTO my_orderitems (parts_id, qty) VALUES (1, 5.00000);
SELECT description, partnumber, my_parts.id, part_type, instruction FROM my_parts WHERE description ILIKE '%Motor%' AND obsolete = FALSE AND part_type ='service' AND instruction = true ORDER BY ( SELECT ( SELECT count( qty ) FROM orderitems WHERE parts_id = my_parts.id ) ) DESC NULLS LAST LIMIT 5;
SELECT qty FROM (SELECT qty, count( qty ) AS ct FROM my_orderitems WHERE parts_id = 1 GROUP BY 1 ORDER BY ct DESC LIMIT 1) AS test;
Viele Grüße
Ronny