Question: ```
1. links: link_id, road
2. roads: road, code
2. admin_classes: code, admin_class, priority

```

So, for ONE LINK\_ID I have several codes and several admin\_class+priority raws
But in result set i need only one row for each link\_id:
 link\_id and admin\_class, that refers to min priority.
I try to make sql statement, like following:

```
select l.link_id, ac.admin_class, min(ac.priority)
from base.links as l
inner join base.admin_classes as ac
on ac.code=l.code
where ac.admin_class<>0

```

but, firstly, it does not work and it is not that i need. I want only two columns in result set - link\_id and admin\_class

Then I try following:

```
select l.link_id, (select ac.admin_class from base.admin_classes as ac where ac.code=l.code
order by ac.priority limit 1) as admin_class from base.links as l

```

It is right, but I don know how to add condition on inner select, I only need raws with admin\_class<>0

I use PostgreSql 8.3

Thank you in advance.

Answer: 
I believe you should do two things.

**1st Create a view called viewAdminRoads**

```

CREATE VIEW viewAdminRoads
AS
SELECT 
roads.code,
roads.road,
admin_classes.admin_class,
admin_classes.priority
FROM roads 
INNER JOIN admin_classes
        ON roads.road
         = admin_classes.road
WHERE admin_classes.priority <> 0

```

*Creating this view simplifies the entire process*

**2nd Create your selection**

```

SELECT
link.link_id,
MIN(viewAdminRoads.priority) as priority
FROM links
INNER JOIN viewAdminRoads
        ON links.code
         = viewAdminRoads.code
GROUP BY link.link_id

```

Remember, views are your friend.
Well i don't know much about PostgreSql, but this is how it's done with T-SQL:

```
select l.link_id, ac.admin_class, min(ac.priority)
from base.links as l
inner join base.admin_classes as ac
on ac.code=l.code
group by l.link_id, ac.admin_class
having ac.admin_class <> 0

```