Sql 还有信息。因此,我需要创建一个新表,其中只包含消息1和3(在我给定的lat long范围内),但包含来自消息5的信息。(因为它不包括在1和3中)如果您在我的示例表中看到:第一行有两个空值。您将如何创建一个新表,其中的空值由从第二行获取信息(因为它们具有相同

Sql 还有信息。因此,我需要创建一个新表,其中只包含消息1和3(在我给定的lat long范围内),但包含来自消息5的信息。(因为它不包括在1和3中)如果您在我的示例表中看到:第一行有两个空值。您将如何创建一个新表,其中的空值由从第二行获取信息(因为它们具有相同,sql,database,sql-server-2008,Sql,Database,Sql Server 2008,还有信息。因此,我需要创建一个新表,其中只包含消息1和3(在我给定的lat long范围内),但包含来自消息5的信息。(因为它不包括在1和3中)如果您在我的示例表中看到:第一行有两个空值。您将如何创建一个新表,其中的空值由从第二行获取信息(因为它们具有相同的MMSI编号,因此是同一艘船)填充。这是一次性的事情吗(因此,一个快速且肮脏的解决方案就可以了)或者您打算在某种形式的应用程序中定期使用该解决方案?我对数据库有点陌生,我不确定这会是什么样子。此外,我没有更改现有表的权限。我猜您不必删除旧数据


还有信息。因此,我需要创建一个新表,其中只包含消息1和3(在我给定的lat long范围内),但包含来自消息5的信息。(因为它不包括在1和3中)如果您在我的示例表中看到:第一行有两个空值。您将如何创建一个新表,其中的空值由从第二行获取信息(因为它们具有相同的MMSI编号,因此是同一艘船)填充。这是一次性的事情吗(因此,一个快速且肮脏的解决方案就可以了)或者您打算在某种形式的应用程序中定期使用该解决方案?我对数据库有点陌生,我不确定这会是什么样子。此外,我没有更改现有表的权限。我猜您不必删除旧数据,但我假设您想移动而不是复制它。当我尝试此查询时,我在初始选择条件上得到红线,例如,如果我将鼠标悬停在M13.MMSI上,它会显示:无法删除多部分标识符“M13.MMSI”bound@criticalfix我想你加入的是一排人,因此,这可能无法按预期工作。谢谢@papatoob和@dpalm-我没有想到
TOP 1
会如何工作。我需要重新思考和编辑这个。我只是将初始的
内部连接
更改为
左侧外部连接
,以防某些船舶没有“type 5”消息。
Table Name: dbo.DecodedCSVMessages_Staging
Columns: MMSI, Message_ID, Time, Vessel_Name, Ship_Type, IMO, Dimension_to_Bow, Dimension_to_stern, Dimension_to_port, Dimension_to_starboard, Draught, Longitude, Latitude
Vessel_Name,  Ship_Type, IMO, 
Dimension_to_Bow, 
Dimension_to_stern,
Dimension_to_port, 
Dimension_to_starboard, 
Draught
Longitude, 
Latitude, 
Time, 
MMSI
Where Latitude > 55 and Latitude <85 and Longitude > 50 and Longitude < 141;
MMSI/ Message_ID /Time/Ship_type/Vessel_Name/Latitude/Longitude

21029300, 3, 2012-06-01, NULL, NULL, 56.528003, 85.233443

21029300, 5, 2012-07-01, 70, RIO_CUBAL, NULL, NULL

2109300, 1, 2012-08-01, NULL, NULL, 57.432345, 131.123343

2109300, 1, 2012-09-01, NULL, NULL, 62.432345, 121.123343

2109300, 1, 2012-09-02, NULL, NULL, 65.432345, 140.123343

21029300, 5, 2012-08-01, 70, RIO_CUBAL, NULL, NULL
21029300, 3, 2012-06-01, 70, RIO_CUBAL, 56.528003, 85.233443

2109300, 1, 2012-08-01, 70, RIO_CUBAL, 57.432345, 131.123343

2109300, 1, 2012-09-01, 70, RIO_CUBAL, 62.432345, 121.123343

2109300, 1, 2012-09-02, 70, RIO_CUBAL, 65.432345, 140.123343
SELECT DISTINCT M13.MMSI, M13.Message_ID, M13.Time, M13.Latitude, M13.Longitude,
M5.Vessel_Name, M5.Ship_Type, M5.IMO, M5.Dimension_to_Bow
M5.Dimension_to_stern, M5.Dimension_to_port, 
M5.Dimension_to_starboard, M5.Draught
FROM dbo.DecodedCSVMessages_Staging M13
JOIN (
SELECT MMSI, Time, Vessel_Name, Ship_Type, IMO, Dimension_to_Bow
Dimension_to_stern, Dimension_to_port, Dimension_to_starboard, 
Draught
FROM dbo.DecodedCSVMessages_Staging
WHERE Message_ID = 5
ORDER BY Time
) M5
ON M5.MMSI = M13.MMSI
WHERE M13.Message_ID IN (1, 3)
AND M13.Latitude > 55
AND M13.Latitude < 85
AND M13.Longitude > 50
AND M13.Longitude < 141
ORDER BY M13.Time
SELECT Messages.MMSI
    ,Messages.Message_ID
    ,Messages.TIME
    ,Type5Messages.Vessel_Name
    ,Type5Messages.Ship_Type
    ,Type5Messages.IMO
    ,Type5Messages.Dimension_to_Bow
    ,Type5Messages.Dimension_to_stern
    ,Type5Messages.Dimension_to_port
    ,Type5Messages.Dimension_to_starboard
    ,Type5Messages.Draught
    ,Messages.Longitude
    ,Messages.Latitude
INTO [DataBaseName].[dbo].[YourNewTableName]
FROM dbo.DecodedCSVMessages_Staging Messages
LEFT OUTER JOIN (
    SELECT DISTINCT MMSI
        ,Vessel_Name
        ,Ship_Type
        ,IMO
        ,Dimension_to_Bow
        ,Dimension_to_stern
        ,Dimension_to_port
        ,Dimension_to_starboard
    FROM dbo.DecodedCSVMessages_Staging
    WHERE Messages.Message_ID = 5
    ) Type5Messages
    ON Messages.MMSI = Type5Messages.MMSI
WHERE Messages.Message_ID IN (1,3)
    AND Messages.Latitude > 55
    AND Messages.Latitude < 85
    AND Messages.Longitude > 50
    AND Messages.Longitude < 141;