【问题标题】:How to make a query with a collection in the select statement (not in the where clause)如何在 select 语句中使用集合进行查询(而不是在 where 子句中)
【发布时间】:2020-04-08 20:14:36
【问题描述】:

我有 2 张桌子。父表(parent)与子表是一对多的关系。 父表 (Pa_Projects_all) 和子表 pa_project_players 他们通过 person_id 加入

假设我有一个项目 (project_id = 1001)

Project Id Project Name      
1001       This is a project 

项目参与者有

查询应该是这样的

Select Project_Id, Person_id, Name
from Pa_projects_all a, Pa_project_players b
where a.person_id = b.person_id
and a.project_id = 1001;

预期的结果是

Project Id Person Id  Name
1001        500       John Smith
            501       Peter Carpenter
            502       Steve Sun

我认为列 Person Id 和 Name 应该是一个集合。

查询将进入 XML Publisher 的数据定义中,结果将出现在 Excel 中。

所以在 Excel 中 A 列 = 项目 ID,B 列 = 人员 ID,C 列 = 名称。 Excel 中的一行作为结果。 换句话说,每个项目 ID 一个 Excel 行。

这可能吗?

【问题讨论】:

    标签: sql oracle


    【解决方案1】:

    如果您想在 SQL*Plus 中拥有此报告,则可以使用 BREAK ON 语句。

    SQL> break on Project_Id
    SQL> Select Project_Id, Person_id, Name
      2  from Pa_projects_all a, Pa_project_players b
      3  where a.person_id = b.person_id
      4  and a.project_id = 1001;
    

    如果您想在任何工具中使用它,那么您可以使用解析函数ROW_NUMBER,如下所示:

    SELECT CASE WHEN RN = 1 THEN Project_Id END AS Project_Id, Person_id, Name FROM
    (Select Project_Id, Person_id, Name,
           ROW_NUMBER() OVER (PARTITION BY Project_Id ORDER BY Person_id) AS RN
    from Pa_projects_all a, Pa_project_players b
    where a.person_id = b.person_id
    and a.project_id = 1001)
    ORDER BY RN;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-19
      • 1970-01-01
      • 2021-11-16
      • 1970-01-01
      • 2021-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多