Select unique from table if specific value does not exists

HI all,

I have a table that has for each shop a value that can change over time

For example

BK_POS 1 --> Segment A

BK_POS 1 --> Segment /

What I would like to achieve is to get all distinct Shops (BK_POS) from the table above, but if for that specific pos a row exists where the segment = "/" then I do not want to take this BK_POS in my select query.


More concrete, the for example above I do not want to select BK_POS 1 because he has one row where the segment = "/".

Could anyone help me formulate this?

July 31st, 2015 5:55am

I assume that you have a table Shops

SELECT S.ShopID
FROM   Shops S
WHERE  NOT EXISTS(SELECT *
                  FROM   tbl
                  WHERE  tbl.BK_POS = S.ShopID
                    AND  tbl.Segment NOT LIKE '/%')

Is you don't have a Shop table, there is something mysterious with your data model. :-)

Free Windows Admin Tool Kit Click here and download it now
July 31st, 2015 6:13am

this?

SELECT DISTINCT Shops
FROM Table t
WHERE NOT EXISTS (
SELECT 1
FROM Table
WHERE Shops = t.Shops
AND Segment LIKE '%\%'
)

July 31st, 2015 6:26am

Please follow basic Netiquette and post the DDL we need to answer this. Follow industry and ANSI/ISO standards in your data. You should follow ISO-11179 rules for naming data elements. You should follow ISO-8601 rules for displaying temporal data. We need to know the data types, keys and constraints on the table. Avoid dialect in favor of ANSI/ISO Standard SQL. 

What you posted makes no sense. And it was very rude! 

Free Windows Admin Tool Kit Click here and download it now
July 31st, 2015 7:32pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics