【问题标题】:SQL - How to perform a nested SELECT with multiple queries from the same table?SQL - 如何使用来自同一个表的多个查询执行嵌套 SELECT?
【发布时间】:2019-07-16 16:03:21
【问题描述】:

我编写了一个嵌套选择查询,它工作正常。

select user_id, transaction_id, t_timestamp 
from (
    select *, row_number() over (partition by (user_id || '*' || transaction_id) order by f_time asc) rownum
    from transactionMetricsView
    where metric_1 in (select metric_value 
                       from main.metrics 
                       where metric_key = 'metric_type_1')
)
where rownum = 1

main.metrics 表也有一个m_date 字段,所以我需要检查日期是否在给定范围内。 但我不确定如何正确实施。

我认为它应该类似于下面的查询,但现在更像是一个伪代码。

select user_id, transaction_id, t_timestamp 
from (
     select *, row_number() over (partition by (user_id || '*' || transaction_id) order by f_time asc) rownum
     from transactionMetricsView
     where metric_1 in (select metric_value 
                        from main.metrics 
                        where metric_key = 'metric_type_1') 
         and (select m_date 
              from main.metrics between $startDate and $endDate)
)
where rownum = 1

【问题讨论】:

  • 样本数据和期望的结果会有所帮助。适当的数据库标签也是如此。
  • 标记适当的数据库名称。还要添加示例数据和预期输出。

标签: sql postgresql select


【解决方案1】:

这可能有效:

 select user_id, transaction_id, t_timestamp from (select *, row_number() over (partition by (user_id || '*' || transaction_id) order by f_time asc) rownum
 from transactionMetricsView
 where metric_1 in (select metric_value 
                    from main.metrics 
                    where metric_key = 'metric_type_1'
                      and m_date between $startDate and $endDate
                    )
  and rownum = 1

【讨论】:

    猜你喜欢
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多