SELECT '# projects' AS ___;
SELECT count(*) AS count FROM projects;

SELECT 'projects' AS ___;
SELECT CAST(project_id AS char) AS project FROM projects ORDER BY project;

SELECT 'each project\'s # locations' AS ___;
SELECT
    CAST(projects.project_id AS char) AS project
    , count(PlotID) AS locations_count
FROM projects
JOIN plotMetadata ON plotMetadata.project_id = projects.project_id
GROUP BY project
ORDER BY project
;

SELECT '# locations' AS ___;
SELECT count(*) AS count
FROM
(
    SELECT DISTINCT LatDec, LongDec FROM plotMetadata
) locationcoords
;

SELECT '# location events' AS ___;
SELECT count(*) AS count FROM plotMetadata;

SELECT '1st methods' AS ___;
SELECT DISTINCT
    CAST(projects.project_id AS char) AS project
    , PlotMethod AS method
FROM projects
JOIN plotMetadata ON plotMetadata.project_id = projects.project_id
ORDER BY project, method
LIMIT 10;

SELECT '1st observation measures' AS ___;
SELECT DISTINCT
    CAST(projects.project_id AS char) AS project
    , lookup_MethodCode.Description AS observationmeasure
FROM projects
JOIN plotMetadata ON plotMetadata.project_id = projects.project_id
JOIN lookup_MethodCode ON lookup_MethodCode.MethodCode = plotMetadata.MethodCode
ORDER BY project, observationmeasure
LIMIT 10;

/*
SELECT p.PlotID, p.SiteCode, COUNT(*)
FROM projects pr JOIN plotMetadata p JOIN plotObservations o
ON pr.project_id=p.project_id AND p.PlotID=o.PlotID
WHERE p.project_id=2
GROUP BY p.PlotID, p.SiteCode;

SELECT NoInd, COUNT(*) AS observations
FROM plotObservations o JOIN plotMetadata p 
ON o.PlotID=p.PlotID
WHERE project_id=2
GROUP BY NoInd;

SELECT p.PlotID, p.SiteCode, SUM(NoInd) AS Individuals
FROM projects pr JOIN plotMetadata p JOIN plotObservations o
ON pr.project_id=p.project_id AND p.PlotID=o.PlotID
WHERE p.project_id=2
GROUP BY p.PlotID, p.SiteCode;

SELECT DISTINCT pr.project_id, project_name, PlotMethod, m.MethodCode, m.Description
FROM projects pr JOIN plotMetadata p JOIN lookup_MethodCode m
ON pr.project_id=p.project_id AND p.MethodCode=m.MethodCode
WHERE m.MethodCode=8;

SELECT NoInd, COUNT(*) AS observations
FROM plotObservations o JOIN plotMetadata p JOIN lookup_MethodCode m
ON o.PlotID=p.PlotID AND p.MethodCode=m.MethodCode
WHERE project_id=2
GROUP BY NoInd;

SELECT p.PlotID, p.SiteCode, SUM(NoInd) AS Individuals
FROM projects pr JOIN plotMetadata p JOIN plotObservations o
ON pr.project_id=p.project_id AND p.PlotID=o.PlotID
WHERE p.project_id=1
GROUP BY p.PlotID, p.SiteCode
LIMIT 10;

SELECT PlotMethod, MethodCode, Description, COUNT(project_id) as projects
FROM (
SELECT DISTINCT p.PlotMethod, p.MethodCode, p.Description, p.project_id
FROM (
SELECT project_id, PlotID, PlotMethod, m.MethodCode, m.Description
FROM plotMetadata p JOIN lookup_MethodCode m
ON p.MethodCode=m.MethodCode
) p JOIN plotObservations o JOIN stems s
ON p.PlotID=o.PlotID AND o.PlotObsID=s.PlotObsID
GROUP BY project_id
) AS a
GROUP BY PlotMethod, MethodCode, Description;

SELECT DISTINCT pr.project_id, project_name
FROM projects pr JOIN plotMetadata p JOIN plotObservations o JOIN stems s
ON pr.project_id=p.project_id AND p.PlotID=o.PlotID AND o.PlotObsID=s.PlotObsID
WHERE p.MethodCode=3;

SELECT p.PlotID, SiteName as plot, COUNT(*) AS stems
FROM plotMetadata p JOIN plotObservations o JOIN stems s
ON p.PlotID=o.PlotID AND o.PlotObsID=s.PlotObsID
WHERE p.project_id=2
GROUP BY p.PlotID, SiteName
LIMIT 10;

SELECT p.PlotID, SiteName AS plot, COUNT(DISTINCT Family, Genus, Species) AS SpeciesCount
FROM plotMetadata p JOIN plotObservations o
ON p.PlotID=o.PlotID
WHERE project_id=2
GROUP BY p.PlotID, SiteName;

SELECT DISTINCT p.PlotID, SiteName AS plot, Family, CONCAT_WS(' ',IFNULL(Genus,''), IFNULL(Species,'')) AS SpeciesName
FROM plotMetadata p JOIN plotObservations o
ON p.PlotID=o.PlotID
WHERE p.PlotID=298;

SELECT name_status, count(*) FROM plotObservations GROUP BY name_status;

SELECT DISTINCT p.PlotID, SiteName AS plot, Family, 
CONCAT_WS(' ',IFNULL(Genus,''), IFNULL(Species,'')) AS SpeciesName, name_status
FROM plotMetadata p JOIN plotObservations o
ON p.PlotID=o.PlotID
WHERE p.PlotID=298;

SELECT DISTINCT p.PlotID, SiteName AS plot, Family, 
CONCAT_WS(' ',IFNULL(Genus,''), IFNULL(Species,'')) AS SpeciesName, name_status
FROM plotMetadata p JOIN plotObservations o
ON p.PlotID=o.PlotID
WHERE p.PlotID=298 AND name_status=1;

SELECT p.PlotID, SiteCode AS plot, o.Line as subplot, COUNT(*) AS individuals
FROM plotMetadata p JOIN plotObservations o
ON p.PlotID=o.PlotID
WHERE p.PlotID=298 
GROUP BY p.PlotID, plot, subplot;

SELECT p.PlotID, SiteCode AS plot, o.Line as subplot, SUM(NoInd) AS individuals
FROM plotMetadata p JOIN plotObservations o
ON p.PlotID=o.PlotID
WHERE p.PlotID=298
GROUP BY p.PlotID, plot, subplot;

SELECT p.PlotID, SiteCode AS plot, o.Line as subplot, SUM(NoInd) AS individuals
FROM plotMetadata p JOIN plotObservations o
ON p.PlotID=o.PlotID
WHERE p.PlotID=1
GROUP BY p.PlotID, plot, subplot;

SELECT COUNT(*) FROM plotMetadata WHERE MethodCode=1;

SELECT PlotID FROM plotMetadata WHERE MethodCode=1 LIMIT 1;

SELECT p.PlotID, SiteCode AS plot, Family, 
CONCAT_WS(' ',IFNULL(Genus,''), IFNULL(Species,'')) AS SpeciesName, cover_percent
FROM plotMetadata p JOIN plotObservations o
ON p.PlotID=o.PlotID
WHERE p.PlotID=24589;

SELECT p.PlotID, p.SiteCode as plot, Country, PolDiv1 AS stateProvince, pol2 AS countyParish, 
LatDec as latitude, LongDec as longitude, Elev as elevation
FROM projects pr JOIN plotMetadata p
ON pr.project_id=p.project_id
WHERE pr.project_id=1
LIMIT 20;

SELECT AccessCode, COUNT(*)
FROM plotMetadata
GROUP BY AccessCode;

SELECT pr.project_id, project_name, project_pi, allow_download_all, PlotID, SiteCode, Country
FROM projects pr JOIN plotMetadata p
ON pr.project_id=p.project_id
WHERE p.AccessCode=1;
*/
