我想使用SQL显示在不同集群上运行的作业

时间:2017-09-13 11:58:24

标签: sql postgresql

有两个表作业表和集群表。每个集群都有不同的子集群。

LIKE 主集群

cluster1     cluster2      cluster3
--------------------------------------
job1         job15           job 20
.              .
.
.
j14

第一个群集将有14个工作 第二组将有5个工作岗位 第3组将有5个工作

现在我想使用PostgreSQL显示在不同集群(包括子集群)上运行的作业。

我的表结构如下

CREATE TABLE clusterdata(  
   clusterid bigint NOT NULL,
   sourceclustername character varying NOT NULL,
   clustername character varying NOT NULL
);

CREATE TABLE  job(
   clusterid bigint NOT NULL,
   jobname character varying NOT NULL,  
   sourcecluster bigint
);

请提前帮助我...... Thanx

1 个答案:

答案 0 :(得分:0)

假设你的句子"第一个集群将有14个工作,第二个集群将有5个工作,第三个集群将有5个工作" 你可以使用这样的东西:

WITH BASE AS (
        SELECT * , ROW_NUMBER() OVER (PARTITION BY sourcecluster, clusterid ORDER BY clusterid, jobname) AS RN
        , DENSE_RANK() OVER (PARTITION BY sourcecluster  ORDER BY clusterid) * CASE WHEN sourcecluster=0 THEN 0 ELSE 1 END AS RN_GROUP
        FROM job )
      SELECT A.clusterid AS SOURCE_CLUSTER, A.jobname AS CLUSTER_1
      , B.jobname AS CLUSTER_2
      , C.jobname AS CLUSTER_3
      FROM BASE A      
      LEFT JOIN BASE B ON B.sourcecluster = A.clusterid AND B.RN_group=1 AND A.RN=B.RN   
      LEFT JOIN BASE C ON C.sourcecluster = A.clusterid AND C.RN_group=2 AND B.RN=C.RN
      WHERE A.sourcecluster=0
      ORDER BY A.clusterid, A.RN, B.RN;

输出:

    source_cluster  cluster_1   cluster_2   cluster_3
1   1               job1    job15   job20
2   1               job2    job16   job21
3   1               job3    job17   job22
4   1               job4    job18   NULL
5   1               job5    NULL    NULL
6   4               job41   NULL    NULL
7   4               job42   NULL    NULL
8   4               job43   NULL    NULL
9   4               job44   NULL    NULL
10  4               job45   NULL    NULL
11  5               job51   job55   NULL
12  5               job52   job56   NULL
13  5               job53   job57   NULL
14  5               job54   job58   NULL
15  5               job55   NULL    NULL

示例数据:

INSERT INTO clusterdata VALUES (1, '0', 'clust1');
INSERT INTO clusterdata VALUES (2, 'clust1', 'clust1.1');
INSERT INTO clusterdata VALUES (3, 'clust1', 'clust1.2');
INSERT INTO clusterdata VALUES (4, '0', 'clust2');
INSERT INTO clusterdata VALUES (5, '0', 'clust3');
INSERT INTO clusterdata VALUES (6, 'clust3', 'clust3.1');

INSERT INTO job VALUES (1,'job1',0);
INSERT INTO job VALUES (1,'job2',0);
INSERT INTO job VALUES (1,'job3',0);
INSERT INTO job VALUES (1,'job4',0);
INSERT INTO job VALUES (1,'job5',0);
INSERT INTO job VALUES (2,'job15',1);
INSERT INTO job VALUES (2,'job16',1);
INSERT INTO job VALUES (2,'job17',1);
INSERT INTO job VALUES (2,'job18',1);
INSERT INTO job VALUES (3,'job20',1);
INSERT INTO job VALUES (3,'job21',1);
INSERT INTO job VALUES (3,'job22',1);

INSERT INTO job VALUES (4,'job41',0);
INSERT INTO job VALUES (4,'job42',0);
INSERT INTO job VALUES (4,'job43',0);
INSERT INTO job VALUES (4,'job44',0);
INSERT INTO job VALUES (4,'job45',0);
INSERT INTO job VALUES (5,'job51',0);
INSERT INTO job VALUES (5,'job52',0);
INSERT INTO job VALUES (5,'job53',0);
INSERT INTO job VALUES (5,'job54',0);
INSERT INTO job VALUES (5,'job55',0);
INSERT INTO job VALUES (6,'job55',5);
INSERT INTO job VALUES (6,'job56',5);
INSERT INTO job VALUES (6,'job57',5);
INSERT INTO job VALUES (6,'job58',5);