Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
my stored procedure is this
SQL
create procedure EnquiryAdmission as
create table #Calendar
(Months date)
insert INTO #Calendar VALUES('1/1/2014'),('2/1/2014'),('3/1/2014'),('4/1/2014'),('5/1/2014'),('6/1/2014'),('7/1/2014'),('8/1/2014'),('9/1/2014'),('10/1/2014'),('11/1/2014'),('12/1/2014')

BEGIN 
	SELECT cal.Months,DISTINCT COUNT(enqno) AS Enquiries,DISTINCT count(learnerid)AS Admissions
FROM #calendar cal
LEFT JOIN enquiry e 
ON Month(cal.Months) = Month([enquiry date])
LEFT JOIN  admission
ON  Month(cal.Months)=Month(Date)
WHERE (YEAR([enquiry date])=YEAR(GetDate())) OR [enquiry date] IS NULL
AND (YEAR(date)=YEAR(GetDate())) OR date IS NULL
GROUP BY cal.Months
END

but it gives duplicate records. It should give exact count. What can I do to solve this problem
Posted
Updated 9-Oct-14 20:02pm
v2

better you can use INNER JOIN.
before your Write query know about What is LEFT,RIGHT,INNER JOINS

Read this Article


http://www.dotnet-tricks.com/Tutorial/sqlserver/W1aI140312-Different-Types-of-SQL-Joins.html[^]
 
Share this answer
 
SQL
CREATE procedure EnquiryAdmission as
create table #Calendar
(Months date)
insert INTO #Calendar VALUES('1/1/2014'),('2/1/2014'),('3/1/2014'),('4/1/2014'),('5/1/2014'),('6/1/2014'),('7/1/2014'),('8/1/2014'),('9/1/2014'),('10/1/2014'),('11/1/2014'),('12/1/2014')

BEGIN
 SELECT datename(mm,cal.months), COUNT(DISTINCT enqno) AS Enquiries,count(DISTINCT learnerid)AS Admissions
FROM #calendar cal
LEFT JOIN enquiry e
ON Month(cal.Months) = Month([enquiry date]) AND ((YEAR([enquiry date])=YEAR(GetDate())) OR [enquiry date] IS NULL)
LEFT JOIN  admission
ON  Month(cal.Months)=Month(Date) AND ((YEAR(date)=YEAR(GetDate())) OR date IS NULL)

GROUP BY cal.Months
END
 
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