How to select all in a query but not certain items

  • I have the following columns and there are 807k rows in all so this is just example:

    Line of Business                           Product                                           Market
    Government                                  MMP                                                   VA
    Government                                  MMP                                                   CA
    Government                                  MMP                                                   TX
    Commercial                                   PPO                                                    VA
    Commercial                                   HMO                                                   CA
    Private                                           PPO                                                     TX

    I want to select all of the above except MMP in VA so my return would be everything else. As I said I have 807k rows and many more lines of business, same products of MMP, and the state of VA that appears for other lines of business and products. I was trying where (product = 'mmp' and market = 'va') but doing that excludes all VA and all MMP.


  • SELECT [Line of Business], Product, Market
    FROM [Table with unnecessary spaces in column names]
    WHERE Product<>'MMP' AND Market<>'VA';

  • Joe Torre - Monday, July 23, 2018 4:46 PM


    SELECT [Line of Business], Product, Market
    FROM [Table with unnecessary spaces in column names]
    WHERE Product<>'MMP' AND Market<>'VA';

    Should be

    SELECT [Line of Business], Product, Market
    FROM [Table with unnecessary spaces in column names]
    WHERE Product<>'MMP' OR Market<>'VA';

    or

    SELECT [Line of Business], Product, Market
    FROM [Table with unnecessary spaces in column names]
    WHERE NOT (Product = 'MMP' AND Market = 'VA')

    _____________
    Code for TallyGenerator

  • Sergiy - Monday, July 23, 2018 7:31 PM

    Joe Torre - Monday, July 23, 2018 4:46 PM


    SELECT [Line of Business], Product, Market
    FROM [Table with unnecessary spaces in column names]
    WHERE Product<>'MMP' AND Market<>'VA';

    Should be

    SELECT [Line of Business], Product, Market
    FROM [Table with unnecessary spaces in column names]
    WHERE Product<>'MMP' OR Market<>'VA';

    or

    SELECT [Line of Business], Product, Market
    FROM [Table with unnecessary spaces in column names]
    WHERE NOT (Product = 'MMP' AND Market = 'VA')

    My preference is for the last of those 2.   It makes it quite clear just from reading the query, exactly what you are looking for.   The first one requires that you understand that it's a logical equivalent.

    Steve (aka sgmunson) 🙂 🙂 🙂
    Rent Servers for Income (picks and shovels strategy)

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply