【问题标题】:Which SQL function can be used as alternative of FIRST() of Informatica in SQL?哪个 SQL 函数可以用作 SQL 中 Informatica 的 FIRST() 的替代方法?
【发布时间】:2020-06-15 08:19:13
【问题描述】:

您好,我正在尝试将 Informatica Aggregator 转换转换为 SQL 查询,但我一直在寻找 SQL 中 Informatica 的 FIRST() 函数的替代方案。我使用了以下查询:

Select emp.*,
       first_value(ename)over (order by empid) ename1,
       first_value(ehobby)over (order by empid) ehobby1
From emp;

此查询为所有记录提供第一行,但我想按 empid 对其进行分组。谁能推荐一下?

【问题讨论】:

  • 您使用的是什么 dbms(MySQL、Oracle、MS SQL Server、PostgreSQL,...)?请添加为标签,因为答案可能特定于那个
  • 请提供样本数据和期望的结果。

标签: sql informatica


【解决方案1】:

此查询为所有记录提供第一行,但我想按 empid 对其进行分组。

我希望empid 在名为emp 的表中为distinct。但是,如果您有多行并且想要第一个值,那么您需要一个排序列。让我假设你有一个。在这种情况下:

select emp.*,
       first_value(ename) over (partition by empid order by <ordering col>) as ename1,
       first_value(ehobby) over (partition by empid order by <ordering col>) as ehobby1
from emp;

【讨论】:

    【解决方案2】:
    Select empid,
           (select top 1 ISNULL(ename,'') from emp where ename=obj.ename order by empid) as ename1
           (select top 1 ISNULL(ehobby,'') from emp where ehobby=obj.ehobby order by empid) as ehobby1 
    From emp as obj
    group by obj.empid,obj.ename1,obj.ehobby1
    

    我已经尝试了上述查询,请检查这可能会有所帮助,对于 Group by,您必须选择一个列名 如有错误请见谅 我是stackoverflow的新手。

    【讨论】:

    • 感谢您的建议先生,但我无法理解您要说的内容,您可以编辑我的帖子吗?
    猜你喜欢
    • 2021-09-14
    • 2016-04-08
    • 2020-09-27
    • 2021-01-04
    • 2012-06-30
    • 2012-08-05
    • 2016-06-08
    • 1970-01-01
    • 2021-04-12
    相关资源
    最近更新 更多