Calculating Medians With SQL

Given that median is such a valuable statistical measure, it’s baffling that Microsoft’s SQL Server and other relational databases (MySQL, PostgreSQL) don’t have a built-in MEDIAN function. Well, this week, after working through a data set in SQL Server — and deciding I didn’t want to push the data into SPSS to find medians — I hit the web to find a T-SQL workaround.

I found a ton of solutions (some from people with no clue about the difference between median and average), but the one below — adapted from a post by Adam Machanic at sqlblog.com — was the best. It produces accurate results and is fairly speedy to boot.

Here’s an example. Consider this table with student grades from two courses:
.

ID Class FirstName Grade
1 Math Bob 65
2 Math Joe 72
3 Math Sally 95
4 Science Bob 65
5 Science Joe 81
6 Science Sally 81
7 Science Mike 72

We’d like to find the median grade in each class. Here’s the script:
(more…)

Download My WordPress Theme

I make no claim to having any design sense, but if you like my site’s WordPress theme, you’re welcome to it. Grab it right here.

Portfolio AD is a one-column theme with a two-column sidebar. Both columns in the sidebar can display widgets. The header is a modification of the well-worn Hemingway theme, and the rest is my take-off of a site built via this tutorial from WPDesigner.com. It works fine with WordPress 3.0.

What you see here is the result of constant tinkering. I coded up a theme of my own to learn more about what WordPress can do, and in the process I have learned tons about CSS, HTML, web hosting and content management systems in general. If you have the time, it’s well worth the investment.

Use the theme as a starting point for your own tinkering and have fun!

You Can’t Afford to Stop Learning

The late Jerry McBride, founder of the Marist College information systems program and mentor to many of us in it, gave our capstone class a bit of advice that I’ve always remembered:

“Never stop learning.”

Plenty of people, regardless of industry, argue otherwise. Play it safe, take few risks and stay with the tried and true — that keeps the bills paid and the lights on.

Decades ago, “No one ever got fired for buying IBM” was a phrase you could bank on. IBM had it all — the kings of the air-conditioned mainframe, making oodles of money, and very snug in their white-shirt-and-tie ways. But they were slow to learn. PCs came along, the mainframe business withered, OS/2 failed to unseat Windows, and tens of thousands of people in IBM-hometowns found themselves unemployed. IBM’s come back, but it’s nothing like the company it was in 1980.

Stop learning, rest on the existing models, and it’s easy to become a mainframe-hawker in a PC revolution. Or a railroad tycoon watching with disregard as Henry Ford mass produces Model T’s.

I’ve practiced Jerry McBride’s advice better at times than others. Lately, very much so. I’m on a learning jag. My latest quest is Ubuntu, Apache, PostgreSQL and Django. Last night, this little screen brought a smile or two:

P.S. My bookmarks and RSS feeds prove one thing: There’s no excuse for not learning; the Internet is the best free library you’ll ever find.

Anthony

About me

I'm a journalist who works with words, code and data. I'm also a husband, father, musician, gardener and occasional poet. I love finding and telling great stories. I'm inspired by art, music and design that elevate. I pursue the truth. Data journalism's the focus here, but other topics will crop up. Thanks for reading.
LINKS & TRIVIA

microblog

places

LinkedIn
Twitter
Delicious
Tumblr
Spelling Bee blog
- Tales from our trips to the Scripps National Spelling Bee
360 Sports Jam
- My teen journalist's sports blog

data journalists

Brian Boyer
Aaron Bycoffe
Jack Gillum
Gregory Korte
Aron Pilhofer
Mark Schaver
Matt Waite
Ben Welsh
Derek Willis
Matt Wynn

from the home office

RSS TOP USA TODAY STORIES