Homegrown smartphone OSes gaining favor in China: 59 Jakarta 10350, Indonesia Locally-made mobile operating syst... http://t.co/BruP98Es
7 minutes ago by SmartPhoneHonch on twitterZDNet is available in the following editions:
It can be advantageous to use a Dates table, especially when doing a lot of date calculations.
Learn what a Dates table is and how to create one, and then try it out for yourself.
What is a Dates table?
A Dates table stores a range of dates. Dates tables are very common in a DateWarehouse as a dimension table. You can also use Dates tables in OLTP databases for lookups.
When programmers use Dates tables, they don’t have to worry about using or designing functions for handling or formatting dates in the database.
It is a precompilation of a wide range of date values and their associated month, quarter, year, etc.
Creating a Dates table
It's simple to create a Dates table--it only takes a little TSQL programming. The script below creates the DateLookup table, which I will use throughout the rest of the example.
CREATE TABLE DateLookup ( DateKey INT PRIMARY KEY, DateFull DATETIME, CharacterDate VARCHAR(10), FullYear CHAR(4), QuarterNumber TINYINT, WeekNumber TINYINT, WeekDayName VARCHAR(10), MonthDay TINYINT, MonthName VARCHAR(12), YearDay SMALLINT, DateDefinition VARCHAR(30), WeekDay TINYINT, MonthNumber TINYINT )
As you can see from the field names, the table contains detailed information regarding parts of a date, such as the name of the month, name of the day of the weekend, the quarter number, etc.
It's very useful to have this information stored in a table for date searches based on certain months, quarters, and similar information.
The script below populates my DateLookup table with date information from the year 1900 through the end of 2100. I enter this large range of dates because I am not sure what type of dates I am going to handle in my tables, so I like to have a wide range available.
This range likely won't cover erroneous dates in my tables, but it should do a pretty good job covering a large percentage of them.
DECLARE @Date DATETIME
SET @Date = '1/1/1900'
WHILE @Date < '1/1/2100'
BEGIN
INSERT INTO DateLookup
(
DateKey, DateFull, FullYear,
QuarterNumber, WeekNumber, WeekDayName,
MonthDay, MonthName, YearDay,
DateDefinition,
CharacterDate,
WeekDay,
MonthNumber
)
SELECT
CONVERT(VARCHAR(8), @Date, 112), @Date, YEAR(@Date),
DATEPART(qq, @Date), DATEPART(ww, @Date), DATENAME(dw, @Date),
DATEPART(dd, @Date), DATENAME(mm, @Date), DATEPART(dy,@Date),
DATENAME(mm, @Date) + ' '
+ CAST(DATEPART(dd, @Date) AS CHAR(2)) + ',
' + CAST(DATEPART(yy, @Date) AS CHAR(4)),
CONVERT(VARCHAR(10), @Date, 101),
DATEPART(dw, @Date),
DATEPART(mm, @Date)
SET @Date = DATEADD(dd, 1, @Date)
END
Using the DateLookup table
Once I load data into my DateLookup table, I can run queries against it. For example, the following query lists the number of Wednesdays in the year 2003:
SELECT WeekDayName, DayCount = COUNT(*) FROM DateLookup WHERE FullYear = 2003 AND WeekDayName = 'Wednesday' GROUP BY WeekDayName
There were 52 Wednesdays in 2003.
The real power of using a Dates table comes when you use the table in conjunction with other tables. In the following example, I use my DateLookup table and join it to my SalesHistory table (from my article about generating dynamic SQL statements in SQL Server) on the SaleDate. Performing this join makes it easier for me to analyze my sales information based on the date the sale occurred.
The following query ranks the month with the highest sales per product line:
SELECT * FROM ( SELECT dd.MonthName, Product, RecordCount = COUNT(*), Ranking = DENSE_RANK() OVER ( PARTITION BY Product ORDER BY COUNT(*) DESC, NEWID() DESC ) FROM DateLookup dd JOIN SalesHistory s ON dd.DateKey = CONVERT(VARCHAR(8), SaleDate, 112) GROUP BY dd.MonthName, Product ) a WHERE Ranking = 1
This query ranks product sales per quarter per year:
SELECT FullYear, QuarterNumber, Product, SaleCount = COUNT(*) FROM DateLookup dd JOIN SalesHistory s ON dd.DateKey = CONVERT(VARCHAR(8), SaleDate, 112) GROUP BY FullYear, QuarterNumber, Product ORDER BY FullYear, QuarterNumber
Without the DateLookup table, this query would have to be written like this, which I think is more difficult:
SELECT DATEPART(yy, SaleDate), DATEPART(qq, SaleDate), Product, SaleCount = COUNT(*) FROM SalesHistory s GROUP BY DATEPART(yy, SaleDate), DATEPART(qq, SaleDate), Product ORDER BY DATEPART(yy, SaleDate), DATEPART(qq, SaleDate)
Drawback
The main drawback to using the Dates table is with the way in which I have to join it back to my OLTP tables. My Dates table does not contain time data, and my OLTP data almost certainly will; this means that joining the two tables requires some conversion on my part to do in the join. While this conversion in the join is not necessarily a huge deal, it may lead to poor performance under certain conditions.
Tim Chapman, a SQL Server database administrator and consultant who works for a bank, has more than eight years of IT experience, and he is a Microsoft certified Database Developer and Administrator. If you would like to contact Tim, please e-mail him at chapman.tim@gmail.com.
Homegrown smartphone OSes gaining favor in China: 59 Jakarta 10350, Indonesia Locally-made mobile operating syst... http://t.co/BruP98Es
7 minutes ago by SmartPhoneHonch on twitterRT @MDMGeek: Big data acquisitions pave way to fast, effective innovation - ZDNet Asia http://t.co/ky8YgPAn #Bigdata #analytics via @ciropuglisi
21 minutes ago by data_nerd on twitterIntegration, focused investments to propel Windows Phone http://t.co/6JkDa9sB
21 minutes ago by bestwaytoinvest on twitterRT @AsianFashionLaw: Malaysia offers some manufacturing benefits over China http://t.co/bMquIFiX
37 minutes ago by Serend1p1ty9 on twitterAcquisitions in the Big Data market increasingly important to enterprises… http://t.co/Br4BkXyZ
37 minutes ago by iProConLtd on twitterExperience trumps content in apps monetization http://t.co/iaCY5ebX
52 minutes ago by monetize_me on twitterMalaysia offers some manufacturing benefits over China http://t.co/bMquIFiX
52 minutes ago by AsianFashionLaw on twitterRT @MDMGeek: Big data acquisitions pave way to fast, effective innovation - ZDNet Asia http://t.co/ky8YgPAn #Bigdata #analytics via @ciropuglisi
52 minutes ago by GarnieBolling on twitterThats it.Im digging up an old bus plan i wrote around acquisition of #bigdata talent. http://t.co/gpkha5A1 Any investors want2 read/discuss?
1 hour ago by BigDataInsights on twitterIntegration, focused investments to propel Windows Phone: By Kevin Kwang , ZDNet Asia on May 23, 2012 (2 mins ag... http://t.co/aaa0Cb73
1 hour ago by jamstrit on twitterHomegrown smartphone OSes gaining favor in China http://t.co/lOBVp1T6
1 hour ago by smartfone on twitterHomegrown smartphone OSes gaining favor in China: 59 Jakarta 10350, Indonesia Locally-made mobile operating syst... http://t.co/gHypbdIY
1 hour ago by androidnewshome on twitterIntegration, focused investments to propel Windows Phone - ZDNet Asia http://t.co/7sZi6Dhb
1 hour ago by sonuise on twitterRT @zdnetasia: Homegrown smartphone OSes gaining favor in China. http://t.co/lL8KbccW
2 hours ago by AsiaBites on twitterBig data acquisitions pave way to fast, effective innovation - ZDNet Asia http://t.co/ky8YgPAn #Bigdata #analytics via @ciropuglisi
2 hours ago by MDMGeek on twitterSo much as we know , MTK6575 extremely integrated frequency1GHz ARM Cortex-A9 processor, the superiority of 3G / HSPA Modem, and help the...
1 day ago by y15822137359 on 5 SaaS adoption speed bumps to avoidI reckon your view: "CRM is strategy, not software", if a company replicating the approach uses in ERP implementation into CRM, what they...
2 days ago by wykoong on Gartner: Mobile CRM gives better ROI than socialThis video will teach you about the Excel fill handle but also provide you with a workook to download... http://www.youtube.com/watch?v=...
3 days ago by TradeBrother on A quick fill handle trick for Microsoft Excelwaiting...
5 days ago by eapete on What should count in a company's market value?Boy, you've opened a can of worms now.
Wait for the rants & raves.
I was puzzling before this whether to replicate the success formula we executed for a financial institute, and come out with a standard s...
5 days ago by wykoong on Drop the egos, copy ideas, then innovateThreats and malware know no boundaries. Neither should your web security. See how far Blue Coat Unified Web Security goes to protect your network.
Echelon 2012 - The Awesomer Tech Event in Asia
Echelon 2012 – SEA’s longest running tech startup event goes Awesomer. Catch 50 of Asia’s most promising startups & over 40 international speakers on June 11-12.
Startup Asia Jakarta showcases new product-ready tech startups. Plus: hackathon, exhibition, and speakers. Use promo code CBSi50 for 50% discount.
ZDNet Asia Intelligent Singapore video series
Featuring inteviews with CXOs who define "intelligence" in their markets and reveal how their companies drive business efficiencies through ICT.