Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

sum - SQL Split total only from options in another table

This question will be mind-bending. Let's get started.
Let's say I have a total of $50 and the $50 dollars is split into rows in another table. EXAMPLE

Table 1
ID Total
--    -----
1      $50

Table 2
-------------
ID     Price
--     ------
1       $20
1       $10
1       $10
1        $5
1        $5 

BUT Table 2 can only accept these values from table 3

      Table 3
    -------------
    ID     Price
    --     ------
    1       $10
    2       $20
    3       $30
    4        $1
    5      $2.50

Question:

How can I tell SQL to recreate table 2 to either look like this

Table 2 - DESIRED OUTPUT
-------------
ID     Price
--     ------
1       $20
1       $10
1       $10
1       $10

OR

  Table 2 - DESIRED OUTPUT
    -------------
    ID     Price
    --     ------
    1       $20
    1       $10
    1       $10
    1       $2.50
    1       $2.50
    1       $2.50
    1       $2.50

OR

 Table 2 - DESIRED OUTPUT
        -------------
        ID     Price
        --     ------
        1       $20
        1       $10
        1       $10
        1       $2.50
        1       $2.50
        1       $1
        1       $1
        1       $1
        1       $1
        1       $1

OR

Table 2 - DESIRED OUTPUT
            -------------
            ID     Price
            --     ------
            1       $20
            1       $10
            1       $10
            1       $1
            1       $1
            1       $1
            1       $1
            1       $1
            1       $1
            1       $1
            1       $1
            1       $1
            1       $1

OR any variation based on Table 3.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...