开发者

Do not want to include Case alias in group by clause

SELECT SUM (e.quantity) AS quantity
  ,e.tran_id
  ,e.SEQUENCE
  ,MAX (e.veh) AS veh
  ,a.DATE AS DATE
  ,a.aloc_sequence
  ,CASE
     WHEN a.currency = 'USD' THEN e.COST
     ELSE e.COST * getrate ('ABC'
                           ,a.currency
                           ,a.DATE
                           )
   END AS cost
FROM   execution e
  ,vehicles v
  ,allocation a
WHERE    e.vehicle = v.vehicle
AND      a.part_id = e.part_id
AND      a.aloc_sequence = e.aloc_sequence
GROUP BY e.tran_id
    ,e.SEQUENCE
    ,date
    ,a.aloc_sequence

I dont want to include the cost (that has been calculated using case staement in SELECT) in group by clause ,please help me . I 开发者_开发问答am using oracle as rdbms


When you use a group by, you are essentially consolidating many rows into one. You either GROUP BY a column, which guarantees that the value is the same for every row in the group, or you aggregate the values is some way (min, max, avg etc)

If you don't specify a column in the group by, there is ambiguity about which value to return (since they aren't in the group by they could be different and you need to be explicit about which one you want)

You could wrap an aggregate around COST i.e. SUM(cost) if you want the sum, or MAX(cost) if they are always going to be the same from row to row (although why not just group by then?)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜