Sql 最大日期和在单独列上连接
有人能帮我解决这个问题吗?我需要用我的数据做两件事Sql 最大日期和在单独列上连接,sql,sql-server,Sql,Sql Server,有人能帮我解决这个问题吗?我需要用我的数据做两件事 我需要得到最长的日期 我需要连接Proj_Use列中的值 我的数据如下所示: 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
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 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;
我的结果应该如下所示:
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 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;
我可以使用以下代码获取最大日期:
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多分钟而没有生成结果
有没有人能告诉我,我是否正确地构造了这个查询,或者是否有更好的方法来获得我想要的结果
谢谢你的帮助 我期待着这样的事情:
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 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;
至少,进行连接的子查询没有到外部查询的关联子句(因此中间字符串可能很长)。可能还有其他问题。我期待着这样的事情:
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 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;
至少,进行连接的子查询没有到外部查询的关联子句(因此中间字符串可能很长)。可能还有其他问题。您缺少的一件大事是子查询中的
where
子句
使用
试试这个:
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演示:
返回:
+------------+---------+---------+----------+--------+-------------+
| 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 |
+------------+---------+---------+----------+--------+-------------+
您缺少的一件大事是子查询中的
where
子句
使用
试试这个:
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演示:
返回:
+------------+---------+---------+----------+--------+-------------+
| 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 |
+------------+---------+---------+----------+--------+-------------+
您正在使用哪个数据库?sql server 2014您正在使用哪个数据库?sql server 2014这对我很有用。我确实想向任何研究该解决方案的人指出,您在for XML语句中包含了一个额外的结束语。要使其正常工作,它应该如下所示:对于xml路径(“”),键入.value(‘.’,'nvarchar(max)’),1,1,“”),也感谢您提供了指向其他信息的链接。非常有帮助。@matt哎呀!至少在演示中是正确的。修正了答案中的拼写错误。这对我很有效。我确实想向任何研究该解决方案的人指出,您在for XML语句中包含了一个额外的结束语。要使其正常工作,它应该如下所示:对于xml路径(“”),键入.value(‘.’,'nvarchar(max)’),1,1,“”),也感谢您提供了指向其他信息的链接。非常有帮助。@matt哎呀!至少在演示中是正确的。修正了答案中的错误。