2017-02-27 41 views
1

我有这样的数据在我的表中的数据,我想获得如下数据。我尝试使用coalese,但有问题获取最新的数据。结合基于ID的最新数据

Key    Hazards    DateTime 
170021   Safety    2016-01-25 
170021   Concerns   2016-01-25 
170021   Abuse    2016-01-25 
252098   Financial   2016-10-28 
250606   Environmental  2016-10-26 
359287   food,utilities  2016-08-08 
409153   climate control  2016-06-24 
671881   None    2016-05-24 

Answer: Safety,Concerns,Abuse 

结果应如上所述。 iam试图做的是获取基于DateTime的最新值的密钥,并且如果该Key的多个可用记录将它们连接成一个字符串并返回。如果只有单个记录才能获得该记录。

+0

帮助我们来帮助你 - 请分享表的结构,并应produe你想要得到的结果的样本数据。 – Mureinik

+0

请问什么版本的SQL Server? – gbn

+1

你在找这样的东西吗? http://stackoverflow.com/questions/451415/simulating-group-concat-mysql-function-in-microsoft-sql-server-2005 –

回答

1

SQL服务器2016年,您可以使用STRING_AGG

SELECT STRING_AGG([Hazards], ',') 
FROM MyTable 
GROUP BY [Key] 
+0

喜欢这个方法,但我们不上的SQL 2016,因此不会为我们工作。 – user3038399

+0

我做了评论,问你.. – gbn

1

这是对你有帮助吗?

;WITH testtable([Key],Hazards,[DateTime])AS(
    SELECT '170021','Safety',CONVERT(DATE,'2016-01-25') UNION all 
    SELECT '170021','Concerns','2016-01-25' UNION all 
    SELECT '170021','Abuse','2016-01-25' UNION ALL 
    SELECT '170021','Abuse','2016-01-24' UNION ALL 
    SELECT '170021','Abuse2','2016-01-23' UNION ALL 
    SELECT '170021','Abuse3','2016-01-22' UNION ALL 
    SELECT '170021','Abuse4','2016-01-21' UNION all 
    SELECT '252098','Financial','2016-10-28' UNION all 
    SELECT '250606','Environmental','2016-10-26' UNION all 
    SELECT '359287','food,utilities','2016-08-08' UNION all 
    SELECT '409153','climate control','2016-06-24' UNION all 
    SELECT '671881','None','2016-05-24' 
) 
SELECT DISTINCT t.[Key],t.DateTime,STUFF(c.Hazards,1,1,'') AS Hazards FROM (
    SELECT *,RANK()OVER(PARTITION BY [Key] ORDER BY DateTime DESC) AS rn FROM testtable 
) AS t 
CROSS APPLY(SELECT ','+tt.Hazards FROM testtable AS tt WHERE tt.[Key]=t.[Key] AND DATEDIFF(d,tt.DateTime,t.DateTime)=0 FOR XML PATH('')) AS c(Hazards) 
WHERE rn=1 
 
Key DateTime Hazards 
------ ---------- -------------- 
170021 2016-01-25 Safety,Concerns,Abuse 
250606 2016-10-26 Environmental 
252098 2016-10-28 Financial 
359287 2016-08-08 food,utilities 
409153 2016-06-24 climate control 
671881 2016-05-24 None 
0

使用while循环在存储过程中

Declare @date_1 char(10) 
Declare @cnt int=1 
Declare @aktual_cnt int 
Declare @Haz nvarchar(max)='' 
Declare @Haz_1 nvarchar(max)='' 

select * 
into test_table_1 
from test_table -- Enter your table name 

set @date_1=(select top 1 DateTime from test_table_1 
order by Datetime) 

SET @aktual_cnt=(select count(*) from test_table_1 where [email protected]_1) 

while (@cnt<[email protected]_cnt) 
begin 
if (@cnt=1) 
    begin 
    set @Haz_1=(select top 1 Hazards from test_table_1 where [email protected]_1) 
    set @Haz = @Haz_1 
    delete from test_table_1 where [email protected]_1 and [email protected]_1 
    end 
else 
    begin 
    set @Haz_1=(select top 1 Hazards from test_table_1 where [email protected]_1) 
    set @Haz = @Haz + ','[email protected]_1 
    delete from test_table_1 where [email protected]_1 and [email protected]_1 
    end 


SET @[email protected]+1 


end 
drop table test_table_1 
select @Haz