FGdb SQL queries

From FreekiWiki
Revision as of 16:21, 29 January 2006 by Halfasspete (talk | contribs) (moved and modified from Stats and Testimonials talk)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Overview

The purpose of this page is to keep SQL queries that have been useful in the past in querying FGdb.

As of now (Jan 2006) this includes queries that are useful in describing Free Geek, to make charts and such to go into the Press Kit. But any queries that aren't included in the front end can go here.

Also, I hope this page will be a resource for people trying to learn more SQL, in addition to gathering data about Free Geek. So queries that are not useful in themselves, but are building blocks for more complex queries, are encouraged.

Pete

Basic access to FGdb

log into flowers (not live, a little out of date) or data (if you have an account there, and want live data)
ssh <username>@flowers
to get into FGdb command line
psql fgdb -U fgdb
to list the tables of FGdb
\dt
to describe a table
\d <tablename>

number of money donations, by year and amount

first column of table on main page (Number of donations over $500, by year.)
fgdb=# SELECT EXTRACT( YEAR FROM created ), COUNT(*) FROM donation WHERE cashdonation + monitorfee >= 500 GROUP BY 1 ORDER BY 1;
second column (note alternate syntax for column names, and for range of values)
SELECT EXTRACT( YEAR FROM created ) as Year, COUNT(*) as DonCount FROM donation WHERE cashdonation + monitorfee between 400 and 499.99 GROUP BY Year ORDER BY Year;

number of hours per volunteer, by year

These queries show some of the pieces involved. Still working toward useful queries to use in real life.

number of new adoption volunteers signed up, by year
select EXTRACT( YEAR FROM created ), count(*) from contact where adopter='Y' group by 1 order by 1;
how many hours Pete volunteered per year
select extract( year from created), sum(hours) from memberhour where memberid=460 group by 1 order by 1;
an attempt to modify the above query, to report only years where Martin logged more than 100 hours. DOES NOT WORK, needs some fixin.
select extract( year from created), sum(hours) as total from memberhour where memberid=5989 and total>100 group by 1 order by 1;
number of folks who have volunteered for us, ever
select count( distinct memberid ) from memberhour;
total number of hours logged for each volunteer in each year (yields a very long list!) I'm surprised by number of negative values. Need to look into that.
select extract( year from created ) as year, memberid, sum( hours ) as hours from memberhour where memberid in (select distinct memberid from memberhour) group by year, memberid order by year;
a subset of the above query's results, showing all volunteers who logged 80 hours or more in each year.
select extract( year from created) as year, memberid, sum(hours) as hours from memberhour group by year, memberid having sum(hours) >= 80 order by year;