Paw Prints

Friday, November 21, 2014

SQL Poetry Friday #4

Hello and welcome again to SQL Poetry Friday. I sadly do not have a code poem today, and it turns out that the poetry is suitable to be sung to America's classic hit "A Horse with No Name". It goes like this:

Well I've built me a table
With no column names,
where results never
came out the same.
In the sandbox, they won't
remember your name
for you're not in production
to cause them all pain...

Please come back next week for another exciting parlance into prose.

Woof woof!
Jason

Thursday, November 20, 2014

Count (DISTINCT) saved my brain.

I'm building a "dashboard" which counts up objects in a time period. It happens that the objects have parts (that is to say 'part A', 'part B', etc.) that reside in a separate table. When I would join the parts table into the object table, all of the sudden my count for each object would increase, totally blowing my mind! Using Count(DISTINCT object) brought me back to my original counts and my brain was saved from the pounding of frustration.

Here’s to you, Count(DISTINCT), a real database hero!

Woof woof!
Jason

Tuesday, November 18, 2014

Hour of Code coming December 8-12!

I was notified over the weekend that Kahn Academy is facilitating the Hour of Code for kids the week of 12/8-12/12. Hour of Code introduces kids to some aspect of computer programming to help foster interest in future projects. It sounds like the perfect opportunity to introduce SQL to the next generation.  Yes, you read that right, they have a setup for SQL (as well as javascript and html/css, but I’m partial to the former)!!! Please consider getting involved with your kids or kids in your area, and make plans with the site below.



Woof woof!

Jason

Friday, November 14, 2014

SQL Poetry Friday #3

Hey, poetry lovers! This was a fun little ditty that I created out of Pinal Dave's search for column names in a particular database by table.  I'll post the code and my own results from my master database.

SELECT
c.name AS 'When I Am'
FROM sys.tables t
INNER JOIN sys.columns c 
ON t.OBJECT_ID = c.OBJECTID
WHERE c.name LIKE 'l%'

Results:


If your mind makes the leap like mine did reading quickly down the column, I read it as "When I am low, I start; last run."  Try for yourself and see how your database is feeling.

Woof, woof!
Jason

Wednesday, November 12, 2014

Ordering days of the week

This is a small problem that I’m sure has been solved in a number of different ways, but here is my own take on coding the days of the week in order. Having to categorize information by days can be difficult, as of course, it’s not a list that you can use ORDER BY. Here’s my code:

CREATE TABLE #TMP (ID INT, ADAY VARCHAR(20))
INSERT INTO #TMP(ID, ADAY)
              VALUES (1, 'Sunday'),
              (2, 'Monday'),
              (3, 'Tuesday'),
              (4, 'Wednesday'),
              (5, 'Thursday'),
              (6, 'Friday'),
              (7, 'Saturday')
SELECT
       ADAY
FROM #TMP
ORDER BY ID
DROP TABLE #TMP
Woof woof!
Jason

Monday, November 10, 2014

Building Process and Strategy

Mondays are my day to try and expound or extol or ex-something to shed more light on SQL and it’s pros and cons.  This may be a tougher situation to tackle on a Monday than a sum up of the week, as my weekends are usually free of SQL.  I will say this, however.  I’ve been listening to a LOT of books (I listen to audiobooks as I hardly ever set any time apart to read, so listening is how I have to feed the rest of my head), and many of the business books I’ve been pushing through have talked about forming strategies and then sticking to them.  I’ve been trying to apply this to query and report building, as I have always tended to more of a shotgun approach rather than a rifle; a scattered, hope something sticks pursuit rather than a narrow, step by step.  It’s a detriment that I’ve been working on probably for the last 12 months.  Agile development practices have helped me get started, especially giving myself a 3 point to do list every workday to push towards, but I really need to put something into templates or systemization of reports.  I’ll keep you posted on how this continues to develop, as it is a process I think we all work towards (or at least us hounds who doggedly pursue bigger better things).  Keep digging until that bone is firmly in teeth, my fellow hounds!

Woof woof,
Jason

Friday, November 7, 2014

SQL Poetry Friday #2

This is a fun one! Grab the code and drop it into SSMS.  It's a fun random poem with my thoughts on leaves.  It's

CREATE TABLE #poem
(ID int, WORDS VARCHAR(20))
GO
INSERT INTO #poem
       (ID, WORDS)
VALUES
       (1, 'Chase'),
       (2, 'Fall'),
       (3, 'Crumple'),
       (4, 'Blow'),
       (5, 'Pile'),
       (6, 'Whirl'),
       (7, 'Dance'),
       (8, 'Drift'),
       (9, 'Scatter'),
       (10, 'Play'),
       (11, 'Burn'),
       (12, 'Roll')
GO
DECLARE @Random INT;
DECLARE @Random2 INT;
DECLARE @Random3 INT;
DECLARE @Upper INT;
DECLARE @Lower INT
SET @Lower = 1
SET @Upper = 12
SELECT @Random = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
SELECT @Random2 = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
SELECT @Random3 = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
SELECT
       WORDS AS LeafActions
FROM #poem
WHERE ID = @Random
UNION
SELECT
       WORDS
FROM #poem
WHERE ID = @Random2
UNION
SELECT
       WORDS
FROM #poem
WHERE ID = @Random3
DROP TABLE #poem

Thursday, November 6, 2014

SSRS manipulation: Counting your groups

Wow! Now we're really rolling... almost talking about code!  It is important that we talk about using the tools around the code as well, so this tip involves Reporting Services. So I report a bunch, and I put together this awesome report with drill downs, but I wanted to show the count of each drill down. It took some trial and error, but I found using RunningValue with CountDistinct doing the trick! Let me show you some snaps.

Here's what the report looks like before expansion. Note that in the rectangle there is a basic roll up of the counties that returned data for the report:


And here again, more roll up for the zip codes that returned data:


This can be achieved as stated above with a running value using count distinct, especially on the zip code area that had multiple objects returned for each zip code to be counted.  I used the following code for the zip code counts as an expression:

="Total Zip Codes: " & RunningValue(Fields!PKUPZIP.Value, countdistinct"table1_County")
Note that the grouping is actually the parent group in this case.  That was sticky to figure out, as I kept getting out of scope errors until I had the right grouping.

I hope to continue to do a code based update every Wednesday, and a Monday morning quarterback analysis post to start each week.  And don't forget, more poetry on Friday!

Woof, woof.
Jason

Tuesday, November 4, 2014

The long overdue review of SQLSaturday #333 St. Louis

Wow, that was completely unexpected.  Almost a month has gone by since I attended St. Louis' SQLSaturday (#333) and it was a blast!  It totally deserved a review sooner than this, but work and home just swamped me ever since.  So, here's a brief rundown that I'm putting together in five minutes...

First off, I got to volunteer and I don't think I could have gotten in with a nicer group of folks dedicated to making the day great!  They really had made the day, as without the facilitation of the event, it would've devolved into chaos.  I got to hand out shirts to all the attendees, which was also great because I got to meet not only people from St. Louis, but from all over the country.  That blew my mind that they were able to come in and learn, as I've only been able to attend when it's been in town (as now I've attended two in two different cities).

The presentations were great overall.  Each deserves more than the little I go over here, so make sure to find these either online or see if the people are presenting them somewhere else:

  • Agile Development with Aaron Stanley King: being an agile practitioner myself (practicing, not a master by any stretch) I was hoping to get some methodology with Aaron's presentation, but it tended towards overall good practices in code development.  The high point for me was hearing about using CLR in a stored procedure will protect the code by showing blank when looked at in system.  This made me want to read more about it.
  • Partition Management with Dan Guzman:  I had heard Dan talk about this same topic this summer at a user meeting, and it's great for mega data situations.  He's really on top of database management today.
  • Pivots/Unpivots in SQL with Jeff Foushee: Pivots are a topic that leave me mystified, and Jeff did a terrific job in explaining what really happens behind the scenes.
  • Intro to file tables in 2012 with Warren Sifre: I got a chance to talk with Warren as we attended sessions together, he's a huge wealth of knowledge, and a great guy.  File tables bridge the gap to the windows explorer, which was mind blowing to learn about and I hope an opportunity comes up for me to work with them in the future.
We closed out the day and while I wished I could have attended the after celebration, I had to get home.  Please do not hesitate to get to SQLSaturday in your own area, or if one isn't coming, get involved with your local user group to make it happen.  It's well worth the investment.

Woof woof,
Jason