Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First step to increase readability is to create a View for your tblItems that includes the fancy logic for Price, eg:</p> <p>View [vwItemsWithAdjustedCost]</p> <pre><code>SELECT ProductID, TradeApproved, IsAccessory, Cost, ModelNumber, ItemID, IIf( ( SupplierDiscount / 100 ) &lt;&gt; 0, Cost - ( Cost * ( SupplierDiscount / 100 ) ), Cost ) AS AdjustedCost FROM tblItems </code></pre> <p>View [vwItemsWithPrice]</p> <pre><code>SELECT ProductID, TradeApproved, IsAccessory, Cost, ModelNumber, ItemID, IIf( ( Markup / 100 ) &lt;&gt; 0, AdjustedCost * ( Markup / 100 ), 0 ) + AdjustedCost IIf( TradeApproved, TradeApprovedCost, 0 ) + ShippingCost AS Price FROM vwItemsWithAdjustedCost </code></pre> <p>Next you have to decide what the criteria is for picking one item out of the many that match the same ProductID, if 3 items have the same ID which one do you want to show!?</p> <p>As stated by Tom, an easy way is to just get the first (lowest) ID that matches, something like this:</p> <pre><code>SELECT P.ProductID, P.CategoryCode, P.ScaleTitle, P.Picture, IP.Cost, IP.ModelNumber, IP.ItemID, IP.Price FROM tblProducts P INNER JOIN ( SELECT ProductID, MIN( ItemID ) AS MinItemID FROM tblItems I GROUP BY ProductID ) S ON S.ProductID = P.ProductID INNER JOIN vwItemsWithPrice IP ON IP.ItemID = S.MinItemID WHERE P.CategoryCode = 'BS' AND IP.TradeApproved = 0 AND IP.IsAccessory = false ORDER BY IP.Price </code></pre> <p>This says for each ProductID, give me the first (lowest) ItemID from tblItems, and using that join to my view.</p> <p>Hope this helps!</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