Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello All,

Greetings for the day!

I have a two tables and based upon that i need to generate a third table with some operations. Well, am pretty much done with all the functions but i need it to calculate two resultant columns as total.

When i execute the query the resultant data stores in table3 at the same time i need to calculate SUM of two columns of table3.

Is it possible?

Update: Was googling around and found one link, tried but couldn't get any results. Hope it might be use for you all to understand my question.

Click here[^]

What I have tried:

SQL
INSERT INTO TABLE3
SELECT Capacity.[SPOC-Partner], 
COUNT(PMKVY.CentreID) as Candidate,
COUNT(case when [Aadhar Validation-Aadhar Number Validated] = 'Yes' then 1 end) as Validated,
COUNT(case when [Result Approved by SSC] = 'Pass' then 1 end) as Results_Pass,
COUNT(case when [Result Approved by SSC] = 'Fail' then 1 end) as Results_Fail,
COUNT(case when [Result Approved by SSC] = 'No Value' then 1 end) as Results_NA,
SUM(case when [BankAcountNumber] IS NULL then 0 else 1 end) as Bank,
COUNT(case when [Certified] = 'Yes' then 1 end) as Certified,
COUNT(case when [Monetary Reward Tracker-NSDC Processed] = 'Yes' then 1 end) as NSDC_Certified,
SUM(case when [Monetary Reward Tracker-Disbursement List] = '0' then 0 else 1 end)  as DL,
Capacity.[Total Target],

//Below is what am trying to do. Need to insert "Total_declared" using resultant of "Result_Pass", "Result_Fail", "Result_NA" (Ref: Above query)
-------------------------------------------------------------------------------
sum(sum( Results_Pass)+ sum(Results_Fail)+ sum(Results_NA))) as Total_declared,
-------------------------------------------------------------------------------
//

FROM PMKVY
INNER JOIN Capacity ON PMKVY.CentreID=Capacity.[Training Centre ID]
GROUP BY Capacity.[SPOC-Partner],Capacity.[Total Target]




Thanks
Saiekrishna
Posted
Updated 25-Feb-16 4:30am
v4

1 solution

There were two mistake in your code
1. Missing calculated Keyword
2. Using "+" operator

Try the below code and check?

SQL
INSERT INTO TABLE3
SELECT Capacity.[SPOC-Partner], 
COUNT(PMKVY.CentreID) as Candidate,
COUNT(case when [Aadhar Validation-Aadhar Number Validated] = 'Yes' then 1 end) as Validated,
COUNT(case when [Result Approved by SSC] = 'Pass' then 1 end) as Results_Pass,
COUNT(case when [Result Approved by SSC] = 'Fail' then 1 end) as Results_Fail,
COUNT(case when [Result Approved by SSC] = 'No Value' then 1 end) as Results_NA,
SUM(case when [BankAcountNumber] IS NULL then 0 else 1 end) as Bank,
COUNT(case when [Certified] = 'Yes' then 1 end) as Certified,
COUNT(case when [Monetary Reward Tracker-NSDC Processed] = 'Yes' then 1 end) as NSDC_Certified,
SUM(case when [Monetary Reward Tracker-Disbursement List] = '0' then 0 else 1 end)  as DL,
Capacity.[Total Target], 
sum(calculated Results_Pass ,calculated Results_Fail, calculated Results_NA) as Total_declared,

 
FROM PMKVY
INNER JOIN Capacity ON PMKVY.CentreID=Capacity.[Training Centre ID]
GROUP BY Capacity.[SPOC-Partner],Capacity.[Total Target]
 
Share this answer
 
Comments
Member 10376341 26-Feb-16 2:25am    
Thank you for the reply.
Well, i have tried it but i get an error:

Msg 102, Level 15, State 1, Line 11
Incorrect syntax near 'Results_Pass'.
super 26-Feb-16 3:03am    
Try either one thing and see if it works.
1. Replace Count by SUM

or

2. put "then 1 else 0 end"
Member 10376341 26-Feb-16 3:54am    
Nope! same error remains.

Msg 102, Level 15, State 1, Line 11
Incorrect syntax near 'Results_Pass'.

Error is in the "line 11" that is:
sum(calculated Results_Pass ,calculated Results_Fail, calculated Results_NA) as Total_declared,
super 26-Feb-16 4:19am    
how about you try sum(sum(Results_Pass),sum(Results_Fail),sum(Results_NA)))
Member 10376341 26-Feb-16 4:42am    
This time the error is:

Msg 174, Level 15, State 1, Line 11
The sum function requires 1 argument(s).

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