Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, since you said you want to learn this stuff:</p> <p>An <strong>inner join</strong> will connect Items to ProductId's but will result in a full set. So if you have 3 ProductIds and 1 Item you will get</p> <pre><code>ProdId ItemId Description 1 1 Handy Dandy Randy Sandy! 2 1 Easily Accessible personal grooming comb. 3 1 This item provides a man or woman with extra re... </code></pre> <p>So what you really want to do is get all the ItemIds:</p> <pre><code>select ItemId from Item_tbl </code></pre> <p>And then loop over each result, getting a single ProductId per Item:</p> <pre><code>select top 1 ProductId from Product_tbl where ItemId = 12345 </code></pre> <p>Now anyone who suggests a loop with SQL gets yelled down, and (usually) rightly so. But this is a tough query to make, since it's not something people usually do. </p> <p>You were along the right lines with group by. Group By says "consolidate all the rows that have distinct column X" where column X would be ItemId. So: <em>Give me one row per ItemId</em>. </p> <p>Now you have to <em>pick</em> a ProductId from those 3 Products with ItemId 1. The <strong>cheater</strong> way to do it is not to pick a ProductId at <em>random</em> but rather a productId that fits a particular aggregate function. The most common are min and max. </p> <pre><code>select ItemId, max(ProductId) from Itemtbl i inner join Producttbl p on i.itemid = p.itemId group by ItemId </code></pre> <p>This will get the largest ProductId for each ItemId. You can do the same to get the minimum.</p> <p>Now, what's trickier is finding a ProductId that fits a criteria - say the most recently updated. What you want to say is "select the ItemId, and the max(updatedDate), and then <em>pull the ProductId of that max updatded date along</em> - but that doesn't work in sql (dear god I wish it did though).</p> <p>This query will give bad results:</p> <pre><code>select ItemId, max(ProductId), max(updatdedDate) from Itemtbl i inner join Producttbl p on i.itemid = p.itemId group by ItemId </code></pre> <p>Because the max ProductId <strong>does not necessarily come from the row with the max updatedDate</strong>.</p> <p>Instead you have to write a query that does this:</p> <ol> <li>Selects the ItemId (e.g. 5), and the maxUpdated date (e.g. 5/5/2005)</li> <li>Goes back to the Products_tbl and finds the ProductId whose ItemId is 5 and updatedDate is 5/5/2005</li> </ol> <p>That query is left as an exercise. (<em>but there's a bug! what if two products have the same last updated date and the same ItemId!</em>)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload