• it looks like some inline strings/code that is being constructed in c#, which is then being executed, presumbly by LoadDatatable?

    i'm not sure where =\'" + dtg + "\' is coming from, but combining the two queries i think would look something like this:

    DECLARE @dtg varchar(10) --?=\'" + dtg + "\' ? what datatype?

    select

    ships.name,

    ais_data.mmsi,

    ships.country,

    ais_data.course,

    ais_data.sog,

    ais_data.heading,

    ais_data.latitude,

    ais_data.longitude

    from ships

    LEFT JOIN ais_data

    ON ships.mmsi=ais_data.mmsi

    INNER JOIN (select

    ais_data.mmsi,

    MAX(ais_data.mssisdtg1)

    from ais_data

    where ais_data.dtg=@dtg

    and ais_data.message BETWEEN 1 AND 3

    group by ais_data.mmsi

    )MyAlias

    ON ais_data.mmsi=MyAlias.mmsi

    and ais_data.mssisdtg1=MyAlias.mssisdtg1

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!