Hug a Developer

Here’s a great video that pretty much sums up how my current project is going.

HugADeveloper

http://blip.tv/file/1061088

Return the Earliest Date Record in SQL Server 2005

I’ve been struggling with a query lately and I haven’t been able to find a good answer. 

What I’m trying to find is the earliest record in a detail table, then return all the master and detail records associated with it, but only that single “detail” record.

Here is the table schema:

Table Structure

So the query I am trying to write is trying to get all distinct Order Detail items, but only the earliest record of the Order Header.

When the query is executed as run above, here are the results:

sqlQuery2

However, I only want to return the earliest OrderDate Header record, so the CSR will display as well. 

Here is the required resultset:

Name TotalAmount CSR OrderDate ItemName Quantity
Fatty McGhee 322 Someone Else 2008-05-01 Widget 1 40
Fatty McGhee 322 Someone Else 2008-05-01 Widget 2 34
Fatty McGhee 322 Someone Else 2008-05-01 Widget 3 20
Fatty McGhee 322 Someone Else 2008-05-01 Widget 4 30
Fatty McGhee 343 Capt. No Normalization 2008-06-01 Widget 5 34

I would like this to be a single view/query and not a stored procedure due to customer requirements. Any help with this would be greatly appreciated.



UPDATE:

I thought I would try to explain this a little better.  So I want to get information about the order detail item, “Widget 1″.  I only want to return this record once (distinct), and I want it to include the earliest SINGLE record from the Order Header table. 

If I try to do a group by to get the MIN(OrderDate) and try to return CSR in the result set, it will be incorrect and return 2 Order Header records because CSR is different:

Name TotalAmount CSR OrderDate ItemName Quantity
Fatty McGhee 322 Someone Else 2008-05-01 Widget 1 40
Fatty McGhee 343 Capt. No Normalization 2008-06-01 Widget 1 50

So, why not do a “PARTITION BY”, RANK and add a WHERE clause returning only where RANK = 1?  Because of the size of the order table, returning the partitioned table before getting the RANK of the table without filtering down the results first by User takes too long.  Because I want to filter out for a single user, I would need to pass a variable into the subquery, MyResults before running the sub query.

SELECT [Name], [TotalAmount], [CSR], [OrderDate], [ItemName],
  [Quantity]
FROM
(
    SELECT U.[Name], OH.[TotalAmount], OH.[CSR], OH.[OrderDate],
      OD.ItemName, OD.Quantity,
      RANK() OVER (PARTITION BY OD.[ItemName]       ORDER BY OrderDate) AS RANK
    FROM OrderDetail AS OD
    INNER JOIN [OrderHeader] AS OH
    ON OD.[OrderID] = OH.[ID]
    INNER JOIN [Users] AS U
    ON OH.UserID = U.ID
) AS MyResults
WHERE RANK = 1
ORDER BY [OrderDate]

So it looks like this can’t be made into a view and must be used in a stored procedure in order to filter the RANKed results.

Thanks everyone.

Testing out Code Formatting

Thanks to RossCode for recommending this plugin for Windows Live Writer, “Insert Code for WLW

Too bad I don’t have a wider blog theme, but I really like this one.  Testing out the VB functionality since I don’t really see VB examples that often:

 
   1:          ''' <summary>
   2:          ''' This method checks to see if the given URL is available and accepting requests.
   3:          ''' </summary>
   4:          ''' <param name="url"></param>
   5:          ''' <returns></returns>
   6:          ''' <remarks></remarks>
   7:          Public Function IsUrlAvailable(ByVal url As String) As Boolean
   8:              Try
   9:                  Dim req As HttpWebRequest = WebRequest.Create(url)
  10:                  Dim rsp As HttpWebResponse = req.GetResponse()
  11:                  If rsp.StatusCode = HttpStatusCode.OK Then
  12:                      Return True
  13:                  End If
  14:              Catch ex As WebException
  15:                  ' Eat it because all we want to do is return false
  16:              End Try
  17:              Return False
  18:          End Function

Creating a Yellow Fade with ASP.NET AJAX Toolkit

I’ve always thought that the Yellow Fade Technique has been a great way to present changes to a web page.  It’s used in all of 37Signal’s applications such as BaseCamp and CampFire.  It highlights the changes in a page using a yellow background, then fades out to the normal background.  This gives a visual cue to the user that something has changed on the page.  You can also use it to get the user’s attention (which is what I use it for).

I searched to find the JavaScript to use in my own application and couldn’t find anything that was easily integrated into the “controls” structure of ASP.NET.  Anything that I was going to use would need to hack into the body tag and the header tag to place functions initializing it.  Here’s a quick way to accomplish this using ASP.NET Ajax and the Control Toolkit.

Read the rest of this entry »

The Post that Should Not Be

I’ve been trying to come up with excuses why I can’t come up with “examples” or “code improvements” for the ASP.NET community or more importantly: “What do I bring to the table?”

I’ve been watching a lot of the conversations on the ALT.NET/CLI group and it actually makes me feel worse as a programmer.  We’re a “young” group and people are trying to tell each other to do our jobs.  What really is the best way to approach coding?  What makes you a good programmer? (I posed this question earlier on Twitter).

I’ve tried to think of real world examples of code that I’ve used that has been a “fore-runner” in the community, but I’ve run into a roadblock every time.  I write new code every day, but it seems so old and archaic to me.  Is that silly?

Most of the code that I write involves the controls that I use in my application, and nothing to do with the code to retrieve data or work in the DAL.

What does this have to do with current events in the .NET community?  Pretty much EVERYTHING!

If you haven’t been following, there have been multiple conversations on Yahoo’s message boards about HOW you program.  People have left message boards because of their stance. It amazes me that people’s opinions about computer science can steer a career in a different direction.  Being on the outside, looking in, I see that it’s mostly an attack on how someone’s approach on a problem is being criticized, not the end result.

Take Frans Bouma for example. he has “exposed”(and not in a bad way) his view on programming and how it should be approached in different forums and been shot down in the most heinous way.  He was only stating the real world examples on what he has worked on and the “ALT.NET” community took a crap on him. 

I don’t think he was treated fairly.  He gave his opinion, people shot it down, and now, because of his opinion, he’s anti-”ALT.NET”.  That was hardly his point.  He simply wanted to give a point of vie that didn’t apply to the “ideal” situation.  I agree with his stance since not every every programming situation calls for TDD and interfaces and mocks.  I have been in plenty of different situations where I don’t have time to build the interface, DAL, BLL and tests in time for it to ship to the customer. 

In these situations, you have to feel comfortable that what you have built is what the customer wanted in the first place and not about how you feel how you programmed.

Which brings me back to my original question on Twitter:  “What makes you a good programmer?”