Verknüfung zweier SELECTS

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

Wenn ich dich richtig verstanden habe, könnte es so funktionieren.
Wenn es mehrere Treffer mit der gleichen Anzahl von qty gibt, so wählt er in dem Beispiel, die höchste davon aus.

select 	p.description, p.partnumber, p.id, p.part_type, p.instruction
		, (
			select qty
				from my_orderitems i
				where i.parts_id = p.id
					and i.qty is not null
				group by qty
				order by count(*) desc, qty desc
				limit 1
		) as qty
	from my_parts p
	where  p.description ilike '%motor%' 
		and p.obsolete = false 
		and p.part_type ='service' 
		and p.instruction = true 
;

Gruß frastr

postgres=# select * from my_parts;
 id |  description   | partnumber | part_type | instruction | obsolete 
----+----------------+------------+-----------+-------------+----------
  1 | Motor wechseln | 5674       | service   | t           | f
  2 | Öl wechseln    | 5677       | service   | t           | f
(2 rows)

postgres=# select * from my_orderitems;
 id | parts_id |   qty   
----+----------+---------
  1 |        1 | 1.00000
  2 |        1 | 5.00000
  3 |        1 | 5.00000
(3 rows)

postgres=# select partnumber, description, max(qty) from my_parts p left join my_orderitems o on p.id=o.parts_id group by partnumber,description;
 partnumber |  description   |   max   
------------+----------------+---------
 5677       | Öl wechseln    |        
 5674       | Motor wechseln | 5.00000
(2 rows)

postgres=#

Ich hätte gedacht die Anforderung ist. Die qty die am häufigsten vorkommt.

Vielen Dank für die schnellen Antworten,
ich werden die Lösung von frastr verwenden.

Viele Grüße

Ronny

Dieses Thema wurde automatisch 60 Tage nach der letzten Antwort geschlossen. Es sind keine neuen Antworten mehr erlaubt.