4

In ModelBuilder I am trying to perform the following query, using the select by attribute tool. I am having great difficulty with the sql syntax.

What I am trying to do is

Select Top 3 (Avg) Price, GasID Group by GASID order by GASID

The help indicates that aggregrate functions need to be performed in subqueries only? Is this true.

I don't want a subquery. Only this works but returns no rows and it is not what I am looking for:

"RegGasPrice" = (SELECT MAX( "RegGasPrice") FROM gasprices). There is no "in" so I must use equal.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Mar 14, 2013 at 12:22
2
  • case sensative IN ? Commented Mar 14, 2013 at 14:49
  • 1
    What is the underlying DBMS? Commented Mar 14, 2013 at 21:32

2 Answers 2

1

Here is what a definition query would look like that could do this:

-- For 3 highest gas prices
GASID IN (
SELECT TOP 3
GASID
FROM gasprices
GROUP BY GASID
ORDER BY AVG(Price) DESC)
-- For 3 lowest gas prices
GASID IN (
SELECT TOP 3
GASID
FROM gasprices
GROUP BY GASID
ORDER BY AVG(Price) ASC)

You are trying to select features by GASID and not by average price. There is a price field but no average price field. So, here, we select the top 3 GASID's where the average price is one of the top 3 or bottom 3 even though we cannot tell what that average price is in this query. You could obtain the average price by right clicking on the Price field and selecting the "Statistics" item.

answered Apr 8, 2013 at 20:48
0

I think any ArcGIS select statement is treated like a select * from where And the where part is the window that your criteria is placed. So for your statement try something like this:

SELECT top 3 * from <layer> WHERE <column> IN ('item1', 'item2', etc.)

Use the apostrophe for text, and none for numbers.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
answered Mar 15, 2013 at 2:58

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.