SQL Subtraction - Seperate rows

  • Hi

    I currently use the following statement:

    SELECT * FROM Month1_Month2_Diff_Detail

    ORDER BY Name, Order, Month

    and receive the following back:

    Name Order Month Detail Value Margin

    Fred Ord1 Month2 Ace 1300 400

    Fred Ord2 Month2 Long 0 -15010

    Fred Ord3 Month2 Bing 2270 309.09

    Fred Ord4 Month1 Stuff 6471.6 5383.81

    Fred Ord4 Month2 Stuff 3275 -462.79

    Fred Ord5 Month2 Roof 4940 1140.72

    Fred Ord6 Month2 Wall 12545 2110.37

    As you can see, there is only one Order row for each Order except

    Ord4, where there are two. How do I change the ouput so that

    the data for Ord4 looks like:

    FredOrd4Month2Stuff-3196.6-5846.6

    where the data for Ord4 and Month1 is subtracted from the

    data for Ord 4 and Month2? I have multiple Salesman also, so

    it needs to work for that too.

    I don't need to see the column month either, I had to show it

    so that the data could be seen.

    Any ideas?

  • Hi there, here is a piece of code that wll do what you wanted:

    As you are first time poster, i took the onus on my side to create DDLs for your request; going further, please provide full ready-to-use DDLs so that many volunteers here will give you tested code back 🙂

    Now , sample data and table :

    DECLARE @table TABLE

    (

    [Name] varchar(15),

    [Order] varchar(15),

    [Month] varchar(15),

    [Detail] varchar(15),

    [Value] numeric(10,2),

    [Margin] numeric(10,2)

    )

    insert into @table([Name], [Order] ,[Month] ,[Detail] ,[Value] ,[Margin])

    select 'Fred' ,'Ord1', 'Month2', 'Ace', 1300, 400

    union all select 'Fred' ,'Ord2', 'Month2' ,'Long', 0 ,-15010

    union all select 'Fred' ,'Ord3', 'Month2' ,'Bing', 2270 ,309.09

    union all select 'Fred' ,'Ord4', 'Month1' ,'Stuff', 6471.6, 5383.81

    union all select 'Fred' ,'Ord4', 'Month2' ,'Stuff', 3275, -462.79

    union all select 'Fred' ,'Ord5', 'Month2' ,'Roof' ,4940 ,1140.72

    union all select 'Fred' ,'Ord6', 'Month2' ,'Wall' ,12545, 2110.37

    Now the code that will provide you the output:

    ;with cte as

    (

    select [Name], [Order] ,[Month] ,[Detail] ,[Value] ,[Margin] ,

    row_number() over(partition by [Order] order by [Order]) rn from @table

    ),

    only_one as

    (

    select [order] from cte group by [order] having count(*) = 1

    ),

    multiple as

    (

    select * from cte where [order] in (select distinct [Order] from cte where rn > 1)

    )

    select [Name], [Order] ,[Month] ,[Detail] ,[Value] ,[Margin] from cte

    where [order] in (select [order] from only_one)

    union all

    select t1.[Name], t1.[Order] ,t1.[Month] ,t1.[Detail] ,t1.[Value]-t2.[Value] [Value]

    , t1.[Margin] -t2.[Margin] [Margin]

    from multiple t1

    cross join

    multiple t2

    where t1.rn = 2 and t2.rn = 1

  • I would also recommend you to go through the following article on how to post readily consumable data

    CLICK HERE FOR FORUM POSTING ETIQUETTES - JEFF MODEN[/url]

    Hope the code above this post helped you! Tell us if it did the trick!

    C'est Pras!

  • Apologies for not supplying the data set up code, I will remember to do that in

    the future!

    I will try the code given above, and let you know.

    Thanks for your help

  • hi,

    i need a query for

    i have data as

    1,3

    3,4,5

    4,2

    3,4

    2

    on writing query i need to get the ouput as

    1

    2

    3

    4

    5

    with out duplicate

    please send me quickly

  • kravitej9 (1/31/2012)


    hi,

    i need a query for

    i have data as

    1,3

    3,4,5

    4,2

    3,4

    2

    on writing query i need to get the ouput as

    1

    2

    3

    4

    5

    with out duplicate

    please send me quickly

    please create a new thread...this is an old post and unrelated to your question.

    ....look up UNION in BOL...this may help you

    ________________________________________________________________
    you can lead a user to data....but you cannot make them think
    and remember....every day is a school day

  • Can u send me the query

  • kravitej9 (1/29/2013)


    Can u send me the query

    This may help..

    DECLARE @temp AS TABLE

    (v1 INT, v2 INT, v3 INT)

    INSERT INTO @temp

    SELECT 1,3, NULL UNION ALL

    SELECT 3,4,5 UNION ALL

    SELECT 4,2, NULL UNION ALL

    SELECT 3,4, NULL UNION ALL

    SELECT 2, NULL, NULL

    SELECT DISTINCT

    cp.*

    FROM

    @temp

    CROSS APPLY (

    SELECT Newcol

    FROM (VALUES (v1), (v2), (v3)) X (Newcol)

    ) cp

    Andy

    ==========================================================================================================================
    A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila. Mitch Ratcliffe

  • Hi thank for ur reply.

    I have another question

    I need an auto decrement in oracle 11g

    For example i am having a table as contacts

    and column name ad ID

    when i delete an row. the ID should derease by 1 automatically

    can u please explain with an example

  • PLEASE GIVE ME SOLUTION FOR THE ABOVE BY ANY ONE

    THANKS IN ADVANCE

Viewing 10 posts - 1 through 9 (of 9 total)

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