How to divide the value in row 2 by the value in row 2 in the same column

  • Hi all,

    Can you please help what is wrong with my query, I try to take the value from row 1 divided by the value from row 2 but it is not working, here is the following:

    Here is my table called Trans

    ID Period Sales Profit

    1 Current 20 5

    2 Previous 40 20

    I want to take 20 divided by 40(20/40), 5 divided by 20(5/20)

    Here is the result I want:

    ID Period Sales Sales_Per Profit Profit_Per

    1 Current 20 50% 5 25%

    2 Previous 40 50% 20 25%

    Here is my query:

    Select

    (t1.Sales/NULLIF(t2.Sales,0) * 100) as Sales_Per

    From Trans t1

    INNER JOIN Trans t2

    on t1.Id = t2.ID - 1

    Please give me some ideas. Thanks so much for all your help!

  • trishdiep (3/4/2015)


    Hi all,

    Can you please help what is wrong with my query, I try to take the value from row 1 divided by the value from row 2 but it is not working, here is the following:

    Here is my table called Trans

    ID Period Sales Profit

    1 Current 20 5

    2 Previous 40 20

    I want to take 20 divided by 40(20/40), 5 divided by 20(5/20)

    Here is the result I want:

    ID Period Sales Sales_Per Profit Profit_Per

    1 Current 20 50% 5 25%

    2 Previous 40 50% 20 25%

    Here is my query:

    Select

    (t1.Sales/NULLIF(t2.Sales,0) * 100) as Sales_Per

    From Trans t1

    INNER JOIN Trans t2

    on t1.Id = t2.ID - 1

    Please give me some ideas. Thanks so much for all your help!

    I don't think your join condition is quite right here. For row 2 you would get whatever is in Transaction 3. Also, this code assumes you have no gaps at all in your rows which is highly unlikely. You probably need to add another predicate in your join condition.

    In order to help we will need a few things:

    1. Sample DDL in the form of CREATE TABLE statements

    2. Sample data in the form of INSERT INTO statements

    3. Expected results based on the sample data

    Please take a few minutes and read the first article in my signature for best practices when posting questions.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Thank you for your reply Sean.

    I tried my code again, the query is right, however I need to convert the columns to float and I got the right % that I wanted.

    Thanks!

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply