将两个SQL查询组合到一个整洁的表中

时间:2017-03-03 19:13:24

标签: sql sql-server sql-server-2008 reporting-services

让我先说一下我是初学者,所以请随意让查询看起来更合适。无论如何,我在尝试将所有两个查询组合起来时遇到了麻烦,我认为这比将两个查询合并到一个表中更容易。我尝试了一个联合,但这只是从第一个查询返回列。我想要完成的是使用TrainProgram将第一个查询与第二个查询连接起来。 UserLastName是我真正需要的一列,但它不知道通过。任何帮助将不胜感激。谢谢。这是第一个查询:

select 
    aa.TrainProgramID
  , aa.TrainProgram_OrganizationalUnitName
  , aa.TrainProgramMetaDataTypeName
  , aa.TrainProgramName
  , a.Activity_Name
  , a.Activity_Duration
  , a.Activity_UpdateDate
  , i.EntityName        as [MPN]
  , a.Activity_TypeName as [Activity_MDT]
  , g.Activity_ID
  , a.Activity_Description
  , a.ActivityPar_SNa  as [ActivitySName]
  , a.Activity_Order
  , a.ActivityPar_SID  as [ActivitySID]
from rawViews.MCD_DH_Activities a
  left join rawViews.MCD_DH_TrainingProgramsBasicData aa
    on a.Activity_EntityID = aa.TrainingProgramID
  inner join dh.MCD_DH_Activities g
    on g.Activity_ID = a.Activity_ID
  full join rawViews.MCD_DH_ActivityPrerequisites h
    on h.ActivityID = a.Activity_ID
  left join dh.MCD_DH_BaseEntities i
    on a.Activity_EntityID = i.EntityID
where a.Activity_VersionStatusID = 1

这是我的第二个问题:

select 
    b.TrainProgramID
  , c.uid
  , c.UserLastName
from dh.MCD_DH_UserTrainProAssoc a
  inner join rawViews.MCD_DH_TrainProgramsBasicData b
    on a.TrainProgramID = b.TrainProgramID
  inner join rawViews.MCD_DH_UBasicData c
    on a.uid = c.uid

1 个答案:

答案 0 :(得分:2)

我认为这就是你要找的东西:

select 
    aa.TrainProgramID
  , aa.TrainProgram_OrganizationalUnitName
  , aa.TrainProgramMetaDataTypeName
  , aa.TrainProgramName
  , c.uid
  , c.UserLastName
  , a.Activity_Name
  , a.Activity_Duration
  , a.Activity_UpdateDate
  , i.EntityName        as [MPN]
  , a.Activity_TypeName as [Activity_MDT]
  , g.Activity_ID
  , a.Activity_Description
  , a.ActivityPar_SNa  as [ActivitySName]
  , a.Activity_Order
  , a.ActivityPar_SID  as [ActivitySID]
from rawViews.MCD_DH_Activities a
  left join rawViews.MCD_DH_TrainingProgramsBasicData aa
    on a.Activity_EntityID = aa.TrainingProgramID
  left join dh.MCD_DH_UserTrainProAssoc utpa
    on aa.TrainProgramID = utpa.TrainProgramID
  left join rawViews.MCD_DH_UBasicData c
    on utpa.uid = c.uid
  inner join dh.MCD_DH_Activities g
    on g.Activity_ID = a.Activity_ID
  full join rawViews.MCD_DH_ActivityPrerequisites h
    on h.ActivityID = a.Activity_ID
  left join dh.MCD_DH_BaseEntities i
    on a.Activity_EntityID = i.EntityID
where a.Activity_VersionStatusID = 1