Click here to Skip to main content
15,915,324 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi am having 5 tables in my project in every table contains profit varchar(50) column so i want to sum and display in text box..can any one suggest me?

thanks
Posted
Comments
Maciej Los 16-May-12 9:51am    
Do you want to sum(Field1 varchar(50))? I don't understand...
Maybe you want to group some values by Field1 and then sum(Field2 int)?

- First of all, why are you using varchar for that? Use some numeric field.
- As a table contains several records, I suppose you dont want the sum of that column for all records in all five tables.
- You can do it in a stored procedure, a single SQL query statement, or with five standalone statements
- if you have the result you can put it in the textbox.

What is your problem exactly?

Example:
SQL
select sum (amount) from
(
select amount from t1
union
select amount from t2
union
select amount from t3
union
select amount from t4
union
select amount from t5
) as sq
 
Share this answer
 
v2
Comments
ythisbug 16-May-12 8:05am    
thanks dude..actually i want to show total profit of all tables so fr that i want to create one sp and want to show in page in label or text box..so i stuck with query.
Zoltán Zörgő 16-May-12 8:46am    
Post the DDL of your tables here. Are you really using varchar for the profit?
ythisbug 17-May-12 0:56am    
ya am using varchar..
Even though it's not a new idea I really advice you to move away from varchar data types. If you store numbers in the database, use number data types. The same applies to all data types.

When using improper data types you just:
- create potential problems like conversion mismatches, CPU usage etc.
- enforce complex SQL
- create possible cultural problems
- create extra workload on the server and so on

As Zoltán Zörgő stated, you can use an inline view to get all the data and the sum it as you like. But I would also consider, do you really need 5 different tables?

Very often this kind of question arises because of incomplete normalization. Could you for example have an additional column in your table to define the "role" of the row and use a single table instead.
 
Share this answer
 
Comments
VJ Reddy 16-May-12 19:44pm    
Very good advice. 5!
Wendelius 17-May-12 4:21am    
Thanks :)
ythisbug 17-May-12 0:56am    
ya thanks mika
Wendelius 17-May-12 4:21am    
You're welcome :)
SQL
Declare @test table (COL varchar (20),   COL1 int,   COL2 int,  COL3  int,  COL4 int   )
insert into @test
Select 'AA', 150, 100 , 50  , 4 union all
Select 'HHH', 161 , 125  , 36  , 4 union all
Select 'PPPP',160 , 85 ,75  ,   4 union all
Select 'PPPP',160 , 85 ,75  ,   4 union all
Select 'JJJJJ', 120, 56 , 64 ,  2 union all
Select 'GGGG', 40, 31  ,  9 , 2

Select COL,COL1,COL2,COL3,COL4 from @test
union all
Select 'Total',sum(col1),sum(col2),sum(col3),sum(col4) from @test
 
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