Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / DB Engine / SQL Server / July 2008

Tip: Looking for answers? Try searching our database.

compare a sub-query to an outer query to filter max sighting code

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Janis Rough - 01 Jul 2008 00:15 GMT
The outer query gives me the last sighting for a rail car.  Now I
have the very last sighting, however within the last sighting results
there will be multiple records with that clm_id(sighting)   with
that date but different sighting codes for different transactions
within the sighting.  I want to filter the max sighting_code from the
records that are the last sighting to get the greatest code letters a-
z.

I don't know how to relate the inner query to the outer query so I put
the c.clm_id = c.clm_id  and c.sighting_date which gives me more than
one record.
The outer query gets the last sighting for a car and then
it has to compare those records with themselves to get the max
sighting code.

I'm not sure if I have the relationship of the inner query to the
outer query correct to get the two filters for max sighting date and
max sighting code?

tia,

SELECT c.l_e, c.car_mark, c.car_number, c.location_city,
c.location_state, MAX( c.sighting_date ) , e.classification,
c.railroad, s.city AS origin_city, s.state AS origin_state,
c.destination_city, c.destination_state, t.eta, c.sighting_code
FROM INTERNAL_REMARK r, INTERNAL_CAR_LEASE cl, INTERNAL_CODES e,
INTERNAL_CLM AS c
LEFT JOIN INTERNAL_TRIPS AS t ON c.trip_id = t.trip_id
JOIN INTERNAL_SPLC AS s ON t.origin_splc_id = s.splc_id
WHERE cl.lease_id =74
AND cl.remark_id = r.remark_id
AND r.car_mark = c.car_mark
AND c.sighting_code = e.code
AND c.car_number = r.car_number
AND c.sighting_code > (

SELECT c.sighting_code
FROM INTERNAL_CLM
WHERE c.clm_Id = c.clm_id
AND c.sighting_date = c.sighting_date
)
GROUP BY c.car_mark, c.car_number, c.l_e, c.location_city,
c.location_state, c.sighting_date, c.sighting_code, e.classification,
c.railroad, s.city, s.state, c.destination_city, c.destination_state,
t.eta
Plamen Ratchev - 01 Jul 2008 04:35 GMT
Try this:

SELECT c.l_e,
         c.car_mark,
         c.car_number,
         c.location_city,
         c.location_state,
         MAX(c.sighting_date),
         e.classification,
         c.railroad,
         s.city AS origin_city,
         s.state AS origin_state,
         c.destination_city,
         c.destination_state,
         t.eta,
         c.sighting_code
FROM INTERNAL_REMARK AS r
JOIN INTERNAL_CAR_LEASE AS cl
 ON cl.remark_id = r.remark_id
JOIN INTERNAL_CLM AS c
 ON r.car_mark = c.car_mark
AND r.car_number = c.car_number
JOIN INTERNAL_CODES AS e
 ON c.sighting_code = e.code
LEFT JOIN INTERNAL_TRIPS AS t
 ON c.trip_id = t.trip_id
JOIN INTERNAL_SPLC AS s
 ON t.origin_splc_id = s.splc_id
WHERE cl.lease_id =74
  AND c.sighting_code = (SELECT MAX(sighting_code)
                                   FROM INTERNAL_CLM AS I
                                   WHERE I.clm_Id = c.clm_id
                                      AND I.sighting_date =
c.sighting_date)
GROUP BY c.car_mark,
              c.car_number,
              c.l_e,
              c.location_city,
              c.location_state,
              c.sighting_date,
              c.sighting_code,
              e.classification,
              c.railroad,
              s.city,
              s.state,
              c.destination_city,
              c.destination_state,
              t.eta;

HTH,

Plamen Ratchev
http://www.SQLStudio.com
John Bell - 01 Jul 2008 22:14 GMT
This is the third time you have posted virtually the same issue. The reply I
posted last week would have solved this.

John

> The outer query gives me the last sighting for a rail car.  Now I
> have the very last sighting, however within the last sighting results
[quoted text clipped - 41 lines]
> c.railroad, s.city, s.state, c.destination_city, c.destination_state,
> t.eta
Janis Rough - 02 Jul 2008 01:55 GMT
> This is the third time you have posted virtually the same issue. The reply I
> posted last week would have solved this.
[quoted text clipped - 46 lines]
> > c.railroad, s.city, s.state, c.destination_city, c.destination_state,
> > t.eta

I missed seeing the response but now I am reading it and trying to
understand all those relationships including the ON AND construct.
I will test it on some data.  THANKS!
John Bell - 02 Jul 2008 20:11 GMT
>> This is the third time you have posted virtually the same issue. The
>> reply I
[quoted text clipped - 51 lines]
> understand all those relationships including the ON AND construct.
> I will test it on some data.  THANKS!

Hi Janis

FYI

Posting DDL and sample data (as inserts) along with expected output from
your statement is always useful for someone trying to formulate the reply,
it may mean slightly more effort on the posters part but running through the
code (in a test database) may mean your find the answer before even posting.
Overall it should mean that people understand the problem better which
should lead to a quicker solution.

You may want to try and simplify your problem and try it with fewer table
and columns and then build up the query back to the original problem.

John
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.