Query Help: A view that totals some groups

非 Y 不嫁゛ 提交于 2019-12-11 06:24:19

问题


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!


回答1:


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



来源:https://stackoverflow.com/questions/8056845/query-help-a-view-that-totals-some-groups

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!