l i n u x - u s e r s - g r o u p - o f - d a v i s
L U G O D
 
Next Meeting:
April 21: Google Glass
Next Installfest:
TBD
Latest News:
Mar. 18: Google Glass at LUGOD's April meeting
Page last updated:
2006 Apr 11 14:09

The following is an archive of a post made to our 'vox-tech mailing list' by one of its subscribers.

Report this post as spam:

(Enter your email address)
[vox-tech] puzzling over MySQL query: joining aggregated values ina single query
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[vox-tech] puzzling over MySQL query: joining aggregated values ina single query



Hi!

I have a simple table with multiple records per id number: soil horizons from 
numerous pits. the top and bottom of each horizon or layer is defined in the 
fields 'top' and 'bottom'

a quick example of a single pit's records would like like this:
+---------------------+-----------+------+------+--------+
| pedon_id            | hz_number | name | top  | bottom |
+---------------------+-----------+------+------+--------+
| SSGG-spring-05-P009 |         1 | O    |    0 |      3 |
| SSGG-spring-05-P009 |         2 | A1   |    3 |     14 |
| SSGG-spring-05-P009 |         3 | A2   |   14 |     26 |
| SSGG-spring-05-P009 |         4 | AB   |   26 |     70 |
| SSGG-spring-05-P009 |         5 | Bw1  |   70 |    108 |
| SSGG-spring-05-P009 |         6 | Bw2  |  108 |    145 |
| SSGG-spring-05-P009 |         7 | 2C   |  145 |    170 |
| SSGG-spring-05-P009 |         8 | 3Ab  |  170 |    226 |
| SSGG-spring-05-P009 |         9 | 3Bwb |  226 |    240 |
+---------------------+-----------+------+------+--------+


I am able to select out the soil depth, excluding certain horizons with the 
following query:

select pedon_id, max(bottom) as depth from horizon where name REGEXP '.?C.?|
R|.b.?' != 1 group by pedon_id ; 

a simple result for the above example would be:
+---------------------+-------+
| pedon_id            | depth |
+---------------------+-------+
| SSGG-spring-05-P009 |   145 |
+---------------------+-------+

This works well, except for when I would like to join this aggregated 
information to a new table in a single query. Is it possible to do something 
like this:

pseudo-SQL;

select table_a.* , (pedon_id, max(bottom) as depth from horizon where name 
REGEXP '.?C.?|R|.b.?' != 1 group by pedon_id) from table_a, horizon 
where ....

i.e. is it possible to perform an aggregation in the same step as the join, or 
am I going to need to use some temp. tables?

Any thoughts -- am i trying to do something completely ridiculous?

Thanks!





-- 
Dylan Beaudette
Soils and Biogeochemistry Graduate Group
University of California at Davis
530.754.7341
_______________________________________________
vox-tech mailing list
vox-tech@lists.lugod.org
http://lists.lugod.org/mailman/listinfo/vox-tech



LinkedIn
LUGOD Group on LinkedIn
Sign up for LUGOD event announcements
Your email address:
facebook
LUGOD Group on Facebook
'Like' LUGOD on Facebook:

Hosting provided by:
Sunset Systems
Sunset Systems offers preconfigured Linux systems, remote system administration and custom software development.

LUGOD: Linux Users' Group of Davis
PO Box 2082, Davis, CA 95617
Contact Us

LUGOD is a 501(c)7 non-profit organization
based in Davis, California
and serving the Sacramento area.
"Linux" is a trademark of Linus Torvalds.

Sponsored in part by:
Appahost Applications
For a significant contribution towards our projector, and a generous donation to allow us to continue meeting at the Davis Library.