Note that there are some explanatory texts on larger screens.

plurals
  1. POSelect rows from query with a distinct foreign key?
    primarykey
    data
    text
    <p>I am having trouble just now with yet another SQL problem. I really need to take some time out to learn this properly.</p> <p>Anyway I have this query that someone else wrote and it gets values from a few different tables.</p> <p>Now more than one item can have the same ProductID. So there may be 3 items returned all with the same ProductID but they have different descriptions etc.</p> <p>I want to select only 1 item per ProductID. I have tried using DISTINCT and group by but I get a lot of errors. Also this is for an ACCESS database.</p> <p>I think it's because of the logic used in the select query that is messing up my grouping.</p> <p>Here is the query (I have tried formatting it a little better, used an online tool but its still a huge mess)</p> <pre><code>SELECT tblproducts.productid, tblproducts.categorycode, tblproducts.scaletitle, tblproducts.picture, tblitems.cost, tblitems.modelnumber, tblitems.itemid, Iif([tblitems]![tradeapproved],Iif(([tblitems]![markup] / 100) &lt;&gt; 0,(Iif(([tblitems]![supplierdiscount] / 100) &lt;&gt; 0, [tblitems]![cost] - ([tblitems]![cost] * ([tblitems]![supplierdiscount] / 100)), [tblitems]![cost])) * ([tblitems]![markup] / 100), 0) + Iif(([tblitems]![supplierdiscount] / 100) &lt;&gt; 0, [tblitems]![cost] - ([tblitems]![cost] * ([tblitems]![supplierdiscount] / 100)), [tblitems]![cost]) + [tblitems]![tradeapprovedcost] + [tblitems]![shippingcost], Iif(([tblitems]![markup] / 100) &lt;&gt; 0,(Iif(([tblitems]![supplierdiscount] / 100) &lt;&gt; 0, [tblitems]![cost] - ([tblitems]![cost] * ([tblitems]![supplierdiscount] / 100)), [tblitems]![cost])) * ([tblitems]![markup] / 100), 0) + Iif(([tblitems]![supplierdiscount] / 100) &lt;&gt; 0, [tblitems]![cost] - ([tblitems]![cost] * ([tblitems]![supplierdiscount] / 100)), [tblitems]![cost]) + [tblitems]![shippingcost]) AS price FROM (tblitems INNER JOIN tblproducts ON tblitems.productid = tblproducts.productid) INNER JOIN tblsuppliers ON tblproducts.supplierid = tblsuppliers.supplierid WHERE tblproducts.categorycode = 'BS' AND tblitems.tradeapproved = 0 AND tblsuppliers.active = on AND tblitems.isaccessory = false ORDER BY Iif([tblitems]![tradeapproved],Iif(([tblitems]![markup] / 100) &lt;&gt; 0,(Iif(([tblitems]![supplierdiscount] / 100) &lt;&gt; 0, [tblitems]![cost] - ([tblitems]![cost] * ([tblitems]![supplierdiscount] / 100)), [tblitems]![cost])) * ([tblitems]![markup] / 100), 0) + Iif(([tblitems]![supplierdiscount] / 100) &lt;&gt; 0, [tblitems]![cost] - ([tblitems]![cost] * ([tblitems]![supplierdiscount] / 100)), [tblitems]![cost]) + [tblitems]![tradeapprovedcost] + [tblitems]![shippingcost], Iif(([tblitems]![markup] / 100) &lt;&gt; 0,(Iif(([tblitems]![supplierdiscount] / 100) &lt;&gt; 0, [tblitems]![cost] - ([tblitems]![cost] * ([tblitems]![supplierdiscount] / 100)), [tblitems]![cost])) * ([tblitems]![markup] / 100), 0) + Iif(([tblitems]![supplierdiscount] / 100) &lt;&gt; 0, [tblitems]![cost] - ([tblitems]![cost] * ([tblitems]![supplierdiscount] / 100)), [tblitems]![cost]) + [tblitems]![shippingcost]) </code></pre> <p>Can anyone post a quick fix for this? Thanks</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.
 

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