December 2008 Archives

How to ORDER BY a Bitwise Field in SQL

| No Comments | No TrackBacks

So, I had an interesting problem crop up a little while ago.  If you are storing a bitwise “flags” field in your database, and you need to order by a particular flag or flags of that field, how do you do it ?

It turns out the answer is relatively simple.

Table Structure: id, name, flags
Flag Values:  1 = Enemy, 2 = Friend, 4 = Owe Money To, 8 = Owes Me Money

Sample Data:
1, Matthew, 2 // Friend
2, Josh, 6 // Friend, I Owe Money
3, Thomas, 10 // Friend, Owes Me Money
4, John, 9 // Enemy, Owes Me Money
5, Abe, 2
6, Becky, 6
7, Jimmy, 10
8, Jason, 9
9, Dean, 1
10,Joseph, 5
11,Frank, 6
12,Julie, 11 // Frenemy, Owes me Money
13,Hannah, 10
14,Sam, 5
15,Q, 9

So we have a list of people in the database that are fall under a few different categories [flags] as referenced above.  Now I want to query from this table to find out various things about the people contained within.

Now I want to see all of the people in the table that are my friends, ordered by those that owe me money, followed by those that I owe money to, followed by any others.

SELECT *, (flags & 4 = 4) AS owesme, (flags & 8 = 8) AS iowethem FROM temp WHERE (flags & 2 = 2) ORDER BY owesme DESC, iowethem DESC, name


I’m pretty happy with that.  I had a need, and figured out how to scratch it.  I am a little surprised that I had never needed this before, but it certainly does work!  I hope this helps those that might be trying to Google for this.. I did not find a single entry, luckily my hunch paid off.  I wonder if not a lot of people do this sort of thing ?  I love storing flags, and now I can use those for easy ordering as well.

VistaDB Rocks:
I wasn’t completely sure that I was going to be able to pull this off in VisatDB, but sure enough, it worked like a charm and the first time.  I’m quite impressed!  Yet something else that VistaDB does well.  I also tried it in MySQL to make sure I wasn’t on drugs either. 

Experiment, play around with it, see if you can find a use for something like this.

Happy programming!

Matthew MacSuga

A Move to a New Host

| No Comments | No TrackBacks

I moved the site to a new hosting facility, and had a few problems along the way!  It’s up and running now and should be in good shape.  If anyone notices any problems, please let me know :P

P.S.  I’m going to try and write a few more articles this week and post some pretty cool code.  While this is called “C# By Design”, I’ll probably be writing a bit about PHP and how to consume .NET web services!  I had a lot of issues with Google while developing my solutions so I think they’ll be of use.


Matthew MacSuga

Maintaining a Blog is Hard, and So is Coding

| No Comments | No TrackBacks

I’ve decided that maintaining a blog is hard work.  You might be prompted to ask, “Matthew, why is maintaining a blog hard wokr?”  I would answer, “Because you’re supposed to write in it!”

My original intention was to write about the things that I’m working on.  Unfortunately, this means that I have to actually take the time to do so.  Lately, time has been very limited because I’ve been working on a lot of really difficult and completely cool stuff!  That means, I have a back-log of things to write about.

Things I’ve been working on lately

1.  Creating a new .NET based Ski Reporting & Faxing System
2.  Writing API’s for the ski reporting system
3.  Writing applications in PHP to consume the data from the ski reporting system
4.  Writing PHP again!
5.  Learning a new database (VistaDB) []
6.  Learning more about Developer Express’ XtraReporting Suite
7.  Learning about Windows Workflow Foundation  (WF)
8.  Learning how to use Ra-Ajax []
9.  Writing Multi-Threaded Windows Management Applications for said ski system!
10.  Not skiing
11.  Learning how to use Click Once deployment
12.  Reading the web, reading books, learning new things, participating in newsgroups

What do I intend to blog about next?

Namely, the above.
I’ve been doing so much programming, learning so many new things, it’s just been crazy.  I can’t wait to share some of the neat PHP code that I wrote that uses NuSOAP and calls remote web services written in .NET.  I learned some new things, and came up with some code that I had an extremely hard time finding the answer to on Google.  In fact, I did not find the answer on Google!  A co-worker and I found the solution.  So I will definitely be writing about that (Hint:  Passing List<T> data TO/FROM a Web Service) experience as it may help some.

I’m going to write a white-paper style article on the ski reporting system, how it all works.. the challenges I faced.. what new things I created to solve problems.. I did (in my opinion) some seriously nice work on that system.  I can’t wait to explain it all to everyone.  It’s based in both Linux (PHP) and Windows (C#/.NET) with Web Services being the interface between both.  I also developed new database code to work with VistaDB, which is a new database I found that I absolutely love.  I’ll be writing about that as well.

Developer Express
The Developer Express [] products are absolutely amazing.  I discover more and more neat stuff about the product suite every day.  I learned that you can use the XtraReporting suite to create really NICE web pages [example:  Ski Idaho Conditions Report Summary] without creating all of the HTML yourself.  In my opinion, this is one of the nicest features of the entire product line.. not only can you create PDFs and Images, but flawless HTML.  The things that I can do absolutely amaze me, and I still don’t think I’ve scratched the surface of everything that I can do.  I will be writing a separate post about that, and how the solution works in both .NET and PHP.

I’m also investigating Ra-Ajax [] for doing all of my AJAX related stuff.  I’ve been a huge jQuery fan lately, but Thomas Hansen is a friend of mine and I really believe in the work that he’s doing.  He is a top-notch programmer, and he’s been a huge help to me since I’ve known him!  You’ll remember that I previously suggest Gaia Ajax Widgets – I do not anymore.. in addition, Ra-Ajax is a far superior product.  Thomas fixed everything that he did wrong in Gaia, and made it all better in Ra.  Huge kudos go out to him for what he’s doing and giving to the .NET community, and those that want to develop cutting edge FAST AJAX applications with a minimal amount of JavaScript code creation.

I will most likely be writing a separate series of articles on Ra-Ajax at a later time detailing my experiences with it.  You might be prompted to ask why I would use Ra, when I have the Developer Express product.. well, DX does some things really well, but like most ASP.NET toolkits, they are to heavy.  Ra-Ajax is lightweight, open source, and easy to implement.  Much faster than the other guys.. even though it might not look as good, looks can be fixed.. trimming the fat off of heavy is much harder to do.

Disclaimer:  I’m friends with Thomas, and I support him and his endeavors.    I sound like I’m writing marketing speak, and getting paid for it – but I’m not, honest!!  I just write about what I use, and believe in.  If I think something about it sucks, I’ll write about that too!

Windows Workflow (WF)
One last thing before I go – I discovered Windows Workflow Foundation, and absolutely recommend Pro WF: Windows Workflow in .NET 3.5 (Pro).  This book really helped me understand Windows Workflow (WF) and I actually implemented it in my ski reporting system.  Unfortunately, I had to remove the code later-down-the-road because I had some issues, and being under pressure and all.. it was very easy to “remove” though, because it just encapsulates logic.  I love the technology, and can’t wait for the next release.

Microsoft Charts!
Oh, by the way, did you know Microsoft released a Charting Suite (by the Dundas people) for free, as part of .NET 3.5 SP1 ? 
Blog by Jason at VistaDB:  Free Chart Control from Microsoft For .NET 3.5
Direct Microsoft Download:  Download Charts Control

I won’t ever use them, but still cool that they did that.  (I won’t use because I have charts in Developer Express XtraCharts suite – heh, having an Enterprise subscription sure helps!)

Writing Too Much Again
Sorry for being so long winded.
Upcoming posts will be chalked full of code and interesting tidbits of information that I’ve learned over the last few months.  Will be worth it!

Amazon Affiliate Program [ I sold out ! ]
P.S.  I am embedding links now (Pro WF) link.  I will not link to anything I don’t own, but if you buy something – I sure wouldn’t mind a kickback, so I joined their referral program.

Matthew MacSuga

About this Archive

This page is an archive of entries from December 2008 listed from newest to oldest.

July 2008 is the previous archive.

January 2009 is the next archive.

Find recent content on the main index or look in the archives to find all content.