Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 592905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:47:20+00:00 2026-05-13T15:47:20+00:00

I need to model an idea which can be broken down and thought of

  • 0

I need to model an idea which can be broken down and thought of as follows:

  1. BookDetails
  2. BookPrices

The problem here is that you can have many prices for books and these prices are likely to change. Here’s an example

BookDetails:
-----------------
ID  Name
1   Harry Potter…

This is easy enough.

Where it is more interesting is that for this one book I might have ten different prices on that day, e.g.:

BookPrices:
------------------------------------
Book_Details_Id  Kind          Price
1                SpecialOffer     10
1                BulkPurchase     20
1                Normal           30

I need to provide a list of books and all their prices in columns – something like:

BookName         SpecialOffer   BulkPurchase      Normal    
Harry Potter…              10             20          30

My question is: Should the book price table actually have all the different price types as columns? To me this is ugly and a better idea is to have each price as a row

If I use that approach I cannot think of a SQL query to generate me the result set. I have been thinking about this all morning.

EDIT: I have no leeway on calculating prices – they have to be stored down.

EDIT: This is basically the 1-n appraoch I can think of (Thanks to comment below) – Its what I actually had in mind

SELECT book.bookid, bp1.price, bp2.price FROM book JOIN bookprice bp1 JOIN bookprice bp2 ON bp1.bookid = book.bookid AND bp1.pricetype=1 AND bp2.bookid = book.bookid AND bp2.pricetype=2…

The problem is for ten prices you will be joining ten times which stinks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:47:20+00:00Added an answer on May 13, 2026 at 3:47 pm

    This answer is t-SQL specific, and could use a little refinement, but it works on SQL 2005.

    Uncomment the commented lines and it’ll update like MAGIC! (okay, not magic, but niftily hacky)

    DROP TABLE Books
    DROP TABLE Prices
    DROP TABLE bookpricing
    
    CREATE TABLE Books ( id INT, title VARCHAR(20) )
    CREATE TABLE Prices ( id INT, [desc] VARCHAR(20), pricingchange VARCHAR(20))
    CREATE TABLE bookpricing ( id INT, bookid INT, priceid INT, bookprice MONEY )
    
    INSERT INTO Books VALUES (1, 'Hi Mom')
    --INSERT INTO Books Values (2, 'This is another book') 
    INSERT INTO Prices VALUES (1, 'Standard', '1')
    INSERT INTO Prices  VALUES (2, 'Discount', '.5')
    INSERT INTO Prices VALUES(3, 'HyperMarkup', '1.5')
    INSERT INTO prices VALUES(4, 'Remaindered', '.1')
    
    INSERT INTO BookPricing VALUES (1,1,1,20.00)
    INSERT INTO BookPricing VALUES (2,1,2,10.00)
    INSERT INTO BookPricing VALUES (3,1,3,30.00)
    --INSERT INTO BookPricing VALUES (4,2,1,30.00)
    --INSERT INTO BookPricing VALUES (5,2,2,15.00)
    --INSERT INTO BookPricing VALUES (6,2,4,3.00)
    
    SELECT * FROM bookpricing 
    

    /** this bit stolen from http://www.tsqltutorials.com/pivot.php **/

    DECLARE @columns VARCHAR(max)
    
    SELECT @columns = COALESCE(@columns + ',[' + cast(id as varchar) + ']',
    '[' + cast(id as varchar)+ ']')
    FROM prices
    
    
    DECLARE @query VARCHAR(max)
    
    SET @query = '
    SELECT * FROM (SELECT BookID, PriceID, BookPrice FROM BookPricing) AS BookTable PIVOT (SUM(bookprice) FOR priceid 
    IN (' + @columns + ')
    )
    AS p'
    
    EXECUTE(@query)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.