Wednesday, 11 September 2013

How to join multiple select statements in SQL and display in different colums

How to join multiple select statements in SQL and display in different colums

I wrote a query that joins two tables that shows the total no. of
appointments and leads using the union keyword and I would like the result
of this query to be displayed in two different columns, I am having a
tough description
SELECT COUNT(FilteredAppointment.createdbyname) AS Appointment
FROM FilteredBusinessUnit
INNER JOIN FilteredSystemUser
ON FilteredBusinessUnit.businessunitid =
FilteredSystemUser.businessunitid
INNER JOIN FilteredAppointment
ON FilteredSystemUser.systemuserid = FilteredAppointment.createdby
UNION
SELECT COUNT(FilteredLead.fullname) AS Lead
FROM FilteredBusinessUnit
INNER JOIN FilteredSystemUser
ON FilteredBusinessUnit.businessunitid =
FilteredSystemUser.businessunitid
INNER JOIN FilteredLead
ON FilteredSystemUser.systemuserid = FilteredLead.createdby
WHERE (FilteredBusinessUnit.name IN (@Branch))
My desired result is:
CRITERIA | Appointment | Leads
Total | 200 | 123

No comments:

Post a Comment