Category: Blog

  • Counting the Existing, and Missing, Rows

    I saw this as a problem recently from someone and thought it would make a good post. As I was building a quick solution, someone else posted theirs, but I decided to save mine as a blog post. It was a good, quick, T-SQL exercise for me to work on.

    I decided to take the Christmas season and use that as my example. My wife and I buy presents for the family and we try to understand what we’ve bought each year to balance out our efforts for each kid.

    My setup:

    CREATE TABLE People ( id INT , firstname VARCHAR(20) ); CREATE TABLE presents ( id INT , present VARCHAR(20) , value NUMERIC(6, 2) ); INSERT INTO People VALUES ( 1, 'Kyle' ), ( 2, 'Delaney' ), ( 3, 'Kendall' ), ( 4, 'Tia' ), ( 5, 'Steve' ) INSERT INTO presents VALUES ( 1, 'Book', 10 ), ( 1, 'Fire', 157 ), ( 3, 'Book', 8 ), ( 3, 'tablet', 162 ), ( 3, 'hat', 12 ), ( 4, 'bracelet', 80 )

    I’ve modeled this with two tables: one holding people and one with the presents. I need to join them together and see what I’ve bought.

    SELECT p.firstname , presentcount = COUNT(ps.present) , value = ISNULL(SUM( ps.value), 0) FROM people p INNER JOIN presents ps ON p.id = ps.id GROUP BY p.firstname

     

    That gives me a count of gifts and money spent

    giftsa

    The problem is that it doesn’t let me know what people I haven’t bought for. For that I need to change to an outer join, in this case I’ll choose a left outer join since the people table is the one I need all rows from:

    SELECT p.firstname , presentcount = COUNT(ps.present) , value = ISNULL(SUM( ps.value), 0) FROM people p LEFT OUTER JOIN presents ps ON p.id = ps.id GROUP BY p.firstname

    Now I can see that Kendall and Steve haven’t received any presents yet. More work to be done:

    giftsb

    Outer joins are a quick way to find issues, but be sure you understand how they work. In this simple case, it’s an easy change.

    Now this looks like software I might actually use. Perhaps this would make a good project for me?

  • Powershell in a Month Day 10 – Formatting

    This is part of my Powershell Challenge, to learn more about PowerShell (PoSh) using the Learn Windows Powershell 3 in a Month of Lunches book by Don Jones.

    Not an exciting chapter for sure. This chapter deals with the formatting of results, or objects, for display (or saving to a file or printing). It’s mundane, and not the most interesting stuff to learn. As an analogy to the SQL world, it’s like learning the difference between

    select *
    and
    select name, id, status
    We typically don’t deal with a lot of formatting in SQL Server. We let SSMS handle things, and we include, or don’t include, results. This chapter shows how to do that, and builds a little on some of the things from previous chapters.
    The Get-Process and Get-Service items are the main examples. The chapter starts with some theory, looking at Get-WMIObject and how the system knows whether to display things in a list or a table, but for the most part we don’t alter those default formats natively, so that wasn’t very interesting.
    However, the format-table and format-wide items are interesting. I could see those being handy when you are trying to cull through information and want to only deal with certain items. I practiced a bunch of the formatting, playing with the results, and practicing expressions. Especially when we get to the point where we want to do math and calculate the results of something. Like normalizing memory or recompiles, or something else.
    The chapter looks at grouping, choosing columns, breaking into separate tables, and in general a light understanding of formatting capabilities. There isn’t much time spent on format-list or format-wide, and not much on out-GridView, do you have to do those on your own. I think out-gridview will be really handy to try and work with a set of data, resort it on the fly, and without re-running commands.
    Overall an easy chapter, light on content, but handy in that it will teach you things that you’ll want to do at some point and won’t want to figure out on the fly. I did OK with most of the lab, but being in a bit of a hurry, I didn’t want to figure out how to get a list of log files and cheated, looking at the answers. I should have dug into help, but help is annoying at times. I’ll have to get over that.
  • Cleveland Rocks! (and does SQL Server)

    My first flight of 2014 will be to Cleveland to run a SQL in the City seminar and then speak at SQL Saturday #241. Both are free events, taking place on February 7th and 8th, respectively, and it’s a great chance for you to improve your SQL Server skills, network, and move your career forward.

    SQL in the City Seminar

    Grant Fritchey and I did SQL in the City seminars last year (and 4 big events) and we kick off 2014 in Cleveland. Come see our Deployment Stairway half day session on Friday, Feb 7, 2014. It’s free, but we’d like you to register.

    We’ll be presenting 3 sessions that build on one another. We’ll set the base with Version Control Systems and databases, giving you some things to think about. We then go into Automated Build and Test, essentially Continuous Integration, which is an amazing process and one you’ll want to get set up in your environment. We finish off with a look at deployments, giving you some ideas on how to smooth and automate this process.

    We’ll cap the afternoon off with a short happy hour, giving you the chance to talk to Grant and myself about anything SQL Server, or anything else.

    SQL Saturday #241

    My second time to a Cleveland SQL Saturday will be #241. I think this is their third or fourth in total, but timing hasn’t worked out well for me. However I’m excited to go back and see friends and meet some new people.

    As an added level of excitement, the T-SQL guru from SQLServerCentral, Jeff Moden, will be speaking. Come learn from one of the masters and speed up your T-SQL code.

    Of course, there will be a ton of other speakers there as well. With 7 tracks and 6 time slots, you’ve got the magic number (and answer to the universe) of sessions to choose from. Speakers from all over the country will be there and you’ll have to really think about how to spend your day.

    I have 2 sessions in the morning. I’ll be talking about encryption at 8:45 and then branding at 11:15. Learn about how to better secure your data and the options that are available for encryption in SQL Server, and then learn about how you can stand out, improve your brand, and find a fantastic job for you.

    My apologies in advance that I won’t make the after party, but my daughter has a volleyball tournament in Colorado on Sunday and I’ll have to leave a little early to catch a plane home Saturday night. However I’ll be around during the day and please feel free to say hi and introduce yourself.

    As I mentioned, both events are free, so if you’re interested in database deployment, sign up for the seminar and if you want to work with SQL Server, register for SQL Saturday.

    Pre-cons

    If you’re looking to be a better DBA and want to learn about the internals of SQL Server and how to better recovery data, think about attending the pre-con from MCM Argenis Fernandez on Friday. It’s an inexpensive training session that will teach you a lot.

    If you want to learn about Powershell, Allen White is putting on an Automation pre-con using PoSh on Friday as well. Learn how to better manage your instances with a little scripting and thought.

  • SQL in Albuquerque

    It’s a little less than two weeks until SQL Saturday #271 – Albuquerque. The second event in that great city, and the beautiful state of New Mexico. I rarely get there now that my brother moved away, but’s always neat to visit.

    I was lucky enough to get chosen to speak and attend the first event last year. When I got my acceptance, a few of us from Colorado got together and planned a #sqlski day on Friday. It worked out well and we have plans to do it again. A few less of us this year, but a few new faces as well. Some of us will be driving to Taos on Thursday night and skiing Friday before driving the rest of the way to Albuquerque.

    This event was a lot of fun last year with an amazing lineup of speakers from all over the country. It was amazing to see how many speakers came from different states, though I do prefer to see more local speakers. Perhaps we’ll inspire a few more New Mexico SQL speakers to jump into the event next year.

    In any case, we have people from Missouri (Bill Fellows), Nebraska (John Morehouse), Texas (Tim Mitchell), Utah (Pat Wright), California (Denny Cherry and Lori Edwards), North Carolina (Bill Pearson) and of course, Colorado. There are at least 13 of us coming down from the north, by my count, and a few more that might be attending.

    I’ll be talking Encryption at the first session at 8am, and from there you have a great choice of lots of other sessions.

    If you’re anywhere near Albuquerque, I’d encourage you to register and come. It will be a fun day of SQL Server learning in New Mexico.

    And it’s FREEEEEEEEE.

    If you want to ski on Friday, keep an eye on the #sqlski hashtag on twitter and we’ll let you know where we are.