mercredi 28 octobre 2015

SQLite Duplicates: unique situation?

This query get data for a report. Normally there is
1 item per po_id, and one po_id per box and tracking number.
Sometimes the situation arises where multiple items are on a PO record, and in this instance there is an item that requires 2 boxes to ship = 3 tracking numbers (tracking records).
Right now its returning double of everything - 6 records when I only want 3.
Here's a link to the small Sqlite DB or a screenshot of the tables laid out.
I've looked at many examples of Sqlite dupe fixes here but have not been able to successfully apply them to my problem. Thanks.

Here is the query as I found it:

SELECT    
    it.item_name AS item_name,  
    it.box_type AS box_type,  
    it.box_id AS box_id,  
    tr.item_id AS item_id,  
    tr.tracking_number AS tracking_number,  
    tr.delivery_date_time AS delivery_date_time,  
    po.po_id AS po_id,  
    po.farm_name AS farm_name,  
    po.farm_state AS farm_state,  
    po.farm_country AS farm_country,  
    po.shipper_id AS shipper_id,  
    po.shipper_name AS shipper_name,  
    po.service_id AS service_id,  
    po.service_name AS service_name,  
    po.ship_cost AS ship_cost,  
    po.delivery_state AS delivery_state,  
    po.delivery_zip_code AS delivery_zip_code,  
    pr.product_group AS product_group,  
    pr.product_name AS product_name,  
    tti.total_trackings  
FROM tracking_info tr, item_info it, po_info po, product_info  pr, total_tracking_info tti  
WHERE  
    tr.item_id = it.item_id AND  
    it.po_id = pr.po_id AND  
    it.product_id = pr.product_id AND  
    pr.po_id = po.po_id AND  
    tti.po_id=po.po_id 

Aucun commentaire:

Enregistrer un commentaire