如何在分层查询中获取不同的元素列表?

发布于 2024-09-02 22:49:40 字数 1282 浏览 1 评论 0原文

我有一个数据库表,其中包含通过姓名、工作和城市标识的人员。我有第二个表,其中包含每个城市公司中每项工作的层次结构表示。

假设人员表中有 3 个人:

[name(PK),title,city]
Jim, Salesman, Houston
Jane, Associate Marketer, Chicago
Bill, Cashier, New York

我的工作表中有数千种工作类型/地点组合,下面是其中的一个示例。您可以看到层次关系,因为parent_title是标题的外键:

[title,city,pay,parent_title]
Salesman, Houston, $50000, CEO
Cashier, Houston, $25000
CEO, USA, $1000000
Associate Marketer, Chicago, $75000
Senior Marketer, Chicago, $125000

.....

我遇到的问题是我的Person表是一个复合键,所以我不知道如何构造start 我的查询的一部分,以便它从我指定的城市中的三个工作中的每一个开始。

我可以执行三个单独的查询来获取我想要的内容,但这不能很好地扩展。例如:

select * from jobs
start with city = (select city from people where name = 'Bill') and title = (select title from people where name = 'Bill')
connect by prior parent_title = title
UNION
select * from jobs
start with city = (select city from people where name = 'Jim') and title = (select title from people where name = 'Jim')
connect by prior parent_title = title
UNION
select * from jobs
start with city = (select city from people where name = 'Jane') and title = (select title from people where name = 'Jane')
connect by prior parent_title = title

我还能如何获得高于我指定的三个人的所有工作的不同列表(或者如果不可能的话,我可以用一个不同的列表来包装它)?

I have a database table, with people identified by a name, a job and a city. I have a second table that contains a hierarchical representation of every job in the company in every city.

Suppose I have 3 people in the people table:

[name(PK),title,city]
Jim, Salesman, Houston
Jane, Associate Marketer, Chicago
Bill, Cashier, New York

And I have thousands of job type/location combinations in the job table, a sample of which follow. You can see the hierarchical relationship since parent_title is a foreign key to title:

[title,city,pay,parent_title]
Salesman, Houston, $50000, CEO
Cashier, Houston, $25000
CEO, USA, $1000000
Associate Marketer, Chicago, $75000
Senior Marketer, Chicago, $125000

.....

The problem I'm having is that my Person table is a composite key, so I don't know how to structure the start with part of my query so that it starts with each of the three jobs in the cities I specified.

I can execute three separate queries to get what I want, but this doesn't scale well. e.g.:

select * from jobs
start with city = (select city from people where name = 'Bill') and title = (select title from people where name = 'Bill')
connect by prior parent_title = title
UNION
select * from jobs
start with city = (select city from people where name = 'Jim') and title = (select title from people where name = 'Jim')
connect by prior parent_title = title
UNION
select * from jobs
start with city = (select city from people where name = 'Jane') and title = (select title from people where name = 'Jane')
connect by prior parent_title = title

How else can I get a distinct list (or I could wrap it with a distinct if not possible) of all the jobs which are above the three people I specified?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初见终念 2024-09-09 22:49:40

请尝试这个。我没有测试过这个。

SELECT  distinct *
FROM    jobs
START   WITH ( city, title ) IN 
     ( SELECT city, title
       FROM   people
       WHERE  name IN ( 'Bill', 'Jim', 'Jane' )
     )
CONNECT BY PRIOR parent_title = title;

Please try this. I haven't tested this.

SELECT  distinct *
FROM    jobs
START   WITH ( city, title ) IN 
     ( SELECT city, title
       FROM   people
       WHERE  name IN ( 'Bill', 'Jim', 'Jane' )
     )
CONNECT BY PRIOR parent_title = title;
明媚如初 2024-09-09 22:49:40

这应该有效:

SQL> SELECT *
  2    FROM jobs
  3   START WITH (title, city) IN (SELECT title, city FROM people)
  4  CONNECT BY PRIOR parent_title = title;

TITLE              CITY           PAY PARENT_TITLE
------------------ ------- ---------- ------------
Associate Marketer Chicago       7500 
Salesman           Houston       5000 CEO
CEO                USA         100000 

this should work:

SQL> SELECT *
  2    FROM jobs
  3   START WITH (title, city) IN (SELECT title, city FROM people)
  4  CONNECT BY PRIOR parent_title = title;

TITLE              CITY           PAY PARENT_TITLE
------------------ ------- ---------- ------------
Associate Marketer Chicago       7500 
Salesman           Houston       5000 CEO
CEO                USA         100000 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文