I have encountered a similar problem many times. The way I look at this is that you can clearly compose a book. So, for example, you have 10 types of prices. Are these types static? Do they ever change? If they do not change, I prefer to have them as columns, in pricing (we will not call it details). This will include an effective start date and an additional end date. This will allow you to process pricing changes in a timely manner.
Now, if you do not know the types of prices you are dealing with. An example is a system in which the end user (most likely the Admin user) will define books and various types of prices. This is a much more complex system because you must also allow the user to determine the rules when to use this price.
Update
Here is an example of using a dynamic summary query:
create the table #BookPrice (bookId int, Price of money, PriceType nvarchar (30)) insert values into the #BookPrice (1,10.55, "List")
insert values into the #BookPrice (1.9.50, 'Cost') insert values into the #bookPrice (2.10.22, List)
/ Determine what prices you need .... probably not by request of the table itself / declare @priceQuery varchar (max) select @priceQuery = IsNull (@priceQuery, '') + '[' + cast (PriceType as varchar (32 )) + '],' from (select a separate PriceType from #BookPrice) P
- Delete the last comma set @priceQuery = left (@priceQuery, len (@priceQuery) -1)
declare @dynquery varchar (max) set @dynquery = 'select bookId, *' + 'from #BookPrice' + 'pivot (' + 'max ([Price])' + 'for [PriceType]' + 'in (' + @priceQuery + ')) as a pivot table
exec (@dynquery)
drop table #bookPrice
UPDATE
The example above has been updated to show how if you have a book that does not have a price type that will be displayed as zero in the request
Joshberry
source share