posted 2/22/2012 by RCharCox - Views: [229]
Well, it’s 8:30 on Tuesday, and “Day Two” of my foray into the TSQL world has come to a close. Today was an interesting day. At times, I felt like I was on top of the subject matter, and then something came along and made me realize I’m not quite there yet. By the way, today’s best practice is to make sure you’re operating in the correct database. MS SQL Server Management Studio isn’t smart enough to read your mind. However, I think we all came through it, a little frazzled at the end, maybe, but alive none the less
Concatenations were easy and will help clean up your results pane, though I feel like there is a lot of value there we haven’t tapped into in class (I gave myself a project on this subject, but more of that later). Joins finally clicked for me 100% to the point where I feel I could join 100 tables together if necessary. A big thanks goes out to Brian Knight for coming into the class at the end of the day and helping us with a particularly nasty join. Specifically, one does not have to use two keys that are titled exactly the same. They can be related even if the name doesn’t match, as long as the relationship is there. He assisted us in locating that relationship information.
Right-click the key in Column list and select “modify.”
Then, Right-click the key icon and select “Relationships…”
This will bring up a window that, on the left side, lists all the relationships to this Primary Key. Again, very good information for us since we were pretty much fried at the day’s end and out of “Mental Reserves,” so to speak.
Getting back to concatenations, I was very excited to have the opportunity to pull multiple columns’ worth of information but to only have to display one field (for example, instead of having a field for one’s first and last name, have one field for their whole name). However, when I wanted to do so with a date, it gave me some trouble. Specifically, I wanted to show CalendarQuarter and Calendar Year as a QQ-YYYY value. Unfortunately, when I went to concatenate the values, the software summed the two int’s together. After doing some research, however, I have found the CAST command. This command is used to tell the software that the integer we are selecting is to be treated as a varchar, and thus not subject to the automatic mathematical operations that the Management Studio likes to initiate. For example:
Select SalesAmountQuota
, CAST(CalendarQuarter as Char(2))+'-'+CAST(CalendarYear as Char(4)) as QYYYY
From FactSalesQuota
Now, I like having each field be at least two digits, so I’m going to have to research how to pad a variable such as this (I have a jumping off point, knowing I need to change the data from an int to a char, but from there… tomorrow night). Zip codes would be another example of where this would come in handy (if you live in the New England area and your zip starts with a zero).
Good night, all. Here’s my homework. As always, feedback is greatly appreciated! Also, I apologize for the absence of screen shots (I was going to put them in, I swear!) and funky formatting below. I'll get better at this blog stuff, I promise!
Scenario One:
SELECT Sum(FRS.SalesAmount) as TotalSales
, E.EmployeeKey
, E.LastName
, E.FirstName
FROM FactResellerSales as FRS
Left Join DimEmployee as E
on E.EmployeeKey = FRS.EmployeeKey
Group by E.EmployeeKey,E.LastName,E.FirstName
Having Sum(FRS.SalesAmount) > 1000000
Order by Sum(FRS.SalesAmount) Desc
Scenario Two:
SELECT EnglishProductName
, EnglishProductSubCategoryName
, EnglishProductCategoryName
, Sum(RS.SalesAmount) as ResellerSales
, Sum(INS.SalesAmount) as InternetSales
FROM DimProduct as P
Join DimProductSubcategory as PSC
on P.ProductSubcategoryKey = PSC.ProductSubcategoryKey
Join DimProductCategory as PC
on PC.ProductCategoryKey = PSC.ProductCategoryKey
Join FactResellerSales as RS
on P.ProductKey = RS.ProductKey
Join FactInternetSales as INS
on INS.ProductKey = P.ProductKey
Group By EnglishProductName, EnglishProductSubcategoryName, EnglishProductCategoryName
Order By EnglishProductName, EnglishProductSubcategoryName, EnglishProductCategoryName
Scenario 3:
SELECT sum(FRS.OrderQuantity) as TotalOrderQty
, ST.SalesTerritoryRegion
Left Join DimSalesTerritory as ST
on ST.SalesTerritoryKey = FRS.SalesTerritoryKey
Left join DimDate as D
on FRS.OrderDateKey = D.DateKey
Where ST.SalesTerritoryCountry = 'United States'
And D.FullDateAlternateKey = '01-01-2006'
Group By St.SalesTerritoryRegion