Click here to Skip to main content
15,905,914 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi!

I want to compute in where the logic is somehow like this:

Table A has a column named "TotalProductSum"
ID||TotalProductSum||
11||1000.00        ||

Table B has a column named "ProductSum"
ID||Products||Value||
11||01      ||20   ||
11||02      ||35   ||
11||03      ||40   ||
11||04      ||10   ||

*ProductSum has .00 in each cell and this should be computed as TotalProductSum - ProductSum. The TotalProductSum changes once it is subtracted to product sum. For example TotalProductSum = 100 and ProductSum = 10 then the NewTotalProductSum = 90. The next value of ProductSum = 30 then the computation will change to NewTotalProductSum - ProductSum then the NewTotalProductSum of the next cell is 60.

OUTPUT TABLE:
TotalProductSum = 1000.00
ID||Products||Value||NewTotalProductSum [Value - [Previous]TotalProductSum]
11||01      ||20   ||800.00
11||02      ||35   ||765.00
11||03      ||40   ||725.00
11||04      ||10   ||715.00

TotalProductSum is the difference of Value and the Previous TotalProductSum.

If you do not get the question, feel free to comment below. Thank you.
Posted
Updated 1-Jul-14 14:35pm
v3
Comments
Magic Wonder 1-Jul-14 5:39am    
did not get the question? Kindly clarify what is your exact requirement and what you have done for that?
mitchiee1226 1-Jul-14 20:30pm    
Updated the question.
[no name] 1-Jul-14 6:48am    
What question is it that you are referring to?
mitchiee1226 1-Jul-14 20:31pm    
The output. I want to output this type of table. Is it possible?
[no name] 1-Jul-14 20:57pm    
Well okay then if that is really your question.... yes it is possible.

1 solution

I have made sample for you check this hope this will help you.

SQL
-- Create Table 
CREATE TABLE [dbo].[TableA](
	[ID] [INT] NOT NULL,
	[TotalProductSum] [INT] NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[TableB](
	[ID] [INT] NOT NULL,
	[Products] [varchar](30) NOT NULL,
	[Value] [INT] NOT NULL
) ON [PRIMARY]


-- Insert Sample Data
INSERT INTO [dbo].[TableA]
           ([ID],[TotalProductSum])
     VALUES (11,1000)
           
           INSERT INTO [TestTB].[dbo].[TableB] ([ID],[Products],[Value])
     VALUES (11 ,'10' ,80)

    INSERT INTO [TestTB].[dbo].[TableB] ([ID],[Products],[Value])
     VALUES (11 ,'20' ,30)
      INSERT INTO [TestTB].[dbo].[TableB] ([ID],[Products],[Value])
     VALUES (11 ,'30' ,45)
      INSERT INTO [TestTB].[dbo].[TableB] ([ID],[Products],[Value])
     VALUES (11 ,'40' ,56)

-- Select query 

select * from TableA
select * from TableB

-- Here isw the select query with your result : 


SELECT A.ID,A.Products,A.Value,B.TotalproductSum- SUM(a.value) OVER (ORDER BY A.Products) NewTotalProductSum 
from  
TableB as A INNER JOIN TableA AS B
ON A.ID=B.ID
ORDER BY a.id;


The Result will be like this
Table A
11 1000
TableB
11 10 80
11 20 30
11 30 45
11 40 56

The result Query will be like this (
VB
NewTotalProductSum [Value - [Previous]TotalProductSum]
)
11 10 80 920
11 20 30 890
11 30 45 845
11 40 56 789
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900