February 27, 2015 at 7:19 am
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
February 27, 2015 at 12:51 pm
Sir, Thanks for your advice!!!
Basically dtg is coming from a web form. I hard coded the dtg here. I applied the query, but it is giving me some error. I am not very expert in databases thats why I am asking for help. I will act upon your advice and one day I will also be able to help others.:-)
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='2015-02-27'
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
I think last two lines are giving following errors:
Msg 8155, Level 16, State 2, Line 1
No column was specified for column 2 of 'MyAlias'.
Msg 207, Level 16, State 1, Line 14
Invalid column name 'mssisdtg1'.
February 27, 2015 at 1:54 pm
i forgot the alias for the max:
MAX(ais_data.mssisdtg1) AS mssisdtg1
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) AS mssisdtg1 from ais_data where ais_data.dtg='2015-02-27'
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
February 27, 2015 at 9:14 pm
Thanks!!!:-)
February 27, 2015 at 9:32 pm
One more little information is required.
If we want to restrict some data in first portion of a query.
For example where ships.country='Malta'. Where we will do that?
February 27, 2015 at 9:35 pm
Yes done. Its done!!!
Viewing 6 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply