sql - Postgresql query gives less results for wider search -
i'm facing wierd issue query.
my query complax i'll use simpler version of it.
select distinct on (a.batch) c.id,a.partid,b.partname,c.stock_stock, components join parts b using(partid) left join (select * componentsreport($1)) c using (partid) join start_work d on d.batchid=a.batchid b.issub group c.id,a.partid,b.partname,c.stock order a.batch; this query gives many rows 1 row c.id=3436:
3436 124 'cpu-a' 450 however when add criteria c.id=3436 :
select distinct on (a.batch) c.id,a.partid,b.partname,c.stock_stock, components join parts b using(partid) left join (select * componentsreport($1)) c using (partid) join start_work d on d.batchid=a.batchid c.id=3436 , b.issub group c.id,a.partid,b.partname,c.stock order a.batch; i 6 rows:
3436 124 'cpu-a' 450 3436 125 'cpu-a' 130 3436 125 'cpu-a' 660 3436 126 'cpu-a' 0 3436 127 'cpu-a' 40 3436 128 'cpu-a' 40 which correct!
i don't understand how when add more conditions more rows? looks bugged!
i'm using postgresql 9.3.3
please advise may cause problem.
this correct result. reason, have no group a.batch, distinct.
f.ex. have following data:
c.id = 3436, a.batch = 1 c.id = 3436, a.batch = 2 c.id = 3437, a.batch = 1 c.id = 3437, a.batch = 2 select distinct after group , order gave 1 unique batch = 1
select gave 2 unique a.batch: 1, 2.
so, fix query need wrap first variant of select 1 condition.
select * ( select distinct on (a.batch) c.id,a.partid,b.partname,c.stock_stock, components join parts b using(partid) left join ( select * componentsreport($1) ) c using (partid) join start_work d on d.batchid=a.batchid b.issub group c.id,a.partid,b.partname,c.stock order a.batch ) tmp tmp.id = 3436
Comments
Post a Comment