I am having trouble just now with yet another SQL problem. I really need to take some time out to learn this properly.
Anyway I have this query that someone else wrote and it gets values from a few different tables.
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.
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.
I think it's because of the logic used in the select query that is messing up my grouping.
Here is the query (I have tried formatting it a little better, used an online tool but its still a huge mess)
SELECT tblproducts.productid,
tblproducts.categorycode,
tblproducts.scaletitle,
tblproducts.picture,
tblitems.cost,
tblitems.modelnumber,
tblitems.itemid,
Iif([tblitems]![tradeapproved],Iif(([tblitems]![markup] / 100) <> 0,(Iif(([tblitems]![supplierdiscount] / 100) <> 0,
[tblitems]![cost] - ([tblitems]![cost] * ([tblitems]![supplierdiscount] / 100)),
[tblitems]![cost])) * ([tblitems]![markup] / 100),
0) + Iif(([tblitems]![supplierdiscount] / 100) <> 0,
[tblitems]![cost] - ([tblitems]![cost] * ([tblitems]![supplierdiscount] / 100)),
[tblitems]![cost]) + [tblitems]![tradeapprovedcost] + [tblitems]![shippingcost],
Iif(([tblitems]![markup] / 100) <> 0,(Iif(([tblitems]![supplierdiscount] / 100) <> 0,
[tblitems]![cost] - ([tblitems]![cost] * ([tblitems]![supplierdiscount] / 100)),
[tblitems]![cost])) * ([tblitems]![markup] / 100),
0) + Iif(([tblitems]![supplierdiscount] / 100) <> 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) <> 0,(Iif(([tblitems]![supplierdiscount] / 100) <> 0,
[tblitems]![cost] - ([tblitems]![cost] * ([tblitems]![supplierdiscount] / 100)),
[tblitems]![cost])) * ([tblitems]![markup] / 100),
0) + Iif(([tblitems]![supplierdiscount] / 100) <> 0,
[tblitems]![cost] - ([tblitems]![cost] * ([tblitems]![supplierdiscount] / 100)),
[tblitems]![cost]) + [tblitems]![tradeapprovedcost] + [tblitems]![shippingcost],
Iif(([tblitems]![markup] / 100) <> 0,(Iif(([tblitems]![supplierdiscount] / 100) <> 0,
[tblitems]![cost] - ([tblitems]![cost] * ([tblitems]![supplierdiscount] / 100)),
[tblitems]![cost])) * ([tblitems]![markup] / 100),
0) + Iif(([tblitems]![supplierdiscount] / 100) <> 0,
[tblitems]![cost] - ([tblitems]![cost] * ([tblitems]![supplierdiscount] / 100)),
[tblitems]![cost]) + [tblitems]![shippingcost])
Can anyone post a quick fix for this? Thanks