Steve on April 18th, 2008

The not so simple SQL Statements

Who would have thought that returning some records would become an all day ordeal. I guess if I had completely thought it through, I might have seen the roadblocks to come, but that’s not how the day went.

All I wanted to have was a drop down box that when the option was changed to a particular kind of equipment, it would bring back (through some AJAX-y goodness) the last 10 pieces of equipment in that category.

I was only working on the functionality of it at this point. The look and wording of it will change before it gets on the site.

After a few minutes I had it working. Not to difficult. Using AJAX to dynamically change the page on an ‘onChange’ event with the drop down box brought back the last 10 pieces of equipment.

It worked, but on closer examination I saw a problem. Since equipment in our database is uploaded in bulk, the last 10 machines are always from the same company, sometimes even 10 of the same model with slightly different specs. What I decided I really wanted was the last 10 machines from 10 different companies.

And THAT is harder than it sounds. For several reasons ‘Select DISTINCT’ can not be used and I hunted around the internet for a while and could find nothing that would help me they way I wanted. I was finally able to consult with our company’s sometimes hanging around .NET programmer (I hate .NET) and he recommended the ‘UNION’ function of SQL.

This sounded like a duct tape solution if I ever heard of one, but it does work.

This works by getting a list of the last 10 companies to update in the category you are working with. Then do a series of ‘Select TOP 1 *’ statements for each company that you then ‘UNION’ together to make the results look like one statement.

For example…

Select Top 1 {COL} from {TABLE} where company = {FIRST COMPANY} UNION Select Top 1 {COL} from {TABLE} where company = {SECOND COMPANY} UNION

And so on. You need to keep count of your UNIONS thought, because when you get to the 10th company, you don’t want to end your statement with ‘UNION’ because it will error out. So you pull out the good old

i = 0
i = i +1
While i < 10

We’ve all used some for of that code block no matter what language you are using to program, but it ended up not working here. It turns out that for some pieces of equipment, the items are so industry specific that there aren’t 10 different dealers. That means that the code errors out before the end of the SQL statement because there is nothing left to select.

So what needs to be done here is something along these lines….

SQL = 'Select TOP 1 {COL} from {TABLE} where company = {RECORDSET.COMPANY}'

while not RECORDSET.EOF
SQL = SQL & ' UNION '
SQL = SQL & 'Select TOP 1 {COL} from {TABLE} where company = {RECORDSET.COMPANY}'
RECORDSET.MOVENEXT
WEND

What happens here is that the SQL Statement is fired and then it checks to see if there are anymore companies. If so, it adds the UNION tag and the next SQL Select. When there are no more companies to bring equipment back from, it ends the statement and there is no error.

Since RECORDSET.COMPANY should be the last 10 companies that updated, the Select statement won’t bring back more than 10 records before it gets to the end.

Of course, that couldn’t be the final problem. Apparently, there are a lot of apostrophe’s in the company names, so it took a few minutes to figure out how to get the REPLACE function to work in this situation, but that wasn’t that big of a deal.

Here is a link to the working version of my code.

http://www.constructionequipmentguide.com/ajax.asp

When I get back to work, I will flesh this out with more accurate code descriptions
.

Work No Comments

Steve on April 15th, 2008

2008 == 1999

At least for me it does.

1999 was the year my company finally realized it needed to be on the web in some way. I had started to suggest the idea to them in 1996 and they put up a cursory contact information page in 1998, but it wasn’t until ‘99 that they decided to actually try and create a presence. 

I mean, who was I that they would listen to anyway. I was a graphic designer and had been here for less than a year, and the web was still so unknown to to most people and companies.

But ‘99 changed all that and we solicited bids from ‘Web Development Companies’. Companies that would handle all aspects of the design from graphics creation, HTML page building and any backend programming that needed to be done.

What we had decided we wanted was to put our newspaper online. We needed an editorial section that would hold that days stories and an archive, and a used equipment section that would consist of a search engine for the equipment and detail pages for each machine.

Well, we ended up with several bids that went from 13,000 all the way up to 100,000 and my company was not prepared for these numbers to say the least.

So I took my shot. I told them I could do it, and they actually believed me.

I still remember the call from my boss over the weekend asking if I thought I would really be able to handle it and I said of course I could (Not mentioning that I had never built a page in my life), and he said that when I came in on Monday, I was no longer to work on the publication, and that my full time job was going to be creating the website. 

They did contract the cheapest of the companies to do some backend programming and with them we launched the site in early 2000, and it was probably the busiest, most stressful time of career to that point. But I loved it.

With the exception of one piece of VB Script that I still use, I had re-written all of the code myself within the next 2 or 3 years, and continually updated the look, changed and added features as needed and rolled out updates on a daily or weekly basis for the next 8 years.

But now its happening again. It’s time for us to move forward and re-build from the ground up, and I’m getting that feeling again. I stressed and REALLY busy. It’s time to add things to the site that I have never built before.

I have been asked to stop the incremental updates to the site. To be honest I have been asked to stop doing anything to the site, and now have a month and a half to deliver the look and basic functionality of a new website and 5 other web related products. Including new admin sections, newsletters, and a video library that can do everything that a service like Brightcove can do, but built in house by me.

And then I have another month or so after to make it all work.

It will still be an ASP/SQL site. I hate .NET and moving to PHP/MySQL isn’t in the cards. Although I do have a PHP version of the site that mirrors the functionality of the active site. I’m sure it’s because of my knowledge of ASP vs PHP, but I can’t get the PHP site to work as fast or as well as the ASP version.

It will of course try to be standards based, having tables only where necessary, and I will use this as an opportunity to add some AJAX-y goodness to the site where appropriate. 

And Flash, with the video comes a lot of Flash. We shall see how that goes, but it will be interesting and I’ll report on it here.

After 12 years of the same company, this could be exactly what the complacent designer needs to get in gear.

Work No Comments

Steve on March 12th, 2008

SXSW Interactive Panels

Maybe it’s me, and I think I’ll post more about the ‘South By’ Interactive panels during daylight and better thinking hours, but why does it seem to me that the best panels - meaning the most focused and prepared - are usually run by Brits?

Am I way off here?

Interests and Work No Comments

Steve on March 12th, 2008

Humboldt County - SXSW Movie Premiere

Just got back from another SXSW movie premiere and posted a review for everyone.

Humboldt County Review

Interests No Comments

Steve on March 9th, 2008

Then She Found Me, Helen Hunt’s directorial debut

Another write up from SXSW. This time it’s Helen Hunt’s directorial debut with Then She Found Me, over at the review site.

Then She Found Me review.

Interests No Comments