2017-06-14 54 views
0

我想知道是否有兩個CR.DestinationDepot記錄,然後將它們連接成像(例如。):KNI/KHN而不是它現在如何被連接起來。有任何想法嗎?謝謝。使用計數來確定發生和連接數據

enter image description here

select CR.DestinationCountry, 
mm.MovementRef, 
CR.DestinationDepot, 
case when count(CR.DestinationDepot) >= 2 then 'yes' else 'no' end as [statement], 
Case when (CR.DestinationCountry <> 'GB') and count(CR.DestinationDepot) >= 2 then CR.DestinationDepot+'/'+CR.DestinationDepot else CR.DestinationDepot end as [DestinationDepot] 
FROM dbo.MALExport AS ME 
    INNER JOIN dbo.movConLink AS MCL ON ME.ConsignmentReference = MCL.ConsignmentReference 
    INNER JOIN dbo.cgtRoute AS CR ON CR.RouteID = MCL.CMRRouteID 
    INNER JOIN dbo.movMovement AS MM ON MM.MovementRef = ME.MovementReference 
    group by cr.DestinationCountry, cr.DestinationDepot, mm.MovementRef 

回答

0

下面的代碼應該是你在找什麼

select CR.DestinationCountry, 
mm.MovementRef, 
CR.DestinationDepot, 
case when count(CR.DestinationDepot) >= 2 then 'yes' else 'no' end as [statement], 
Case when (CR.DestinationCountry <> 'GB') and count(CR.DestinationDepot) >= 2 then CR.DestinationDepot+'/'+CR.DestinationDepot else CR.DestinationDepot end as [DestinationDepot] 
,stuff((SELECT '/' + CR2.DestinationDepot' ' FROM dbo.cgtRout cr2 
    WHERE CR.DestinationCountry <> 'GB' and CR2.RouteID = MCL.CMRRouteID order by CR2.RouteID desc FOR XML PATH('')),1,1,'') AS [DestinationDepot2] 
FROM dbo.MALExport AS ME 
    INNER JOIN dbo.movConLink AS MCL ON ME.ConsignmentReference = MCL.ConsignmentReference 
    INNER JOIN dbo.cgtRoute AS CR ON CR.RouteID = MCL.CMRRouteID 
    INNER JOIN dbo.movMovement AS MM ON MM.MovementRef = ME.MovementReference 
    group by cr.DestinationCountry, cr.DestinationDepot, mm.MovementRef 
+0

嗨@RJ_不幸的是這代碼並沒有解決問題。不管怎樣,謝謝你 –