2014-11-14 106 views
1

我有一個觀點具有SQL腳本:合併兩行條件SQL視圖

Select 
     a.iAssetId, 
     ac.eEventCode, 
     vm.dtUTCDateTime, 
     g.iGeofenceId, 
     g.sGeofenceName, 
     c.sCategoryName, 
     c.iCategoryId, 
     s.sSiteName, 
     s.iSiteId, 
     CASE WHEN ac.eEventCode = 6 THEN vm.dtUTCDateTime ELSE NULL END as EnterTime, 
     CASE WHEN ac.eEventCode = 7 THEN vm.dtUTCDateTime ELSE NULL END as ExitTime, 

CASE WHEN 
a.iAssetId = Lead(a.iAssetId) OVER (ORDER BY a.iAssetId) 
       AND g.iGeofenceId = Lead(g.iGeofenceId) OVER (ORDER BY a.iAssetId) 
       AND ac.eEventCode != Lead(ac.eEventCode) OVER (ORDER BY a.iAssetId) 
       THEN DATEDIFF(minute, vm.dtUTCDateTime, Lead(vm.dtUTCDateTime) OVER (ORDER BY a.iAssetId)) ELSE NULL END as Test 


    From AssetCommunicationSummary ac 
    Inner join VehicleMonitoringLog vm on vm.iVehicleMonitoringId = ac.iVehicleMonitoringId 
    Inner Join Geofences g on g.iGeofenceId = vm.iGeofenceId 
    Inner Join Assets a on a.iAssetId = ac.iAssetId 
    Inner Join Categories c on c.iCategoryId = a.iCategoryId 
    Inner Join Sites s on s.iSiteId = c.iSiteId 
    Where ac.eEventCode = 6 OR ac.eEventCode = 7 
    Group by 
     a.iAssetId, 
      ac.eEventCode, 
      vm.dtUTCDateTime, 
      g.iGeofenceId, 
      g.sGeofenceName, 
      c.sCategoryName, 
      c.iCategoryId, 
      s.sSiteName, 
      s.iSiteId 

我用鉛來計算分鐘,領先的基於條件的行時間differenc。

我需要現在基於條件合併領先的行和當前行。

有沒有可能的方法來做到這一點?

目標是讓EnterTime和ExitTime在列旁邊的同一行中添加時間差。

我的結果是這樣的:

enter image description here

+1

在'MAX()'函數中放置列'EnterTime','ExitTime'和'Test',並由其餘列組成列。 – 2014-11-14 23:30:40

+0

這個的任何例子?我必須合併AssetId相同的roes,geofence是相同的,事件是6和7. – 2014-11-14 23:36:50

+0

您是否必須在結果中實際顯示事件代碼?事件代碼似乎已經進入和退出時間已經隱含。如果您不必在結果中顯示事件代碼,則只要您將其從組中移除並選擇,就會看到您的查詢已經做到了您想要的。 – Daileyo 2014-11-17 19:35:14

回答

1

如果您的eventcode始終是6和7,那麼您可以在連接本身中使用該子句連接兩次。我想我已經在下面正確地加入了其餘的架構,但是如果沒有,您可以調整它以適應。

Select 
    a.iAssetId, 
    vmEnter.dtUTCDateTime, 
    g.iGeofenceId, 
    g.sGeofenceName, 
    c.sCategoryName, 
    c.iCategoryId, 
    s.sSiteName, 
    s.iSiteId, 
    vmEnter.dtUTCDateTime as EnterTime, 
    vmExit.dtUTCDateTime as ExitTime, 
    DATEDIFF(minute, vmEnter.dtUTCDateTime, vmExit.dtUTCDateTime) as ExitTime, 
From Sites s 
    Inner Join Categories c on s.iSiteId = c.iSiteId 
    Inner Join Assets a on c.iCategoryId = a.iCategoryId 
    Inner Join AssetCommunicationSummary acEnter on a.iAssetId = acEnter.iAssetId and acEnter.eEventCode = 6 
    Inner Join VehicleMonitoringLog vmEnter on vmEnter.iVehicleMonitoringId = acEnter.iVehicleMonitoringId 
    Inner Join AssetCommunicationSummary acExit on a.iAssetId = acExit.iAssetId and acExit.eEventCode = 7 
    Inner Join VehicleMonitoringLog vmExit on vmExit.iVehicleMonitoringId = acExit.iVehicleMonitoringId 
    Inner Join Geofences g on g.iGeofenceId = vmEnter.iGeofenceId 
+0

謝謝夥伴。這是最簡單的解決方案。 – 2014-11-22 20:39:30

1

您可以使用此DDL來測試,看看到底是怎麼回事的想法。這是複製和粘貼就緒,如果你想看到不同的時間,請確保你插入每條記錄之前等待。

Create table testing 
(
    Id int , 
    Enter DateTime, 
    Exitt DateTime, 
    Eventt int, 
    GeoCode int 
) 

insert into testing values (1, GETDATE(),null,6,10) 
insert into testing values (1, null,GETDATE(),7,10) 

insert into testing values (1, GETDATE(),null,6,11) 
insert into testing values (1, null,GETDATE(),7,11) 

insert into testing values (2, GETDATE(),null,6,10) 
insert into testing values (2, null,GETDATE(),7,10) 

create table #temp1 
(
    Id int, EnterTime datetime, GeoCode int 
) 

create table #temp2 
(
    Id int, ExitTime datetime, GeoCode int 
) 

insert into #temp1 
Select Id, MAX(Enter), GeoCode from testing where Eventt = 6 group by Id,GeoCode 

insert into #temp2 
Select Id, MAX(Exitt),GeoCode from testing where Eventt = 7 group by Id,GeoCode 

Select t1.Id, t1.EnterTime,t2.ExitTime, t1.GeoCode, DATEDIFF(ss,t1.EnterTime,t2.ExitTime) 
from #temp1 t1 
inner join #temp2 t2 on t2.Id = t1.Id 
        and t1.GeoCode = t2.GeoCode  

這基本上是僞代碼,所以你需要修改,但你需要的一切都在這裏。

1

我也會想這EVENTCODE = 6點的手段,多數民衆贊成在進氣時間

如果是你的兩個數據巴黎不要太大意義的退出時間是進氣時間之前,

查詢僅佔下方如果事件代碼6 =吸入時間 以及退出時間應該在天氣過後的時間。 查詢基於您提供的輸出而不是查看查詢。

如果做一個選擇*您視圖表給出了您的輸出然後用yourviewstablename取代vw_table

有在sqlfiddle的timedif空值,因爲

  • 有由assetid只有一個實例2
  • 由assetid 4和6具有出口時間entertimes前發生

SQLFIDDLE

select 
      v1.iAssetid, 
      v1.EnterTime, 
      v2.ExitTime, 
      datediff(minute, v1.Entertime, v2.Exittime) timedif 


     from vw_table v1 
      left join vw_table v2 on 
      v1.iAssetid= v2.iAssetid 
      and v1.sCategoryNamea = v2.sCategoryNamea 
      and v2.eEventcode = 7 
      and v2.dtUTCDatetime >= v1.dtUTCDatetime 


     where 
      v1.eEventcode = 6 
-1

試試這個

SELECT iAssetid, 
     iGeoFenceId, 
     iGeoFenceName, 
     sCategoryNamea, 
     iCategoryid, 
     sSiteName, 
     Max(EnterTime) As EnterTime, 
     Min(ExitTime) As ExitTime, 
     Datediff(minute, Max(EnterTime), Min(ExitTime)) As Timediff 
FROM #vw_Table 
GROUP BY iAssetid, 
      iGeoFenceId, 
      iGeoFenceName, 
      sCategoryNamea, 
      iCategoryid, 
      sSiteName 
0

您可以通過添加ROW_NUMBER將它們合併兩個結果集,然後在該加入。像

SELECT DISTINCT tbl1.col1, tbl2.col2 
FROM 
    (SELECT FirstName AS col1, ROW_NUMBER() OVER (ORDER BY FirstName) Number FROM dbo.UBUser) tbl1 
INNER JOIN 
    (SELECT LastName AS col2, ROW_NUMBER() OVER (ORDER BY LastName) Number FROM dbo.UBUser) tbl2 
ON tbl1.Number = tbl2.Number 

這樣,您就能夠有EnterTime和EXITTIME在與列在它旁邊時差同一行。