meta data for this page
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| pluginto:useful_sql_queries [2023/10/19 21:46] – joetulenko | pluginto:useful_sql_queries [2026/01/21 17:21] (current) – joetulenko | ||
|---|---|---|---|
| Line 100: | Line 100: | ||
| For example, someone might have data in ICE-D that is yet unpublished but fully measured. The samples can have calculated ages, but of course they are not linked to any publication entry in the publication table. Thus, if you want sample info AND publication info for a bunch of samples in an area of interest, but some of the samples in that area do NOT belong to a publication, | For example, someone might have data in ICE-D that is yet unpublished but fully measured. The samples can have calculated ages, but of course they are not linked to any publication entry in the publication table. Thus, if you want sample info AND publication info for a bunch of samples in an area of interest, but some of the samples in that area do NOT belong to a publication, | ||
| - | There is probably some work around | + | The way around this is through |
| ---- | ---- | ||
| Line 171: | Line 171: | ||
| </ | </ | ||
| - | //note: in this case, since I wasn't looking for sample info at all but rather JUST info from the 14C nuclide table, I use the FROM _c14_quartz statement | + | //note: in this case, since I wasn't looking for sample info at all but rather JUST info from the 14C nuclide table, I use the FROM _c14_quartz statement |
| ---- | ---- | ||
| Line 262: | Line 262: | ||
| \\ | \\ | ||
| + | |||
| + | 7. Filtering selection by choosing samples from sites that have AT LEAST 3 samples per site. For example, let's say I only trust moraine ages in the database that are based on at least 3 boulder ages on the moraine, any less and the age might be uncertain.\\ | ||
| + | **New Feature:** lots of new features in this one, but mainly the GROUP_BY and HAVING COUNT (*) built-in functions. | ||
| + | |||
| + | < | ||
| + | |||
| + | SELECT base_sample.name, | ||
| + | FROM base_sample | ||
| + | JOIN base_site ON base_site.id = base_sample.site_id | ||
| + | WHERE base_site.what LIKE " | ||
| + | AND base_sample.site_id IN | ||
| + | (SELECT base_sample.site_id FROM base_sample GROUP BY base_sample.site_id HAVING COUNT(*) > 2) | ||
| + | |||
| + | </ | ||
| + | |||
| + | //note: the statement before and within the parentheses (I think) works more or less like this:\\ | ||
| + | 1. "AND base_sample.site_id IN" = give me all samples where their site_id field meets the following criteria.\\ | ||
| + | 2." | ||
| + | 3. " | ||
| + | |||
| + | ---- | ||
| + | |||
| + | \\ | ||
| + | |||
| + | 8. Query used to filter the ICE-D Alpine application for samples in the Cascades and Sierra Nevada as part of Tulenko et al. (2024): Cosmogenic 21Ne exposure ages on late Pleistocene moraines in Lassen Volcanic National Park, California, USA. | ||
| + | |||
| + | < | ||
| + | |||
| + | SELECT base_sample.name, | ||
| + | FROM base_sample | ||
| + | JOIN base_site ON base_sample.site_id = base_site.id | ||
| + | JOIN base_calculatedage ON base_calculatedage.sample_id = base_sample.id | ||
| + | WHERE base_sample.what LIKE " | ||
| + | AND base_site.what LIKE " | ||
| + | AND base_sample.site_id IN | ||
| + | (SELECT base_sample.site_id FROM base_sample GROUP BY base_sample.site_id HAVING COUNT(*) > 3) | ||
| + | AND (base_sample.lat_DD > 35 AND base_sample.lat_DD < 48) | ||
| + | AND (base_sample.lon_DD > -123 AND base_sample.lon_DD < -117) | ||
| + | AND (base_calculatedage.t_LSDn > 10000 AND base_calculatedage.t_LSDn < 35000) | ||
| + | ORDER BY base_sample.lat_DD | ||
| + | |||
| + | </ | ||
| + | |||
| + | ---- | ||
| + | |||
| + | \\ | ||
| + | |||
| + | 9. LEFT JOIN - let's say I want info on a bunch of samples from multiple fields/ | ||
| + | |||
| + | In the example below, I am searching for sample names, sample 10Be (and/or 26Al) concentrations, | ||
| + | |||
| + | (1) samples that only have 10Be concentrations (and/or 26Al concentrations) \\ | ||
| + | (2) samples that only have 14C concentrations \\ | ||
| + | (3) samples that have both 10Be (and/or 26Al concentrations) AND 14C concentrations \\ | ||
| + | (4) samples have none of the above - likely because a sample is linked to an incomplete entry in either the 10Be/26Al table and/or the 14C table that's missing nuclide concentration(s) \\ | ||
| + | |||
| + | (using a standard JOIN would only select samples that satisfy criteria 3 and in rare cases 4) | ||
| + | |||
| + | < | ||
| + | |||
| + | SELECT base_sample.name, | ||
| + | FROM base_sample | ||
| + | LEFT JOIN _be10_al26_quartz ON _be10_al26_quartz.sample_id = base_sample.id | ||
| + | LEFT JOIN _c14_quartz ON _c14_quartz.sample_id = base_sample.id | ||
| + | WHERE (base_sample.lat_DD > 31 AND base_sample.lat_DD < 49) | ||
| + | AND (base_sample.lon_DD > -125 AND base_sample.lon_DD < -104) | ||
| + | |||
| + | </ | ||