如何使用枢轴连接两个表并在一个表中组合两个字段

Posted

技术标签:

【中文标题】如何使用枢轴连接两个表并在一个表中组合两个字段【英文标题】:How to Join two table using pivot with combining two field in 1 table 【发布时间】:2016-04-27 18:55:48 【问题描述】:

我有两个这种格式的表

工作技能

     ID_Skill  Min_Job  Idea_Job   Max_Job
     =====================================
         1        0           0       1
         2        0           1       1
         7        1           1       1
         8        1           1       1

工作_教育

        Degree_Job  Field_Job  Min_Job   Idea_Job   Max_Job
        ===================================================
              7        37         0         0         1
              7        106        0         1         1
              13       37         1         1         1
              13       106        1         1         1

我想这样显示

  Criteria   1  2  7  8 [ 7  37 ] [ 7 106 ]  [13 37]   [13 106]
  =============================================================
  Min        0  0  1  1    1         1         0          0   
  Ideal      0  1  1  1    0         1         1          1
  Max        1  1  1  1    1         1         1          1 

我如何在 pivot 中实现这一点。如果您有其他方法,请提出建议。

【问题讨论】:

你使用什么数据库? SQL Server 2014 @SureshKoya 【参考方案1】:
declare @job_skill table (id_skill int,min_job int ,ideal_job int ,max_job int)
insert into @job_skill
VAlues
(         1 ,       0 ,          0 ,      1),
(         2 ,       0 ,          1 ,      1),
(         7 ,       1 ,          1 ,      1),
(         8 ,       1 ,          1 ,      1)


declare @Job_Education table (Degree_Job int,  Field_Job int,  Min_Job int,   Ideal_Job int,  Max_Job int)
insert into @job_education
values
(              7 ,       37  ,       0 ,        0 ,        1),
(              7  ,      106 ,       0 ,        1 ,        1),
(              13 ,      37  ,       1 ,        1 ,        1),
(              13 ,      106 ,       1 ,        1 ,        1)

/*
I want to display like this

  Criteria   1  2  7  8 [ 7  37 ] [ 7 106 ]  [13 37]   [13 106]
  =============================================================
  Min        0  0  1  1    1         1         0          0   
  Ideal      0  1  1  1    0         1         1          1
  Max        1  1  1  1    1         1         1          1 
*/

select *
from
(
select 1 as seq,'Min' as Criteria,cast(id_skill as char(10)) as COL, min_job jobval
from   @job_skill
union  
select 2,'Ideal',cast(id_skill as char(10)) as criteria,ideal_job 
from   @job_skill
union  
select 3,'Max',cast(id_skill as char(10)) as criteria, Max_job
from   @job_skill
union  
select 1, 'Min','(' + rtrim(cast(degree_job as char(4))) +  ' ' + rtrim(cast(field_job as char(4))) +')'  , min_job 
from    @job_education
union  
select 2, 'Ideal','(' + rtrim(cast(degree_job as char(4))) +  ' ' + rtrim(cast(field_job as char(4))) +')'  , ideal_job
from    @job_education
union  
select 3, 'Max','(' + rtrim(cast(degree_job as char(4))) +  ' ' + rtrim(cast(field_job as char(4))) +')'   , Max_job
from    @job_education
) s
pivot  (max(jobval) for col in([1],[2],[7],[8],[(7 37)],[(7 106)],[(13 37)],[(13 106)])) pvt
order by seq

【讨论】:

【参考方案2】:

这就是答案。我有我想要的结果。查询是动态的。

   Declare @Skill  NVARCHAR(MAX),
        @Degree_Field  NVARCHAR(MAX),
        @Experience  NVARCHAR(MAX),
        @query  NVARCHAR(MAX)


    select @Skill = 
                STUFF((
                        select SEQ
                        From
                        (
                            SELECT  SEQ = (',' + QUOTENAME(Skill_Name) )
                            From
                            (
                            select Skill_Meta.Skill_Name,Min_Job, Ideal_Job, Max_Job
                            FROM Job_Skill
                            Inner Join Skill_Meta On Skill_Meta.ID_SKL = Job_Skill.ID_SKL_Job
                            )SKL

                            UNPIVOT (Val FOR Col IN (Min_Job, Ideal_Job, Max_Job)) AS U
                        ) SKL

                        GROUP By SEQ 
                        FOR XML PATH(''), TYPE
                        ).value('.', 'NVARCHAR(MAX)') 
                        ,1,1,'') 

    select @Degree_Field = 
                STUFF((
                        select SEQ
                        From
                        (
                            SELECT  SEQ = (',' + QUOTENAME(cast(CONCAT(Degree_Job,' ', Field_Job) as varchar(10))) )
                            FROM Job_Education
                            UNPIVOT (Val FOR Col IN (Min_Job, Ideal_Job, Max_Job)) AS U
                        ) SKL

                        GROUP By SEQ 
                        FOR XML PATH(''), TYPE
                        ).value('.', 'NVARCHAR(MAX)') 
                        ,1,1,'') 
   select @Experience = 
                STUFF((
                        select SEQ
                        From
                        (
                            SELECT  SEQ = (',' + QUOTENAME(cast(Experience_Job as varchar(10))) )
                            FROM Job_Experience
                            UNPIVOT (Val FOR Col IN (Min_Job, Ideal_Job, Max_Job)) AS U
                        ) SKL

                        GROUP By SEQ 
                        FOR XML PATH(''), TYPE
                        ).value('.', 'NVARCHAR(MAX)') 
                        ,1,1,'')

    set @query = 
            ';With
            P1_Src As
            (
                SELECT ID_Job,Skill_Name, Val, Col
                FROM Job_Skill
                Inner Join Skill_Meta On Skill_Meta.ID_SKL = Job_Skill.ID_SKL_JOB
                UNPIVOT (Val FOR Col IN (Min_Job, Ideal_Job, Max_Job)) AS U

            ), 
            P1 AS
            (
                SELECT ID_Job,Col,'+@Skill+'
                FROM P1_Src
                PIVOT (MAX(Val) FOR Skill_Name IN ('+@Skill+')) AS Pa
            ),

            P2_Src As
            (
                SELECT ID_Job,CONCAT(Degree_Job,'' '', Field_Job) as DegreeField, Val, Col
                FROM Job_Education
                UNPIVOT (Val FOR Col IN (Min_Job, Ideal_Job, Max_Job)) AS U
            ),

            P2 AS
            (
               SELECT ID_Job,Col,'+@Degree_Field+'
               FROM P2_Src
               PIVOT (MAX(Val) FOR DegreeField IN ('+@Degree_Field+')) AS Pb
            ),

            P3_Src As
            (
                SELECT ID_Job,Experience_Job, Val, Col
                FROM Job_Experience
                UNPIVOT (Val FOR Col IN (Min_Job, Ideal_Job, Max_Job)) AS U
            ),

            P3 AS
            (
               SELECT ID_Job,Col,'+@Experience+'
               FROM P3_Src
               PIVOT (MAX(Val) FOR Experience_Job IN ('+@Experience+')) AS Pc
            )



            select P1.ID_Job,P1.Col,P1.'+@Skill+',P2.'+@Degree_Field+',P3.'+@Experience+' 
            From P1
            Inner Join P2 On P1.ID_Job = P2.ID_Job and P1.Col = P2.Col
            Inner Join P3 On P1.ID_Job = P3.ID_Job and P1.Col = P3.Col'

exec sp_executesql @query

【讨论】:

以上是关于如何使用枢轴连接两个表并在一个表中组合两个字段的主要内容,如果未能解决你的问题,请参考以下文章

从mysql连接两个表并在一个JSP列表中显示信息[重复]

如何连接两个表并显示源?

SQL合并一个表中的字段

如何连接两个表并选择不匹配的列

Access - 比较两个表并在第一个表中更新或插入数据

SQL 合并一个表中的字段