|
|
|
Forum 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?
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Wednesday, May 01, 2013 4:37 PM
Points: 2,248,
Visits: 5,352
|
|
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
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Wednesday, May 01, 2013 4:37 PM
Points: 2,248,
Visits: 5,352
|
|
|
|
|
|
Forum 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
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, March 08, 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
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Today @ 1:19 PM
Points: 1,456,
Visits: 14,254
|
|
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 ! __________________________________________________________________
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, March 08, 2013 1:31 AM
Points: 4,
Visits: 11
|
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Today @ 9:25 AM
Points: 595,
Visits: 2,137
|
|
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
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, March 08, 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
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, March 08, 2013 1:31 AM
Points: 4,
Visits: 11
|
|
PLEASE GIVE ME SOLUTION FOR THE ABOVE BY ANY ONE
THANKS IN ADVANCE
|
|
|
|