Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

SQL Subtraction - Seperate rows Expand / Collapse
Author
Message
Posted Wednesday, May 26, 2010 7:00 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, May 28, 2010 6:46 AM
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?
Post #928203
Posted Wednesday, May 26, 2010 7:42 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, July 22, 2014 9:03 PM
Points: 2,262, Visits: 5,405
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

Post #928251
Posted Wednesday, May 26, 2010 7:44 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, July 22, 2014 9:03 PM
Points: 2,262, Visits: 5,405
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!
Post #928255
Posted Wednesday, May 26, 2010 8:04 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, May 28, 2010 6:46 AM
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
Post #928286
Posted Tuesday, January 31, 2012 4:40 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, March 8, 2013 1:31 AM
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
Post #1244231
Posted Tuesday, January 31, 2012 5:23 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 3:49 PM
Points: 1,909, Visits: 19,123
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
Post #1244250
Posted Tuesday, January 29, 2013 2:51 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, March 8, 2013 1:31 AM
Points: 4, Visits: 11
Can u send me the query
Post #1412844
Posted Tuesday, January 29, 2013 3:59 AM


Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Friday, August 22, 2014 9:02 AM
Points: 689, Visits: 2,779
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
Post #1412878
Posted Wednesday, March 6, 2013 12:14 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, March 8, 2013 1:31 AM
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
Post #1427196
Posted Friday, March 8, 2013 1:11 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, March 8, 2013 1:31 AM
Points: 4, Visits: 11
PLEASE GIVE ME SOLUTION FOR THE ABOVE BY ANY ONE

THANKS IN ADVANCE

Post #1428438
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse