Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL Subtraction - Seperate rows


SQL Subtraction - Seperate rows

Author
Message
kevin_batchelor
kevin_batchelor
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 5
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:

Fred Ord4 Month2 Stuff -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?
ColdCoffee
ColdCoffee
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2319 Visits: 5545
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


ColdCoffee
ColdCoffee
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2319 Visits: 5545
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


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

C'est Pras!
kevin_batchelor
kevin_batchelor
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 5
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
kravitej9
kravitej9
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 11
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
J Livingston SQL
J Livingston SQL
Hall of Fame
Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)

Group: General Forum Members
Points: 3462 Visits: 33059
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

kravitej9
kravitej9
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 11
Can u send me the query
Andy Hyslop
Andy Hyslop
Say Hey Kid
Say Hey Kid (706 reputation)Say Hey Kid (706 reputation)Say Hey Kid (706 reputation)Say Hey Kid (706 reputation)Say Hey Kid (706 reputation)Say Hey Kid (706 reputation)Say Hey Kid (706 reputation)Say Hey Kid (706 reputation)

Group: General Forum Members
Points: 706 Visits: 3023
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
kravitej9
kravitej9
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 11
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
kravitej9
kravitej9
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 11
PLEASE GIVE ME SOLUTION FOR THE ABOVE BY ANY ONE

THANKS IN ADVANCE
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search