简体   繁体   English

如何比较两列并在Oracle中将最大的列作为新列返回

[英]How to compare two column and return the largest one as a new column in Oracle

Here is what I've tried: 这是我尝试过的:

SELECT
  cln.CLNID,
  MAX(cln.lastinputdate) clnlast ,
  MAX(subac.lastinputdate) sublast , 
  (CASE
      WHEN clnlast > sublast   
  THEN clnlast
      ELSE sublast 
  END) as lasttime
FROM
  IBROKER.cln cln
LEFT JOIN IBROKER.ClnSrvSubAc subac
ON
  cln.clnid=subac.clnid
GROUP BY
  cln.clnid,

But it ends up with a error "ORA-00904: "CLNLAST": invalid identifier" 但最终会出现错误“ ORA-00904:“ CLNLAST”:无效的标识符”

Please help. 请帮忙。

You can't reference an alias defined in your select list elsewhere in the select list. 你不能引用您定义的别名select列表中的其他地方select列表。 You'd need to use the actual column names. 您需要使用实际的列名。 You also don't need to use a case here-- Oracle provides a greatest function 您也不需要在这里使用case -Oracle提供了greatest功能

 greatest( max( cln.lastinputdate ), max( subac.lastinputdate ) )

You can not use column alias in this case. 在这种情况下,您不能使用列别名。 I think it should be: 我认为应该是:

SELECT
  cln.CLNID,
  MAX(cln.lastinputdate) clnlast ,
  MAX(subac.lastinputdate) sublast , 
  (CASE
      WHEN MAX(cln.lastinputdate)> MAX(subac.lastinputdate)
  THEN MAX(cln.lastinputdate)
      ELSE MAX(subac.lastinputdate)
  END) as lasttime
FROM
  IBROKER.cln cln
LEFT JOIN IBROKER.ClnSrvSubAc subac
ON
  cln.clnid=subac.clnid
GROUP BY
  cln.clnid;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM