Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
3.00/5 (4 votes)
See more:
Hi
I am trying to use two count function in one select statement in stored procedure but doesn't works
SQL
SELECT DISTINCT COUNT(order),count(Cus),name
FROM MEd inner join Portfolios on MEd .MEd ID = Port.MEd Ref
INNER JOIN Pro on Port.portID = Pro.portRef
INNER JOIN P on Pro.proID = P.proRef
WHERE Port.MEd = @ID
GROUP BY name
Posted
Updated 3-Jul-12 3:48am
v2

Hi
Check this
SQL
SELECT DISTINCT COUNT([order]),count(Cus),name
FROM MEd inner join Portfolios on MEd .MEd ID = Port.MEd Ref
INNER JOIN Pro on Port.portID = Pro.portRef
INNER JOIN P on Pro.proID = P.proRef
WHERE Port.MEd = @ID
GROUP BY name

Best Regards
M.Mitwalli
 
Share this answer
 
Comments
kolisa 4-Jul-12 2:13am    
left join issues on Pro.proID = issues.projectRef
left join Risks on Pro.proID = Risks.projectRef
this two functions calculate field from this 2 tables,how can i solve that
There are several issues:
The DISTINCT key word is wrong, remove it.
If a column name contains blanks, you have to "escape" that name by using [square brackets], e.g. [MEd Ref].
Do not add blanks between table name and column name, e.g. use MEd.[MEd ID] instead of MEd .MEd ID!
ORDER is a reserved word and must be escaped.
I suggest to give the COUNT columns names, e.g.
SELECT COUNT([order]) as OrderCount, COUNT(Cus) as CusCount, name
 
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