The Denormalizer
Pretty much everyone now days are writing code against relational databases. All those developers should be architecting their relational databases using 3rd normal form(3nf). If you are not using 3rd normal form it’s time to get your “shtuff” together. All applications should be storing their data properly, but when it comes to reporting most people want the data de-normalized. A common request that I have had over the years is to show a comma separated list of related items in a cell. This would typically result in some database function that would help out with this. I found a while ago this technique that has really helped out with this request and is easy to implement and performs very fast. We will be using the “Northwind” example database for our datasource.
Here are the tables we will be using
Below is a small example query that will de-normalize orders by customer. The magic is happening with the substring correlated sub-select outputs the data using FOR XML and concatenating the data on itself.
SELECT
CustomerID
,SUBSTRING
(
(
SELECT
(', ' + CAST(OrderID AS VARCHAR(120)))
FROM Orders AS o2
WHERE o.CustomerID = o2.CustomerID
ORDER BY
o2.OrderID
FOR XML PATH( '' )
),
3,
1000
) AS OrderIDs
FROM
Orders AS o
GROUP BY CustomerIDHere is a small sample of the data that is returned.
Here is more of what a formatted report might look like
SELECT
c.CustomerID
,c.ContactName
,c.ContactTitle
,derCustomerOrders.Orders AS FiveMostRescentOrders
FROM
Customers AS c
LEFT JOIN
(
SELECT
CustomerID
,SUBSTRING
(
(
SELECT TOP 5
(', ' + CAST(OrderID AS VARCHAR(120)) + ' (' + CONVERT(VARCHAR(10), OrderDate, 1) + ')')
FROM Orders AS o2
WHERE o.CustomerID = o2.CustomerID
ORDER BY
o2.OrderID DESC
FOR XML PATH( '' )
),
3,
1000
) AS Orders
FROM
Orders AS o
GROUP BY CustomerID
) derCustomerOrders
ON
c.CustomerID = derCustomerOrders.CustomerID
ORDER BY
c.ContactNameHere is a sample from the results












“The significant problems we face cannot be solved at the same level of thinking we were at when we created them.”
Now is the time to look at what you’re doing within your organization see what you can streamline, optimize and simplify. What we used to know as far as how technology and software used to work is no longer the case. Take a look back at the quote for this entry and note that what you thought a year ago to be a fact may need to be revisited. Pick up a book, read a new blog, or get a new General Ledger statement and demand change.
Every website worth anything has a database of some kind. A lot of programmers do not like to think too much about this part of the application. I am an architect and part of that is dealing with everything as a whole and analyze if it fits with a solution. We will be focusing mainly on the storage in this entry and more specifically the storage of date/time values.
Doing these few steps to ensure there will be less confusion within your application and allow users to view data relevant to where they are working from . You will also be able to be more portable and move your websites or databases to the cloud or hosting environments outside of your time zone.
How would you like to be able to start working on something and finish at another computer right where you left off? Typically cloud company = big
So if any of these things you might find useful and you want to ease your pocket book and make technology work for you. You might want to look at the 
Facebook
RSS
Recent Comments