2017-02-27 68 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