Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sort order

Status
Not open for further replies.

EPNICO

MIS
Jun 14, 2001
45
US
Hello Everyone:

Here is my sql query, I'm MS SQL 7.0 accessing a sybase database. I need the result of my query to be by location_number but anytime I try to place the order by command SQL does not like it. Any ideas as to how I can get the resulting set of the union by location number.

Thanks.

DECLARE @clave int;
set @clave = "40003227";

SELECT
d.insured_loc_name,
d.insured_loc_street1,
d.insured_loc_street2,
d.insured_loc_city +', '+ d.insured_loc_state + ' '+ d.insured_loc_zip,
b.location_number,
c.insured_name

FROM wcps..wcps.policy_named_insured as a,
wcps..wcps.policy_insured_location as b,
wcps..wcps.insured_business as c,
wcps..wcps.insured_location as d


WHERE
( a.insured_business_id = d.insured_business_id ) and
( b.location_id = d.location_id ) and
( c.insured_business_id = a.insured_business_id ) and
( a.policy_id = @clave and b.policy_id = @clave )

union

select
null,
e.street_addr1,
e.street_addr2,
e.city + ', ' + e.state + ' '+ e.zip,
f.location_number,
e.insured_name


from wcps..wcps.insured_business as e,
wcps..wcps.policy_named_insured as f
where f.policy_id = @clave and f.insured_business_id = e.insured_business_id



 
You should be able to add,

order by location_number

after the last line in your example.

Is that what you were doing? If it was what was the EXACT error message.

 
Thanks for your suggestion, I tried it here is the error:
Server: Msg 107, Level 16, State 2, Line 4
The column prefix 'f' does not match with a table name or alias name used in the query.

the f.location_number is valid.
 
Not after the union is done.

Remember the sort is done after the two tables are unioned.

Add the line as written above.

do NOT put in the f. prefix, if that does not work, post the code you are actually using since there is a discrepency here.
 
Thans for your reply. I got it to work by using b.location_number the results are sorted by location number.
here is the final query:

DECLARE @clave int;
set @clave = "40003227";

SELECT
d.insured_loc_name,
d.insured_loc_street1,
d.insured_loc_street2,
d.insured_loc_city +', '+ d.insured_loc_state + ' '+ d.insured_loc_zip,
b.location_number,
c.insured_name


FROM wcps..wcps.policy_named_insured as a,
wcps..wcps.policy_insured_location as b,
wcps..wcps.insured_business as c,
wcps..wcps.insured_location as d


WHERE
( a.insured_business_id = d.insured_business_id ) and
( b.location_id = d.location_id ) and
( c.insured_business_id = a.insured_business_id ) and
( a.policy_id = @clave and b.policy_id = @clave )


union

select
null,
e.street_addr1,
e.street_addr2,
e.city + ', ' + e.state + ' '+ e.zip,
f.location_number,
e.insured_name


from wcps..wcps.insured_business as e,
wcps..wcps.policy_named_insured as f
where f.policy_id = @clave and f.insured_business_id = e.insured_business_id
order by b.location_number


Thanks for your help.



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top