开发者

Different results on the basis of different foreign key value using a falg in where clause

Please see attached image.

alt text http://img241.imageshack.us/img241/3585/customcost.png

Can you please tell me what query will work. Please ignore isdefinite and productp开发者_如何转开发riceid columns.

Thanks


If you want a single query, this should do it if I've interpreted your question properly:

SELECT  DISTINCT t1.SupplierVenueProductID, [...]
FROM    table t1
        LEFT JOIN
                table t2
                ON t1.SupplierVenueProductID = t2.SupplierVenueProductID
                AND t2.iscustomcost = 1
WHERE   t2.SupplierVenueProductID IS NULL
OR      t1.iscustomcost = 1

I don't know your table name, but you join it to itself.


I'm a bit lost on what you want to accomplish here, going by your requirement if isCustomCost = 1 then return record #3 from SupplierVenueProductId 1 and both records from SupplierVenueProductId 2

Trying to generalize this, I think what you need is :

return all rows from the table, unless when there is a record for a SupplierVenueProductId that has isCustomCost = 1, then only return that record for this SupplierVenueProductId

Which then becomes something along the lines of :

SELECT t1.* 
  FROM myTable t1
 WHERE t1.isCustomCost = 1
    OR NOT EXISTs (SELECT *
                     FROM t2
                    WHERE t2.SupplierVenueProductId = t1.SupplierVenueProductId
                      AND t2.isCustomCost = 1)

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜