【问题标题】:How to combine two rows into a single row in CTE如何在 CTE 中将两行合并为一行
【发布时间】:2018-12-18 11:41:05
【问题描述】:

我正在像这样的 CTE 中获取数据。

这里我为每个 ActivityId 获取两条记录,我想要实现的是两个从两行创建一条记录。从 r2=1 的行开始,我只需要 RDate 作为 Todate,ActualProgress 作为 endprogress,PlannedProgress 作为 endplannedprogress,因为这些是唯一会在两行上更改的值。

我在 CTE 中获取这些数据。

WITH CTE as(SELECT row_number() over (partition by pmaph.ActivityId order by date) r1, row_number() over (partition by pmaph.ActivityId order by date desc) r2,pma.PlanEndDate as PlanEndDate ,pma.PlanStartDate as PlanStartDate,pmaph.ActivityId,pmaph.ProjectMilestoneActivProgHist_Id as ProMileAvtiID,umd.UOM_Name as UomName, pmm.MilestoneName as MilestoneName,pma.ActivityName as ActivityName ,pmp.ProjectName as ProjectName, Replace((rtrim(ltrim(Convert(varchar(12),Cast(pmaph.Date as Datetime),106)))),' ','-') as RDate, isnull(pmaph.Actual_Progress,0) as ActualProgress,isnull(pmaph.Planned_Progress,0) as PlannedProgress FROM ProjectMilestoneActivityProgressHistory as pmaph left join dbo.PM_Project as pmp on pmaph.ProjectId=pmp.ProjectId left join dbo.PM_Activity as pma on pmaph.ActivityId=pma.ActivityId  left join dbo.PM_Milestone  as pmm on pmaph.MilestoneId=pmm.MilestoneId left join dbo.UOMDetail as umd on pma.UOM_Id=umd.UOM_Id where pmaph.Client_Id=@ClientId
)
select  * from CTE where r1=1 or r2=1

【问题讨论】:

  • 但如果我注意到promileavtiid 列也发生了变化。
  • 你也应该分享你的 CTE 代码。
  • 当 r1 r1(来自不同的表)时使用 ActivityID 的自联接,然后选择所需的列。可能需要使用简单的 WHERE 按活动过滤 1 行。
  • @DhruvJoshi 是的,每条记录的 promileavtid 都不同,因为数据来自具有自己的 id promileavtid 的历史表。一个 ActivityId 可以有很多行,但我只选择日期为最小值且日期为最大值的记录(2 条记录)。我正在编辑我的问题并包含 CTE 代码。
  • @EzLo 你能简单解释一下吗?

标签: sql-server common-table-expression


【解决方案1】:

您可以按 ActivityId 进行分组并包括一个 MAX(RDate) 和 MAX(ActualProgress),它应该只为每个 ActivityId 返回一行。

【讨论】:

    【解决方案2】:

    也许这可以给你一个提示。确保保持 SQL 的格式。

    ;WITH CTE as
    (
        SELECT 
            row_number() over (partition by pmaph.ActivityId order by date) r1, 
            row_number() over (partition by pmaph.ActivityId order by date desc) r2,
            pma.PlanEndDate as PlanEndDate ,
            pma.PlanStartDate as PlanStartDate,
            pmaph.ActivityId,
            pmaph.ProjectMilestoneActivProgHist_Id as ProMileAvtiID,
            umd.UOM_Name as UomName, 
            pmm.MilestoneName as MilestoneName,
            pma.ActivityName as ActivityName,
            pmp.ProjectName as ProjectName, 
            Replace((rtrim(ltrim(Convert(varchar(12),Cast(pmaph.Date as Datetime),106)))),' ','-') as RDate, 
            isnull(pmaph.Actual_Progress,0) as ActualProgress,
            isnull(pmaph.Planned_Progress,0) as PlannedProgress 
        FROM 
            ProjectMilestoneActivityProgressHistory as pmaph 
            left join dbo.PM_Project as pmp on pmaph.ProjectId=pmp.ProjectId 
            left join dbo.PM_Activity as pma on pmaph.ActivityId=pma.ActivityId  
            left join dbo.PM_Milestone  as pmm on pmaph.MilestoneId=pmm.MilestoneId 
            left join dbo.UOMDetail as umd on pma.UOM_Id=umd.UOM_Id 
        where 
            pmaph.Client_Id=@ClientId
    )
    SELECT
        C1.*, -- Now you have all data in one row (by Activity), select whichever columns you want
        C2.*
    FROM
        CTE AS C1
        INNER JOIN CTE AS C2 ON 
            C1.ActivityId = C2.ActivityId AND
            C2.r2 = 1 -- ... and join against the other row
    WHERE
        C1.r1 = 1 -- pick 1 row by Activity
    

    【讨论】:

      猜你喜欢
      • 2023-02-19
      • 2022-12-18
      • 1970-01-01
      • 2015-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多