Need Help with Advanced SQL Query

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default Need Help with Advanced SQL Query

    It's advanced for me, at least. I have three tables that I need to use:

    Product (product_id and product_title are the fields)

    shipRegion (shipRegion_ID)

    product_shipRegion_shipCharge (product_id, shipRegion_ID,
    primaryShipCharge, secondaryShipCharge)

    What I am trying to do is create a query that will look for two things:

    1. Any product that is listed in the product_shipRegion_shipCharge table that
    has a primary or secondary ship charge of $0.00.

    That part is easy:

    SELECT DISTINCT p.product_id, p.product_title
    FROM product p
    INNER JOIN product_shipRegion_shipCharge pss ON p.product_id = pss.product_id
    AND (pss.primaryShipCharge IS NULL OR pss.primaryShipCharge = 0 OR
    pss.secondaryShipCharge IS NULL OR pss.secondaryShipCharge = 0)
    WHERE p.display = 1
    AND p.price > 0



    It's this next part that's tricky for me:

    2. Get the product_id and product_title (from the product table) that does NOT
    have any entry in the product_shipRegion_shipCharge table.

    I'm guessing that I need to include some kind of LEFT JOIN in the above query,
    but I have no idea how to do it.

    Anyone?

    Josh

    wjs Guest

  2. Similar Questions and Discussions

    1. Where to find: Advanced Query Wizard
      you can find a link to the Advanced Query Wizard on the Adobe Exchange, but under the given link there is no more any software to find. Does anyone...
    2. advanced delete query sql
      Hi, I have two tables: dev table: dev_id volgnr type 1 B-05-1001 1(pc) 2 B-05-3001...
    3. Query of Queries on query New type query
      In CF5 we have a page that creates a query, using queryNew and querySetCell and the like, we then used dbtype="query" and gave it's name so we could...
    4. ok advanced for me, how do I...
      http://www.azcdr.com/projects/wuwac5/index.htm In the larger blue panel, I'd like to have a flip down door and reveal a host of other controls....
    5. advanced SQL
      Until recently, I thought MySQL, and SQL in general was no problem....doing simple INSERTS, DELETES, and UPDATES;...then I heard about triggers,...
  3. #2

    Default Re: Need Help with Advanced SQL Query

    As long as you don't want it in a single query, this should do it.

    SELECT DISTINCT p.product_id, p.product_title
    FROM product p
    WHERE NOT EXISTS
    (SELECT 1
    FROM product_shipRegion_shipCharge pss
    WHERE p.product_id = pss.product_id)

    If you want it all in a single query, then just UNION your query with this one.

    Phil

    paross1 Guest

  4. #3

    Default Re: Need Help with Advanced SQL Query

    Actually I think you may have better luck with using union. I have a similar
    query that i use that for and it works great. Make all the same fields
    selected, but put a NULL for the values that arent applicable. . .

    liquid One Guest

  5. #4

    Default Re: Need Help with Advanced SQL Query

    The UNION indeed works fine. What I was missing knowledge-wise was the "WHERE NOT EXISTS."

    Thanks to you both.

    Josh

    wjs Guest

  6. #5

    Default Re: Need Help with Advanced SQL Query

    You could also use a NOT IN, but that generally isn't as efficient as using
    EXISTS or NOT EXISTS.

    SELECT DISTINCT p.product_id, p.product_title
    FROM product p
    WHERE NOT IN
    (SELECT pss.product_id
    FROM product_shipRegion_shipCharge pss)

    Phil

    paross1 Guest

  7. #6

    Default Re: Need Help with Advanced SQL Query

    This should be what the query would look like using the left join:

    SELECT product_table.product_id, product_table.product_title
    FROM product_table
    LEFT OUTER JOIN product_shipRegion_shipCharge
    ON product_shipRegion_shipCharge.product_id = product_table.product_id
    WHERE product_shipRegion_shipCharge. product_id IS NULL

    SQL Server's query optimizer will probably turn the '...IN (subquery)' or
    '...exists (subquery)' into this kind of execution plan on its own - depending
    on your table structures.

    JaredJBlackburn Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139