Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query 1 Table But Sort By Another

Author  Topic 

paulferree
Starting Member

8 Posts

Posted - 2008-07-11 : 14:49:46
I'm having a difficult time trying to figure out how to sort some data. It's probably simple but I can't seem to figure it out.

I have 2 SQL tables:

Table 1 has 2 columns:
category_id | vendor_id

Table 2 has multiple columns but I only need to utilize 2 for this example:
vendor_id | vendor_package_id

There is a simple query that retrieves all the vendor_ids for a single category_id....for example
category_id | vendor_id
35 | 125
35 | 223
35 | 109

I need to sort these results based on the vendor_packaged_id that is specific to each vendor_id.

Essentially, this is what I want:

"Return me all the vendor_ids that are assigned to category 35 FROM tblCategory_Matrix
Sort those vendor_ids based on their vendor_package_id FROM tblVendors"

Any help would be appreciated!

Thanks!
Paul Ferree

mfemenel
Professor Frink

1421 Posts

Posted - 2008-07-11 : 15:02:17
You should just be able to join by vendor_id and sort by the package id if I'm understanding you correctly. See example below.

Create table table1(category_id int, vendor_id int)

Create table table2 (vendor_id int,vendor_package_id int)

insert into table1(category_id,vendor_id)
values(35,1)
insert into table1(category_id,vendor_id)
values(35,2)
insert into table1(category_id,vendor_id)
values(35,3)

insert into table2(vendor_id,vendor_package_id)
values(1,100)
insert into table2(vendor_id,vendor_package_id)
values(2,1)
insert into table2(vendor_id,vendor_package_id)
values(3,15)

select a.category_id,v.vendor_id
from table1 a
inner join table2 v on a.vendor_id=v.vendor_id
order by v.vendor_package_id

Mike
"oh, that monkey is going to pay"
Go to Top of Page

paulferree
Starting Member

8 Posts

Posted - 2008-07-11 : 15:27:32
It was enough to get me in the right direction!

Here was the final QUERY that worked for me:

SELECT cm.vendor_id, cm.category_id , v.vendor_id, v.vendor_package_id
FROM tblCategory_Matrix cm, tblVendors v
WHERE cm.category_id = '34' AND v.vendor_id = cm.vendor_id
ORDER BY v.vendor_package_id DESC

Worked like a charm!!

Thanks for the help!!
Paul
Go to Top of Page
   

- Advertisement -