Click here to Skip to main content
15,885,179 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can you help me to combine the following two queries using UNION
Query 1:
SELECT  
	sr.Reg_Id as [Reg Id],
	se.Enquiry_Id as [Enquiry Id],
	se.studentName as [StudentName],
	se.ResPhone as [ResPhone],
	se.MobileNo as [MobileNo],
	tblUserDetails.UserName as Counselor,
	[dbo].fnGetStringOfCourseFromRegId(sr.Reg_Id) AS Courses,
	sf.Installment1 as Installment1,
	sf.PaymentationDate1 as PaymentationDate1,
	sf.InstallmentAmt1 as InstallmentAmt1
 	FROM
         	tblStudentsRegister sr  INNER JOIN tblStudentsEnquiry se ON
         	sr.Enquiry_Id=se.Enquiry_Id inner join tblFeeDatailsCalc sf on sf.EnqueryId=sr.Enquiry_Id 
         	and sf.PaymentationDate1>=@fromdate  and sf.PaymentationDate1<= @todate
         	 inner join tblUserDetails on 
         	se.Councelor_Id = tblUserDetails.User_Id  AND se.studentName like @studentname
         	AND se.Company_Id=5 and se.feestatus=0 ORDER BY  sr.Reg_Id 
  Query2:
    SELECT  
	sr.Reg_Id as [Reg Id],
	se.Enquiry_Id as [Enquiry Id],
	se.studentName as [StudentName],
	se.ResPhone as [ResPhone],
	se.MobileNo as [MobileNo],
	tblUserDetails.UserName as Counselor,
	[dbo].fnGetStringOfCourseFromRegId(sr.Reg_Id) AS Courses,
	sf.Installment2 as Installment,
	sf.PaymentationDate2 as PaymentationDate,
	sf.InstallmentAmt2 as InstallmentAmt
 	FROM
         	tblStudentsRegister sr  INNER JOIN tblStudentsEnquiry se ON
         	sr.Enquiry_Id=se.Enquiry_Id inner join tblFeeDatailsCalc sf on sf.EnqueryId=sr.Enquiry_Id 
         	and sf.PaymentationDate2>=@fromdate  and sf.PaymentationDate2<= @todate
         	 inner join tblUserDetails on 
         	se.Councelor_Id = tblUserDetails.User_Id  AND se.studentName like @studentname
         	AND se.Company_Id=5 and se.feestatus=0 ORDER BY  sr.Reg_Id 
Posted

Just put the UNION[^] between the two SELECT[^] queries.

SQL
SELECT *
UNION
SELECT *
 
Share this answer
 
Try this
SQL
SELECT  
	sr.Reg_Id as [Reg Id],
	se.Enquiry_Id as [Enquiry Id],
	se.studentName as [StudentName],
	se.ResPhone as [ResPhone],
	se.MobileNo as [MobileNo],
	tblUserDetails.UserName as Counselor,
	[dbo].fnGetStringOfCourseFromRegId(sr.Reg_Id) AS Courses,
	sf.Installment1 as Installment1,
	sf.PaymentationDate1 as PaymentationDate1,
	sf.InstallmentAmt1 as InstallmentAmt1
 	FROM
         	tblStudentsRegister sr  INNER JOIN tblStudentsEnquiry se ON
         	sr.Enquiry_Id=se.Enquiry_Id inner join tblFeeDatailsCalc sf on sf.EnqueryId=sr.Enquiry_Id 
         	and sf.PaymentationDate1>=@fromdate  and sf.PaymentationDate1<= @todate
         	 inner join tblUserDetails on 
         	se.Councelor_Id = tblUserDetails.User_Id  AND se.studentName like @studentname
         	AND se.Company_Id=5 and se.feestatus=0 
  union
    SELECT  
	sr.Reg_Id as [Reg Id],
	se.Enquiry_Id as [Enquiry Id],
	se.studentName as [StudentName],
	se.ResPhone as [ResPhone],
	se.MobileNo as [MobileNo],
	tblUserDetails.UserName as Counselor,
	[dbo].fnGetStringOfCourseFromRegId(sr.Reg_Id) AS Courses,
	sf.Installment2 as Installment1,
	sf.PaymentationDate2 as PaymentationDate1,
	sf.InstallmentAmt2 as InstallmentAmt1
 	FROM
         	tblStudentsRegister sr  INNER JOIN tblStudentsEnquiry se ON
         	sr.Enquiry_Id=se.Enquiry_Id inner join tblFeeDatailsCalc sf on sf.EnqueryId=sr.Enquiry_Id 
         	and sf.PaymentationDate2>=@fromdate  and sf.PaymentationDate2<= @todate
         	 inner join tblUserDetails on 
         	se.Councelor_Id = tblUserDetails.User_Id  AND se.studentName like @studentname
         	AND se.Company_Id=5 and se.feestatus=0 ORDER BY  sr.Reg_Id 
 
Share this answer
 
v2
Comments
Nimisha Mary John 20-Oct-11 0:37am    
Thank you ..........
Before u do union/union all, both queries have same no of columns and also column datatypes also same.

-- UNION will retain the duplicates
-- UNION ALL will allow all the entries (allow duplicates)
-- Order by clause should use last


SELECT
sr.Reg_Id as [Reg Id],
se.Enquiry_Id as [Enquiry Id],
se.studentName as [StudentName],
se.ResPhone as [ResPhone],
se.MobileNo as [MobileNo],
tblUserDetails.UserName as Counselor,
[dbo].fnGetStringOfCourseFromRegId(sr.Reg_Id) AS Courses,
sf.Installment1 as Installment1,
sf.PaymentationDate1 as PaymentationDate1,
sf.InstallmentAmt1 as InstallmentAmt1
FROM
tblStudentsRegister sr INNER JOIN tblStudentsEnquiry se ON
sr.Enquiry_Id=se.Enquiry_Id inner join tblFeeDatailsCalc sf on sf.EnqueryId=sr.Enquiry_Id
and sf.PaymentationDate1>=@fromdate and sf.PaymentationDate1<= @todate
inner join tblUserDetails on
se.Councelor_Id = tblUserDetails.User_Id AND se.studentName like @studentname
AND se.Company_Id=5 and se.feestatus=0

UNION

SELECT
sr.Reg_Id as [Reg Id],
se.Enquiry_Id as [Enquiry Id],
se.studentName as [StudentName],
se.ResPhone as [ResPhone],
se.MobileNo as [MobileNo],
tblUserDetails.UserName as Counselor,
[dbo].fnGetStringOfCourseFromRegId(sr.Reg_Id) AS Courses,
sf.Installment2 as Installment,
sf.PaymentationDate2 as PaymentationDate,
sf.InstallmentAmt2 as InstallmentAmt
FROM
tblStudentsRegister sr INNER JOIN tblStudentsEnquiry se ON
sr.Enquiry_Id=se.Enquiry_Id inner join tblFeeDatailsCalc sf on sf.EnqueryId=sr.Enquiry_Id
and sf.PaymentationDate2>=@fromdate and sf.PaymentationDate2<= @todate
inner join tblUserDetails on
se.Councelor_Id = tblUserDetails.User_Id AND se.studentName like @studentname
AND se.Company_Id=5 and se.feestatus=0
ORDER BY 1 .
 
Share this answer
 
Comments
Nimisha Mary John 20-Oct-11 0:36am    
Thank you So much............
Open a text editor add the first query to it, then write UNION then add the second query.
SQL
query1
UNION
query2
 
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