Tuesday, June 28, 2016

Concatenate 2 Integers Into 1 Varchar (SQL)

Yesterdays Just A Tip Felt so good, I thought I would do it again today.  Twice in one week.  You guys & gals get a great tip, while I get another notch under my belt.  Win.  Win.

Today's coding issue involves 2 Integer fields that need to be concatenated into 1 big field and that field needs to be Varchar.  To accomplish this, you will need to Cast each  Integer as a Varchar then concatenate them.
For Example, in an SQL table called tblPetOwners you have 2 fields (OwnerNumber & PetNumber)   which are Integers and wish to join them with an '&' in between them.  To do this, you have to CAST them both as Varchar before concatenating them.  Below is the best way I have came across to accomplish this task and I show you the data in the table, the code itself, followed by the results.

Table tblPetOwners Data:


OwnerNumberPetNumber
223122543224
643233235432
332342654523
Example Code:

SELECT CAST(OwnerNumber AS VARCHAR(10) ) 
+ ' & ' + 
CAST(PetNumber AS VARCHAR(10) ) OwnerAndPet, OwnerNumber, PetNumber 
FROM tblPetOwners



Results Data:


OwnerAndPetOwnerNumberPetNumber
223122 & 543224223122543224
643233 & 235432643233235432
332342 & 654523332342654523

If you like this post and want to see more, follow me on my website www.chadcompton.com
Or if you prefer...


Monday, June 27, 2016

Union & Count Together in A SQL Query

I know it has been a while since I have given you a good tip...So without further ado.

Today I had a situation where I needed to give a count of items that were across a couple of tables and involved a few unions in SQL.  Since my query took up about 2 pages, I figured I would make a simple version for all you folks out there reading this.  I hope it helps.

For Example, let us say you have two tables ( Results & ArchivedResults) and were needing to get a count on them both.  Let us also assume you didn't use a JOIN because of the different criteria in each SQL statement.  Based off those results you need to get a count.  Below is how this can be accomplished.

Example Code:
SELECT PullResults.Name, COUNT(*) 
FROM  (SELECT Name FROM Results
UNION ALL
SELECT Name FROM ArchivedResults) as PullResults
GROUP BY Name
ORDER BY Name

If you like this post and want to see more, follow me on my website www.chadcompton.com
Or if you prefer...


Tuesday, May 24, 2016

SUM Using Group By (SQL)


At some point, you will be required to use the SQL GROUP BY clause with the SQL SUM function.  My time to use this was today and I thought I would create this post to help anyone else that may be needing to accomplish the same.
For example, you could also use the SQL SUM function to return the name of the department and the total sales (in the associated department).

Example Code:
SELECT department, SUM(sales) AS "Total sales"
FROM order_details
GROUP BY department;
Because you have listed one column in your SQL SELECT statement that is not encapsulated in the SQL SUM function, you must use the SQL GROUP BY clause. The department field must, therefore, be listed in the SQL GROUP BY section.
If you like this post and want to see more, follow me on my website www.chadcompton.com
Or if you prefer...


Tuesday, April 26, 2016

AVG Function (SQL)

Over the last couple of coding posts I have showed you how to use the SUM function.  This being said, I thought I would show you how to use the AVE function in SQL.  


Use the AVG() Function to get the average of a of a specific numeric column.  Below is an example of it being used.

SQL AVG Example:


SELECT  AVG(ColumnName) 
FROM TableName;


If you like this post and want to see more, follow me on my website www.chadcompton.com
Or if you prefer...


Wednesday, April 6, 2016

Markers Mark Tour

Please Note this tour was done with YELP of Lexington, therefore your experience will not be exactly the same.


This was an Official Yelp Event (OYE) put on by Heather P our  Lexington's Great Yelp Community Ambassador.  I swear these events keep getting better and better.

This specific event started out with a meet up in Isle 35 at Keenland where we were greeted with Bloody Marks (aka bloody mary's made with good old Makers Mark) and North Limestone Doughnuts provided by Jacklyn E., our lovely tour guide from Makers Mark.  This was truly a perfect tailgate breakfast before we hopped on the Mint Julep Tour bus and were on our way to Loretto, the home of Maker's Mark Distillery.  

The trip itself was mostly through the beautiful country side down small winding roads most of which is part of the Kentucky Bourbon Trail.  Mint Julep Tours provided us with a great driver and the perfect tour guide leader (Bob and John).  They pointed out great tid bits and facts  about the local area as we drove through the small towns.  They also pointed out great destination places along the way, like the man with 500+ riding lawn mowers outside his house.  I have to admit as a geeky road trip kid, that was a great stop. lol. We also learned about bourbon on the way with the help of Jacklyn.  Great facts before we even got to Maker's Mark.

Once we arrived at Maker's Mark Distillery, we grabbed some tasty Maker's Mark Bourbon Coffee and started our Distillery tour.  This is where Jacklyn shined.  She was so knowledgeable of Maker's Mark history and the bourbon making process, it was amazing.  Being on a special tour, we got to go into a couple buildings that most tours will not allow.  The actually Distilling Building was one and the Quality Control Area was the other.  

In the Distilling Building, we got to see the mill that does all the corn and grain grinding for every batch of bourbon that gets made.  We got to see and taste the 1 day mash that was bubbly and sweet (Odd I know).  Then sampled one that was more bitter and corny (BLAH), video proof below.   We saw the White dogs which is the liquid at 120 and 130 proof which we also got to sample.   Be aware this isn't on the normal tours. 
Next room was the labeling building.  Every label on every bottle is printed and then cut on an old repurposed printing press.  This cutter only cuts a max of 4 sheets at a time.  A time consuming process but well worth the effort.

The super secret Quality Control Building was next.  These ladies in here are like CSI of the booze world.  Very impressive.  They do samplings of all the batches at many stages of the process.  They also keep one bottle of every batch for referencing.  They have 25 workers that sniff, sip and sample bourbon every morning.  Those lucky people.  What a job.   This was also the place where Maker's 46 was invented.  
The storage barn was next.  There we got to see the inside of a barrel and learned how the rotate the casts manually by hand from upper levels of the barn to the lower level.  They don't use climate control facilities here. It takes average of 6 years or so to get the flavor profile that makes Maker's Mark so special.  There isn't an exact day per say, the bourbon will tell them when its ready by its flavor.  At that point is is diluted with their distilled water to get the proof they require.  Unless you are lucky enough to buy Straight Cast (like I did).  

The Bottling/Dipping building was next stop on our tour, a few of the videos are listed below.  There we saw the empty bottles move from cleaning to filling and sealing.  Then hand dipped in the signature red wax after the label has been slapped on.  It was a fancier version of the beer scene from Lavern and Shirley opening scene.  It was really impressive.  

Then we arrived at the tasting room.  Here we got to sample 5 different bourbons (2 which aren't normally on the tastings).  Under Mature, Fully Mature, Over Mature, Maker's 46, Cast Straight.   Jacklyn rocked out here as well.  She explained to us how we should sniff the bourbons before sipping.  Basically how to experience bourbon with all our senses.  I loved it.  Each bourbon tasted like bourbon, but had different hints of flavors and smells.  Pretty neat. They gave us Maker's Mark distilled water also.  WHO KNEW?  Freakin Water from their Super Secret Limestone Lake.  

Next we helped our selves to Bourbon Balls as we walked into the Beautiful hall filled with hand blown glass made by Stephen Rolfe Powell.  It is so beautiful.  Hundreds of unique, brightly colored glass lines this ceiling in this hall way.  So stunning.  There are also hidden angels in here as well.  I forgot what their meaning was.

The gift shop was the last stop on our Maker's Mark Distillery Tour.  We were given a bottle of Maker's and got all gussied up and were able to dip our own bottles in the signature red wax.  Such a great experience.  Here we also shopped and bought other goodies.  I got Round Ice trays and Maker's 46 Straight Cast (which I also got to dip).

Then we hopped on the bus and headed home.   Lunch boxes provided by Joe's Deli. (no more room) Thanks ALL.  Keep a look out for my next article about touring Wild Turkey.

Video Snipits:


Random Pictures Below:

If you like this post and want to see more, follow me on my website www.chadcompton.com Or if you prefer...


Monday, January 4, 2016

SUM Using A Formula (SQL)

Today I was asked to pull some data and give a total difference of two different columns in a table in SQL.  Then I realized that not everyone would have came across this.  Even though in the past I have showed you how to use the SUM Function in SQL.  This time I thought I would go one step further and show you how to get a SUM while using a formula in SQL.

Use the SUM() Function to get the total of a specific numeric column.  With that being said, the Sum function doesn't need to be a single field.  In this case, you can use a formula.   Kind of like, getting total Profit by subtracting Expense (Column1) from Total Sales (Column2).   Below is the specific syntax to use.

SQL SUM Example:


SELECT  SUM(Column1 - Column2) 
FROM TableName;


Please note, you can also preform groupings while using this function as well.  I will show you those at a later date.

If you like this post and want to see more, follow me on my website www.chadcompton.com
Or if you prefer...