sql query

5 pts.
Tags:
SQL
SQL Query
from this table i have to take those records that have common gridcode and then from these records i have to store the records having maximum area in another table... gridcode state area 13013 rajasthan 50000 13013 gujarat 45000 13014 rajasthan 25000 13014 gujarat 25000
ASKED: April 30, 2008  5:50 AM
UPDATED: April 30, 2008  12:10 PM

Answer Wiki

Thanks. We'll let you know when a new response is added.

A join will work out for you.

<pre>INSERT INTO AnotherTable
SELECT a.GridCode, a.State, a.Area
FROM Table
JOIN (SELECT GridCode, max(Area) as Area
FROM Table
GROUP BY GridCode) b ON a.GridCode = b.GridCode AND a.Area = b.Area</pre>

If each GridCode contains only one state (I assumed that this was not the case).

<pre>INSERT INTO AnotherTable
SELECT GridCode, State, max(Area)
FROM Table
GROUP BY GridCode, State</pre>

The first example I gave is the more precise example.

Discuss This Question: 1  Reply

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following