in

ASP.NET Weblogs

Nick Wilczynski

  • SUM(Case) in SQL

    While answering some questions in the forums, I notice that there are a lot questions that have queries that could be quickly modified to make them more efficient. Queries like:

    SELECT (SELECT SUM(Column1) FROM MyTable WHERE Column2 = 'Some Value') as FieldNeeded1,

    (SELECT SUM(Column1) FROM MyTable WHERE Column2 = 'Some Other Value') as FieldNeeded2,

    (SELECT SUM(Column1) FROM MyTable WHERE Column2 = 'Some Other Value 3') as FieldNeeded3, ETC

     

    Can be easily rewritten as :

    SELECT SUM(CASE WHEN Column2 = 'Some Value' THEN Column1 ELSE 0 END) as FieldNeeded1,

    SUM(CASE WHEN Column2 = 'Some Other Value' THEN Column1 ELSE 0 END) as FieldNeeded2,

    SUM(CASE WHEN Column2 = 'Some Other Value 3' THEN Column1 ELSE 0 END) as FieldNeeded3

    FROM MyTable

     

    While it might not save much on a small table, think when the there are a million records. Running a select 3 times to pull the same fields would be very inefficient. However, this method is very efficient because the select only has to happen once, and the data is then just summarized as it is needed.

    Anybody else have some very simple tips to increase peoples query performance?

  • Florida Tweener

    For those of you who are heading to TechEd next week, there is a FREE conference on the 7th and 8th right at the same convention center. Anybody interested in attending can take a look here. For those of you who will be at the Tweener, drop me a message, and we can meet up. I'd like to meet some of the people who blog here.

  • Book Review: Pro Linq – Language Integrated Query in C# 2008 by Joseph C. Rattz, Jr.

    See book here: Amazon

    When I first got this book, I was concerned. The book was 600 pages and the first two chapters were pretty dull (if you've been living under a rock for the past year or so and have no idea what the new features of 2008 are, then you will like them). However, after those two chapters, everything really picks up and I understand why the book is so large. The book is broken down into four sections, LINQ to Objects, LINQ to XML, LINQ to DataSets and LINQ to SQL.

    In the LINQ to Objects section, the book reviews what all of the return types are, but more importantly, the book explains in detail all of the operators and keywords. And not only are they detailed, but an example is given for each one as well. This is a great source of information. Easily something I can see myself referring back to over and over again.

    In the second section, LINQ to XML, the book reviews the changes that occurred in XML. These are the changes I am very excited about. No more 200 lines of code just to create a simple XML file! I think the author did a great job of explaining this out as well. But low and behold, there are examples of this as well. After that, the book then explains the operators in LINQ to XML with examples.

    In the third section, LINQ to DataSets are explained. Following the first two sections, the book reviews the operators that are important to LINQ to DataSets. My biggest gripe with this section is that the author used untyped datasets to much. It would have been a more powerful presentation if the datasets were typed like they should be in the real world if thats what you are using (IMO).

    Lastly, the book reviews LINQ to SQL. The book goes over everything from the operations, entity classes, concurrency, and data context. I didnt read this section to carefully as I use Oracle, but from what I did read, it contained a good amount of information that would be beneficial to those that do use SQL Server.

    The biggest thing I wasn’t happy about was with the talking about subjects(Lamba expressions, DataContext, Entity Classes, etc), but not explaining them until later in the chapter or in a seperate chapter all together. I guess at least they were talked about later, but I would have liked to see them explained fully when they are first brought up.

    Based on this, the book is an AWESOME reference guide. If you need to figure out how to do something in LINQ, open the book and it will have a pretty good example on how it works. The book also does a good job of reinforcing the features of LINQ, but if you are new, you'll want to start somewhere else BEFORE going to this book. I do think that this book would be a benefit to anybody's library however.

  • Poor Code

    So while I am happy that the asp.net team opened up blogging to everybody (including myself), I'm discouraged by some of the poor code I've seen come out so far. I'm actually starting to question whether it is a good idea to have opened the blogs up, as a lot of people read them, and are going to see this code and think its the right way to do things. What does everybody else think? Does the good code and tips out weight the bad ones?

More Posts