tags:

views:

42

answers:

1

I have an sql statement that gets repair orders and repair visits, howver i only want one particualr visit being pulled out but its pulling them all out and then duplicating the parts ordered, and i dont know what im doing wrong im sure there is really simple way round this but i dont know what is wrong with me today just cant see the obvious i dont think.

Below example data i just want to be able to see engineer 1 and the parts ordered.

Tables used are

  • Tbl_property - just to get address etc
  • tbl_repair_order - order raised
  • tbl_repair_visit - all visits logged again order, this where both engineer names stored
  • tbl_partorder - where parts are linked to order

Example data:

  • Orderid: 123 - Engineer1 - Part 1
  • Orderid: 123 - Engineer1 - Part 2
  • Orderid: 123 - Engineer2 - part 1
  • Orderid: 123 - Engineer2 - part 2

    SELECT dbo.tbl_repair_order.CONTRACT, dbo.tbl_property.PROPADDRESS, dbo.tbl_property.PROPNUMBER, dbo.tbl_property.FULLADDRESS, dbo.tbl_repair_order.ORDERID, dbo.tbl_repair_order.THEIRREF, dbo.tbl_repair_order.ORIGIN, dbo.tbl_repair_order.RAISEDDATE AS JOBRAISED, dbo.tbl_repair_order.ENGINEER AS ENG1, dbo.tbl_repair_visit.ENGINEER, dbo.tbl_partorder.EXSUPPLIERORDERID, dbo.tbl_partorder.SUPPLIER, dbo.tbl_partorder.RAISED AS PARTRAISED, dbo.tbl_partorder.CODE, dbo.tbl_partorder.ITEM, dbo.tbl_partorder.UNITCOST, dbo.tbl_partorder.QUANTITY, dbo.tbl_partorder.COST, dbo.tbl_partorder.EXPECTED, dbo.tbl_repair_visit.Status FROM dbo.tbl_property INNER JOIN dbo.tbl_repair_order ON dbo.tbl_property.PROPREF = dbo.tbl_repair_order.PROPREF INNER JOIN dbo.tbl_partorder ON dbo.tbl_repair_order.ORDERID = dbo.tbl_partorder.ORDERID LEFT OUTER JOIN dbo.tbl_repair_visit ON dbo.tbl_partorder.ORDERID = dbo.tbl_repair_visit.ORDERID WHERE (dbo.tbl_repair_order.CONTRACT = 'HOM') AND (dbo.tbl_repair_order.ORDERID = 4342) AND (dbo.tbl_repair_visit.Status = 'Parts Req')

+1  A: 

This is becuase the order has the parts for both engineer ids, do you want it for both or not?

What you can try is to use a sub select for table to get distinct orderid and part id in the joins

astander
no i dont want both the engineer details just the one from the visit table where the status was set to parts req as this is the negineer that ordered the parts?
Please provide the table structures.
astander
This is how they are linked, is that what you meanFROM dbo.tbl_property INNER JOIN dbo.tbl_repair_order ON dbo.tbl_property.PROPREF = dbo.tbl_repair_order.PROPREF INNER JOIN dbo.tbl_partorder ON dbo.tbl_repair_order.ORDERID = dbo.tbl_partorder.ORDERID LEFT OUTER JOIN dbo.tbl_repair_visit ON dbo.tbl_partorder.ORDERID = dbo.tbl_repair_visit.ORDERID
the visit can be linked on the order table or the part table as orderid is in both i have played with few different ways linking
I would need to see what the table structure look like, but from the given data provided, the specific order id would bring back rows for engineer1 part1 and 2 and engineer2 part1 and 2, which would seem like duplicates, unless this is what is required. you need to specify the engineerID aswell
astander
Oh i think i found the issue, both visits are marked as parts req!!!!! Will have find another way round this now!!!!
Advice: show the contents of the tables themselves.
Dems
not sure what you mean by that sorry?
what are the fields in the tables, and examples of the data itself? Not just the sql-query, the table definition and example data...
Dems