最大日期和连接在单独的列上

时间:2017-06-06 18:47:14

标签: sql sql-server

有人可以帮我解决这个问题吗?我需要用我的数据做两件事。

  1. 我需要获得最长日期
  2. 我需要连接Proj_Use列中的值
  3. 我的数据如下:

    Report_Date  |  Program  |  Project  |  Proj_Mgr  |  Region  |  Proj_Use
    
    6/5/2017  |  PG1  |  Prj01  |  Anne  |  East  |  PU1
    
    6/4/2017  |  PG1  |  Prj01  |  Anne  |  East  |  PU2
    
    6/3/2017  |  PG1  |  Prj01  |  Anne  |  East  |  PU3
    
    6/5/2017  |  PG2  |  Prj02  |  Monica  |  West  | PU4
    
    6/4/2017  |  PG2  |  Prj02  |  Monica  |  West  |  PU5
    
    6/3/2017  |  PG2  |  Prj02  |  Monica  |  West  |  PU6
    

    我的结果应如下所示:

    Report_Date Program Project Proj_MgrRegion  Proj_Use
    
    6/5/2017 | PG1 | Prj01 | Anne | East | PU1, PU2, PU3
    
    6/5/2017 | PG2 | Prj02 | Monica | West | PU4, PU5, PU6
    

    我可以使用以下代码获取最长日期:

        SELECT   t.[Report_Date]
            ,t.[Program]
            ,t.[Project]
            ,t.[Proj_Mgr]
            ,t.[Region]
            ,t.[Proj_Use]
        FROM project_table t
        JOIN
        (SELECT MAX([Report_Date]) as [Report Date], [Program]
            FROM project_table
            GROUP BY [Program]) max
            ON t.[Report_Date] = max.[Report Date]
            AND t.[Program] = max.[Program]
    

    我在论坛上搜索了一种连接方法,并找到了sql server的group_concat方法。我已经尝试将其添加到代码中并提出了这个:

        SELECT   t.[Report_Date]
            ,t.[Program]
            ,t.[Project]
            ,t.[Proj_Mgr]
            ,t.[Region]
            ,STUFF ((
                SELECT ',' + t.[Proj_Use]
                    FROM project_table
                    FOR XML PATH ('')), 1,1, '') AS [Project Use]
        FROM project_table t
        JOIN
        (SELECT MAX([Report_Date]) as [Report Date], [Program]
            FROM project_table
            GROUP BY [Program]) max
            ON t.[Report_Date] = max.[Report Date]
            AND t.[Program] = max.[Program]
    

    当我尝试使用我的真实数据运行时,查询运行超过20分钟而没有产生结果。

    如果我正确构建了此查询,或者是否有更好的方法来获取我正在寻找的结果,有人能告诉我吗?

    感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

我期待这样的事情:

SELECT pt.*,
       STUFF((SELECT ',' + pt2.[Proj_Use]
              FROM project_table pt2
              WHERE pt2.Program = pt.Program
              ORDER BY pt2.[Report Date]
              FOR XML PATH ('')
             ), 1, 1, '') AS [Projects Used]
FROM (SELECT pt.*,
             ROW_NUMBER() OVER (PARTITION BY Program ORDER BY [Report Date] DESC) as seqnum
      FROM project_table t
     ) pt
WHERE seqnum = 1;

至少,用于连接的子查询没有与外部查询相关的子句(因此中间字符串可能非常长)。可能还有其他问题。

答案 1 :(得分:0)

您遗失的一件大事是子查询中的where子句。

使用stuff() with select ... for xml path ('') method of string concatenation

试试这个:

select
    ReportDate = max(t.Report_Date)
  , t.Program
  , t.Project
  , t.Proj_Mgr
  , t.Region
  , [Project_Use] = stuff((
   select ',' + i.Proj_Use
   from project_table i
   where i.Program = t.Program
     and i.Project = t.Project
     and i.Proj_Mgr = t.Proj_Mgr
     and i.Region = t.Region
   for xml path(''), type).value('.','nvarchar(max)')
   , 1, 1, '') 
from project_table t
group by t.Program, t.Project, t.Proj_Mgr, t.Region

rextester演示:http://rextester.com/SEBKZD40038

返回:

+------------+---------+---------+----------+--------+-------------+
| ReportDate | Program | Project | Proj_Mgr | Region | Project_Use |
+------------+---------+---------+----------+--------+-------------+
| 2017-06-04 | pg1     | Prj01   | Anne     | East   | pu1,pu2,pu3 |
| 2017-06-04 | pg2     | Prj02   | Monica   | West   | pu4,pu5,pu6 |
+------------+---------+---------+----------+--------+-------------+