ERD & SQL Fiddle provided. We're trying to design a reporting tool for our stakeholders around a normalized Key-Value Pair model. We've tried to find a solution using Pivot Tables but can't seem to find a way to accomplish a query in SQL Server.
http://sqlfiddle.com/#!2/6ed2f (SQL Fiddle's server is having some issues.)
Metaphor for the business problem: we have a collection of surveys that collect responses from users. 1 survey has many questions, 1 question has many responses, each of the responses is from a user, and the user has many responses.
A survey can also generate an output of the surveying experience. Each survey has different output keys. 1 survey has many output keys, 1 output key has many values, and each output value is associated with a user.
The input/output values and other data is tied together to the user through a "Survey Submission".
(Random fact: output values and response/input values are both stored as JSON strings to preserve a dynamic data type.)
The stakeholder would like to see the aggregated values for a given survey.
A solution for just the Inputs would be fine, although a solution that works for Inputs and/or Outputs would be great.
Best Answer
Hello from 2 years later! I've lived and learned and realized that a key-value model is probably actually an ideal purpose for NoSQL. But our org/team didn't have access to a NoSQL Document DB, past or present.
The way that we have it set up makes it easy to create new "surveys", but difficult to report on. The best solution I can come up with, as efficiently as I can, was to create a view for each survey....
But wait...
I got lucky that our "Question Keys" happen to be one-word (PascalCase) without any punctuation/spaces. So check out this dynamic SQL monster, that scrapes the question keys for a given survey and turns them into column headings.
Psuedo-output...
Thoughts? Is this the best of SQL Server mousetraps?