Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
DECLARE @sql    NVARCHAR(MAX),
        @CompID NVARCHAR(MAX),
        @ID     NVARCHAR(MAX);
SET @CompID = 245;
SET @sql = 'SELECT SUM(Cw.Weightage) AS CompanyWeight FROM CompanyWeight CW WHERE CW.CompanyID IN (' + @CompID + ')';
SET @sql = @sql + ' SELECT SUM(LW.LocWeight) AS LocWeight FROM LocationWeight LW WHERE LW.CompanyID IN (' + @CompID + ')';
SET @sql = @sql + ' SELECT SUM(CPW.Weight) AS ContWeight FROM ContactPersonWeight CPW WHERE CPW.CompanyID IN (' + @CompID + ')';
EXEC ( @sql );


What I have tried:

I m trying to add the below Exec result ....I don't know how to add when the result came on select1 result =30 , select2 result =20 and select3 result =25..

I need output for 30+20+25 = 75

plz kindly help...SQL
Posted
Updated 11-Aug-16 2:25am
v2

1 solution

SQL
SELECT (SELECT SUM(Cw.Weightage) AS CompanyWeight FROM CompanyWeight CW WHERE CW.CompanyID IN (@CompID )) + (SELECT SUM(LW.LocWeight) AS LocWeight FROM LocationWeight LW WHERE LW.CompanyID IN (@CompID ))+ (SELECT SUM(CPW.Weight) AS ContWeight FROM ContactPersonWeight CPW WHERE CPW.CompanyID IN ( @CompID))


try this!

[Edit-karthik]: added in code block.
 
Share this answer
 
v3
Comments
venkatesh (chennai) 11-Aug-16 8:42am    
Thank u...its working...and i have another doubt..?

set @LocCount = 'SELECT COUNT(Cw.LocWeight) FROM LocationWeight Cw where Cw.CompanyID = 245'

how to divide the above count value to
SELECT SUM(Cw.Weightage / @LocCount) AS CompanyWeight
FROM CompanyWeight CW
WHERE CW.CompanyID IN( @CompID ))
divya behera 11-Aug-16 10:07am    
declare @LocCount int;
set @LocCount = SELECT COUNT(Cw.LocWeight) FROM LocationWeight Cw where Cw.CompanyID = 245;


SELECT SUM(Cw.Weightage / @LocCount) AS CompanyWeight
FROM CompanyWeight CW
WHERE CW.CompanyID IN( @CompID ))

Can you try the above one!

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