Skip to main content

Posts

Showing posts with the label database

NoSQL Databases - MongoDB first cut

A few days ago I tried MongoDB, my first encounter with what is called a NOSQL Database. No, it does not mean 'no' 'sql', rather Not only SQL, anyway I thought I would try mongodb, out of the many available there. It has both the document-oriented database design and ability to be accessed from Javascript, both features I really like. However, since I spend most time in Java, I thought I would start there. First, let me tell you you should try their web console before writing a line of code or downloading anything because it's a really good self-run tutorial, that lets you type real commands and get real output all from the browser! pretty awesome... Anyway, here is what I did to get started. 1. Download mongodb from http://www.mongodb.org/ 2. Create working dir for MongoDB to use as data directory 3. Open favorite IDE or editor, and start writing test, yes Test , not source, in the spirit of Test Driven Development. 4. In test package, create test such as: ...

Some subquery magic with MySQL

As part of a Rails project I had to gather up some data from a single table, and calculate various stats on the data Problem was - had to group data by a field, then calculate aggregates in various ways not possible with a single GROUP BY query Using the SQL cookbook tips, and some pondering, I learned that I could create a bunch of 'fake' tables from the same table using subqueries, then join these tables in a way that would yield a single resultset An example: Let us say you have a table of person, their test points whether they passed the test attempt, like (two people a and b tried a test at various times) : Name Points Passed a 10 1 a 25 0 b 21 1 a 28 0 b 23 1 a 35 1 b 31 1 a 35 1 b 41 1 You want a result like Name Avg Age No. of attempts'passed' a 30.3 5 b 25.1 4 (caution: These numbers are probably wrong just too lazy to fix them up!) As you can see, it is not possible with a single query, since grouping by name and...