SQL查询连接表澄清

时间:2018-03-13 05:11:04

标签: sql sql-server

我正在尝试加入来自两个不同表格的列。

  • 第一个表格为TBWORKFLOWPROCESS,列名称为processname
  • 第二个表格为TBLSTAPP,列名称为isforreconsiderationisforamendment

我的查询是:

select 
    pro.processname, app.isforreconsideration, app.isforamendment 
from 
    TBWORKFLOWPROCESS pro 
left join 
    TBLSTAPP app on app.applicationtype = pro.workflowid 
where  
    sequenceno >= 10 and pro.workflowid = 1 
group by 
    pro.processname, app.isforreconsideration, app.isforamendment

输出:

processname                 isforreconsideration          isforamendment
------------------------------------------------------------------------
booked
booked,doc pending
cancelled
rejected

输出正确但我想要的是这个。

processname               
---------------
booked
booked,doc pending
cancelled
rejected
isforreconsideration
isforamendment

任何人都可以帮我吗?谢谢你们。

1 个答案:

答案 0 :(得分:0)

鉴于您提出的问题需要解释很多,并且基于我能理解的内容和许多假设,我认为您希望将相同条件下的两个属性(isforreconsideration和isforamendment)的值合并。在processname的属性下显示它们。

因此,在这里,我假设您需要一列下的所有数据。

在下面的代码中,我基本上只使用UNION关键字添加了两行,因此将其重命名为另一列的不良编码。

select 
    pro.processname
from 
    TBWORKFLOWPROCESS pro
    LEFT JOIN TBLSTAPP app on app.applicationtype = pro.workflowid
where
    pro.sequenceno >=10 
    and pro.workflowid = 1 

union

select
    app.isforreconsideration as 'processname'
from
    TBWORKFLOWPROCESS pro
    LEFT JOIN TBLSTAPP app on app.applicationtype = pro.workflowid
where
    sequenceno >=10 
    and pro.workflowid = 1

union

select app.isforamendment as 'processname'
from
    TBWORKFLOWPROCESS pro
    LEFT JOIN TBLSTAPP app on app.applicationtype = pro.workflowid
where
    sequenceno >=10 
    and pro.workflowid = 1

P.S。 :我非常确定这段代码不是很正确,我可能已经触及了太多的神经,但是如果你能给出数据类型,每列都有,你想要的是哪种数据的例子输出,许多其他人将能够提供帮助。