Frage

I'm looking for the correct way to structure the query for a view in order to include some totaling, etc. on the DB side (the view will be used by reporting systems).

Relevant Data Structure

+---------+      +---------+
|WORKORDER|      |WPLABOR  |
|---------|      |---------|
|WONUM    |+---->|WONUM    |
|...      |  +   |LABORHRS |
+---------+  |   |RATE     |
             |   +---------+
             |
             |   +---------+
             |   |WPITEM   |
             |   |---------|
             +-->|WONUM    |
             |   |ITEMQTY  |
             |   |UNITCOST |
             |   +---------+
             |
             |   +----------------+
             |   |LONGDESCRIPTION |
             |   |----------------|
             +-->|LDKEY           |
                 |LDTEXT          |
                 +----------------+

Goal

I would like to return the following:

  • Various items from Workorder
  • The sum of all labor costs (sum of each laborhrs*rate)
  • The sum of all items (sum of each itemqty*unitcost)
  • The LDText CLOB

I'm to the point where I have the relevant information displayed in the query, but am struggling with the totaling of the labor and item costs.

Query So Far

 SELECT 
    WORKORDER.WONUM, 
    WORKORDER.ACTLABHRS, 
    WORKORDER.LOCATION, 
    WORKORDER.STATUS, 
    WORKORDER.WO7,          -- Requester
    WORKORDER.WO8,          -- Extension
    WORKORDER.WO9,          -- Location
    WORKORDER.LEADCRAFT, 
    WORKORDER.WO11,         -- Extension
    WORKORDER.GLACCOUNT, 
    WORKORDER.WO10,         -- Contact
    WORKORDER.DESCRIPTION,  -- Short description
    WORKORDER.WO6,          -- Plant rearrangement (YORN / boolean value)
    LONGDESCRIPTION.LDTEXT,
    WPLABOR.LABORHRS,
    WPLABOR.RATE,
    WPITEM.ITEMQTY,
    WPITEM.UNITCOST
  FROM   
    MAXIMO.WORKORDER
  LEFT OUTER JOIN 
    MAXIMO.LONGDESCRIPTION
      ON WORKORDER.WONUM = CAST(LONGDESCRIPTION.LDKEY as varchar(22))

  LEFT OUTER JOIN
    MAXIMO.WPLABOR
      ON WORKORDER.WONUM = WPLABOR.WONUM

  LEFT OUTER JOIN
    MAXIMO.WPITEM
      ON WORKORDER.WONUM = WPITEM.WONUM

  WHERE  
    LONGDESCRIPTION.LDOWNERTABLE='WORKORDER' AND
    LONGDESCRIPTION.LDOWNERCOL = 'DESCRIPTION';

Thanks for any help you can give!

War es hilfreich?

Lösung

When you join tables together, you get one row for each possible combination. So if one work order has 3 labor rows and 4 item rows, a join of the three tables returns 12 rows.

One way to avoid that is to group by workorder in a subquery:

SELECT  WORKORDER.WONUM
,       LONGDESCRIPTION.LDTEXT
,       ... other columns ...
,       Labor.LaborCost
,       Item.ItemCost
FROM    MAXIMO.WORKORDER
LEFT JOIN 
        MAXIMO.LONGDESCRIPTION
ON      WORKORDER.WONUM = CAST(LONGDESCRIPTION.LDKEY as varchar(22))
LEFT JOIN
        (
        SELECT  WONUM
        ,       SUM(WPLABOR.LABORHRS * WPLABOR.RATE) as LaborCost
        FROM    MAXIMO.WPLABOR
        GROUP BY
                WOWNUM
        ) Labor        
ON      WORKORDER.WONUM = Labor.WONUM
LEFT JOIN
        (
        SELECT  WONUM
        ,       SUM(WPITEM.ITEMQTY * WPITEM.UNITCOST) as ItemCost
        FROM    MAXIMO.WPITEM
        GROUP BY
                WOWNUM
        ) Item
ON      WORKORDER.WONUM = Item.WONUM

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top