Select mit joins und group by
am 26.11.2007 14:04:45 von MikeGuten Tag,
versuche gerade mit einem Select erforderliche Daten zu erhalten. Mein
Problem ist, das ich zusätzlich einen join auf die Tabelle
orders_products_purchase eingefügt habe und in der where Klausel auf
opp.order_state =3D '0' prüfe. Falls die Tabelle
orders_products_purchase jetzt mehr als ein Datensatz enthält, stimmt
die Summe im Feld demand_quantity erwartungsgemäss nicht mehr. Gibt es
eine Möglichkeit, dieses Problem zu umgehen?
select o.orders_id,
op.orders_products_id, op.products_id,
sum( op.products_quantity ) as demand_quantity,
p.products_quantity as stock_quantity,
pts.supplier_id,
po.purchase_id,
pop.purchase_pos_id, pop.products_quantity as order_quantity
from orders as o
join orders_products as op
on op.orders_id =3D o.orders_id
join orders_products_purchase as opp
on opp.orders_products_id =3D op.orders_products_id
left join products_to_supplier as pts
on pts.products_id =3D op.products_id
left join supplier as s
on s.supplier_id =3D pts.supplier_id
left join address_book as a
on a.address_book_id =3D s.address_book_id
left join products as p
on p.products_id =3D op.products_id
left join purchase_orders as po
on po.supplier_id =3D pts.supplier_id
and po.purchase_state < '1'
left join purchase_orders_pos as pop
on pop.purchase_id =3D po.purchase_id
and pop.products_id =3D op.products_id
where ( o.paid =3D '2' or o.release =3D '1' )
and o.cancel =3D '0'
and opp.order_state =3D '0'
group by op.products_id, pts.supplier_id
order by op.products_name, pts.supplier_id
Besten Dank für jeden Hinweis
Mike