SQL - 将两个记录合并为一个

时间:2017-04-24 13:37:16

标签: sql

我有一个视图和表格。在视图中,我试图从相关作品集中获取作品集#和a,然后重新加入视图以获取相关作品集#。

我的查询如下:

SELECT DISTINCT  A.FOLIOID
                ,A.FOLIOTYPE
                ,A.NAME
                ,A.FOLIONUMBER
                ,B.FOLIOID AS SUBFOLIOID
                ,B.FOLIONUMBER AS SUBFOLIONUMBER
FROM VW_FOLIO AS A
INNER JOIN FOLIOREF B ON B.FOLIOPARENTID = A.FOLIOID
INNER JOIN VW_FOLIO C ON C.FOLIOID=B.FOLIOID 
WHERE A.FOLIONUMBER='2018-01-01'

它给了我这个结果

FOLIOID       FOLIOTYPE   NAME         FOLIONUMBER    SUBFOLIOID     SUBFOLIO
ad61e75a5bcc  DOORS       JOHN BROWN   2018-01-01     bfdbf0bc9f24   2018-03-01
ad61e75a5bcc  DOORS     JOHN BROWN   2018-01-01     fbf4370a4d46   2018-03-15

FOLIOREF表如下所示:

FOLIOREFID       FOLIOID       FOLIOPARENTID
1c489c11e8dd    bfdbf0bc9f24    ad61e75a5bcc
ece462200c59    fbf4370a4d46    ad61e75a5bcc

我认为问题是如何让第二个SUBFOLIO列在右栏而不是创建一个新行。

希望我足够清楚,有人可以帮助我。

****编辑帖子以显示我尝试做什么/得到以下*****

FOLIOID       FOLIOTYPE   NAME         FOLIONUMBER   SUBFOLIO1     SUBFOLIO2
ad61e75a5bcc  DOORS       JOHN BROWN   2018-01-01    2018-03-01   2018-03-15

1 个答案:

答案 0 :(得分:0)

请尝试以下操作:

SELECT DISTINCT  A.FOLIOID
                ,A.FOLIOTYPE
                ,A.NAME
                ,A.FOLIONUMBER
                ,B.FOLIOID AS SUBFOLIOID
                ,B.FOLIONUMBER AS SUBFOLIONUMBER
                ,NVL(D.FOLIOID,'') AS SUBFOLIOID
                ,NVL(D.FOLIONUMBER,'') AS SUBFOLIONUMBER                
FROM VW_FOLIO AS A
INNER JOIN FOLIOREF B ON B.FOLIOPARENTID = A.FOLIOID
INNER JOIN VW_FOLIO C ON C.FOLIOID=B.FOLIOID 
LEFT JOIN FOLIOREF D ON D.FOLIOPARENTID = A.FOLIOID AND D.FOLIOPARENTID <> B.FOLIOPARENTID
WHERE A.FOLIONUMBER='2018-01-01'