Categories
Development

Cool SQL code, with an international flaire

In a previous blog, I mentioned that a friend in the UK had a problem with getting some SQL code to do what he wanted.  Over MSN, he sent me a script to build the 3 tables involved, an “ERD” type diagram, and a description of the problem.


It went like this… He had 3 tables: Events, Users and Bookings.


Events has an EventID, and Name
Users has a UserID, a Name, and a Gender (bit field – 0 = female, 1=male)
Bookings has an EventID, and a UserID


He needed to be able to, in one statement, get a count of how many males, and how many females were attending all of the events.  He’d tried to use “Group By” and “Having” to get the right information, but the solution I gave him was as follows:


SELECT
Events.EventID,
SUM(CASE Users.Gender WHEN 0 THEN 0 WHEN 1 THEN 1 END) AS ‘NumMales’,
SUM(CASE Users.Gender WHEN 0 THEN 1 WHEN 1 THEN 0 END) AS ‘NumFemales’
FROM Bookings
LEFT JOIN Users ON Bookings.UserID = Users.UserID
INNER JOIN Events ON Bookings.EventID = Events.EventID
GROUP BY Events.EventID


The basic idea is as follows. Join all the tables, group by the event id, and then aggregate the fields we need to add.  The twist is to select the “Gender” column twice, so we can use one to count males and the other to count females.  Now if we just “SUM” each of the columns, it will show us how many men are in attendance in both columns… so we need to do a sneaky swap around on the Gender column that’s totalling the number of females so that females are represented by 1, and males by 0… that way when we do a “SUM” of each column, we will get to correct values.  Thank’s to the “CASE” statement we can do this easily. 


I know that I didn’t have to use the CASE statement for the count of the number of males, but I either had to do a CASE, a CAST or a CONVERT because BIT fields can’t be summed. So I just stuck with a CASE.  It was a “random” choice, had nothing to do with performance, and was mainly done because I just it simply involved me doing a copy and paste from the line below.


Anyway, the point of this – remember the useful CASE statement in SQL.  Its not often spoken about and probably not often used, but its very useful indeed.  If you have another way of solving this, let me know… πŸ™‚

Categories
General

Life, Connectedness, and Friends

I was just MSN’ed by an old friend who’s in the UK working and needed a hand with a SQL query.  Isn’t technology wonderful?  Who would have thought that I’d be problem solving, real time, across timezones and continents.

Anyway I managed to help him with his query (more about that later), and then we got to talking about marriage and how there’s a lot of our friends who are getting married now.  When out of the blue he came out with one of his usual wise thoughts on life, and here it is (scuze the format/spelling, but I lifted it straight out of the MSN conversation… it came split over a few messages so I put it together in “paragraphs” as I thought it should go):

in my opinion there’s tone of things that can corrupt and degrade a person in his adult life.  but for me… one of the few things / events that can truly change a person for the better…. is marriage.

it’s maybe the only comittment still, that gives u the platform to become what u never dreamed of.  i dunno why, but society sees marriage as the ‘end’

for me.. it’s just the groundwork …. one can really, honestly, tranform him/her self in marriage i reckon. there’s such union and support… and growth.  it’s almost impossible to stagnate….

how the world sees it the other way i don’t understand….

Wow… That’s Dharmesh for you… In the middle of a regular conversation, he drops in a few sentences you don’t want to forget. (and thanks to MSN :), I could save the conversation)

Categories
Geek food

Sad news

I just found out that Webmonkey has had its plug pulled.  Check out the Wired News articleWebmonkey is how I learned JavaScript, Frames, bits of CSS, XML, and HTML.  I read its PHP, ASP, and Photoshop tutorials, and I passed on the links to many friends between 1997 and 2003. (while I was in varsity, and afterwards while I was in web development)

They were the coolest site, and really saved my butt (and the butt’s of other’s) a few times when we were expected to quickly learn a “new technology” or try out something that we’d not done before.

Reading the people’s comments in the Wired article, I can agree.  I LOVED the frames tutorial(!!), and I’ll miss Webmonkey.

R.I.P.

Categories
Internet/Links

A fun way to waste your time…

Well… not quite so fun, because the “hotspots” you have to click on are sometimes really tiny… but check out this game (or download the SWF file here – its a bit easier to hit the hotspots when the SWF plays in a bigger frame), original blog entry I saw is here, follow up post here, and solution (in rot13) is here.


[UPDATE: A rot13 decoder (which you would find if you looked at the comments on the solution post), can be found here.]

Categories
Humour

VB bad, C# Good

Another reason that VB is for the weak… Check out this article. (I found it via Geoff Snowman’s blog)

Categories
Humour

Let them sing!

John (Charlie‘s brother) just sent me this link.  You enter words and have them sung back to you… very very funny.

Categories
Internet/Links

Pig fat saves lives?

Here’s a story on how pig fat can save lives in israel… from the BBC

Thanks to will who showed me a discussion about the story on PenguinMagic.

Categories
Internet/Links

More interesting photographs

Check this out… Nasa did experiments “in part to develop the ability to rapidly deploy large liquid drops by rupturing an enclosing membrane”.  They have cool video’s of it… I got to it via BoingBoing.

Categories
Geek food

Of joyfull installations, and time wasted

Yesterday I wasted time at work trying to install a product.  I installed over the old version (took about 40 minutes), but then found conflicts, so I uninstalled both versions (about 30 minutes each), then I re-installed the new version (40 minutes again).  Only, I then realised that it had left a whole bunch of files behind in the GAC (Global Assembly Cache – for the non .Net people), and in the “Program FilesCommon Files” folder… which proceeded to cause more hassles… so I uninstalled the new version again (another 30 minutes), and manually removed everything that was left (45 mins – the GAC can be a very unfriendly beast at times), and re-installed the new version (yet another 40 minutes)

Now contrast that with the install of DotNetNuke.  You simply copy the files into your web folder, point it to your database, and start using it.  It picks up for itself that you’ve not installed it before, and does all that it needs to do without asking you anything. (including setting up the database)  So a copy, a text change, and an e-mail later it was up and working.  Now I just need to make the portal look nice, find the .Text module for it, and a nice image gallery module, and I’ll be ready to have my entire site managed by it. (Including a secure section where people from my home cell could go to get the latest cell contact list)  I’ll even be able to put up an event’s calendar for cell, and one for my personal stuff. And lots more… πŸ™‚

Categories
Humour

Fark…

Every now and again I’ve followed links from Will Wheaton’s hillarious blog (more commonly known for the role he played as “Wesley Crusher” in STNG) to Fark.  Generally they’re links like this, where Fark users use photoshop to play around with images.  In the previous link they’re ripping off the various Google holiday logos.

Today I subscribed to their rss feed and have found it full of rather humorous links to news articles… like “Costumed Disney World employee ran over by float, killed during parade. Disney spokesperson attributes incident to goofy mistake”.