Using an existing SQL statement, I need to duplicate rows if value in [Action Taken] equals "See Previous Actions".
“See Previous Actions” means refer back to all actions Case Participant received for this case.
Basically, each [ViolationCategory] with "See Previous Actions" should be filled in with the same actions as other Categories for this case and participant.
I have two participants in my example as you see by the [CaseParticipantID] one that has [Action Taken] filled in and the other one does not.
I assume...data can be unioned back to itself using Case Number and Case Participant ID, but I do not know how to do that.
Data from SQL Statement (View limited to only pertinent columns for easier reading]
Case Number
CaseParticipantID
ViolationCategory
Action Taken
123
456
Conflict of Interest
Compensation
123
456
Conflict of Interest
Reprimand
123
456
Conflict of Interest
Other
123
789
Conflict of Interest
Verbal Reprimand
123
456
Favoritism
See Previous Action
Desired Outcome
Case Number
CaseParticipantID
ViolationCategory
Action Taken
123
456
Conflict of Interest
Compensation
123
456
Conflict of Interest
Reprimand
123
456
Conflict of Interest
Other
123
789
Conflict of Interest
Verbal Reprimand
123
456
Favoritism
Compensation
123
456
Favoritism
Reprimand
123
456
Favoritism
Other
My current SQL Statement, filtered down to just one case is:
SELECT
Cases.CaseNumber
,Cases.DateOpened
,Cases.DateClosed
,Participant.CaseParticipantId
,Participant.RoleInCase
,Participant.RelToOrganization
,Participant.Practice
,Participant.City
,Issues.Issue
,Issues.IssueSubCategory
,Issues.CaseIssueId
,Issues.Outcome
,ParticipantAlias.ParticipantName
,CaseAction.ActionTaken
FROM [Warehouse].[Table].[Case] as Cases
LEFT JOIN [Warehouse].[Table].[CaseParticipant] as Participant
ON Cases.CaseNumber = Participant.CaseNumber
LEFT JOIN [Warehouse].[Table].[CaseIssue] as Issues
ON Participant.CaseParticipantId = Issues.CaseParticipantId
LEFT JOIN [Warehouse].[Table].[CaseAction] as CaseAction
ON Issues.CaseIssueId = CaseAction.CaseIssueId
LEFT JOIN [Warehouse].[Table].[ParticipantAlias] as ParticipantAlias
ON Participant.ParticipantAliasId = ParticipantAlias.ParticipantAliasId
WHERE [Cases].[CaseNumber] = '123'
Based on your query, I cannot tell whether it should be a view or procedure, but I'd suggest making the latter.
Here is an example of how to achieve the expected output with a stored procedure.
The output:
Stored Procedure Sample Output
USE [Warehouse]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[SP_GetCaseResult]
-- Add the parameters for the stored procedure here
#caseNumber as int
AS
BEGIN
SET NOCOUNT ON;
DROP TABLE IF EXISTS #Temp_table
-- 1. Original query into temp table
SELECT
Cases.CaseNumber
,Cases.DateOpened
,Cases.DateClosed
,Participant.CaseParticipantId
,Participant.CaseParticipantKey
,Participant.ParticipantAliasId
,Participant.RoleInCase
,Participant.RelToOrganization
,Participant.Practice
,Participant.City
,Issues.Issue
,Issues.IssueSubCategory
,Issues.CaseIssueId
,Issues.Outcome
,ParticipantAlias.ParticipantName
,CaseAction.ActionTaken
INTO #Temp_table
FROM [Warehouse].[Table].[Case] as Cases
LEFT JOIN
[Warehouse].[Table].[CaseParticipant] as Participant
ON Cases.CaseNumber = Participant.CaseNumber
LEFT JOIN
[Warehouse].[Table].[CaseIssue] as Issues
ON Participant.CaseParticipantId = Issues.CaseParticipantId
LEFT JOIN
[Warehouse].[Table].[CaseAction] as CaseAction
ON Issues.CaseIssueId = CaseAction.CaseIssueId
LEFT JOIN
[Warehouse].[Table].[ParticipantAlias] as ParticipantAlias
ON Participant.ParticipantAliasId = ParticipantAlias.ParticipantAliasId
WHERE [Cases].[CaseNumber] = #caseNumber
-- 2. Query from temptable without "See previous actions"
SELECT * FROM #Temp_table t1 WHERE t1.ActionTaken <> 'See Previous Actions'
-- "Joining" the two results
UNION ALL
-- 3. Query from temptable with joining the two table with and without "See previous actions" and getting needed output.
SELECT
t1.CaseNumber
,t1.DateOpened
,t1.DateClosed
,t1.CaseParticipantId
,t1.CaseParticipantKey
,t1.ParticipantAliasId
,t1.RoleInCase
,t1.RelToOrganization
,t1.Practice
,t1.City
,t1.Issue
,t1.IssueSubCategory
,t1.CaseIssueId
,t1.Outcome
,t1.ParticipantName
,t2.ActionTaken
FROM #Temp_table t2
INNER JOIN #Temp_table t1 ON t2.CaseNumber = t1.CaseNumber AND t2.CaseParticipantId = t1.CaseParticipantId
WHERE t1.ActionTaken = 'See Previous Actions' AND t2.ActionTaken <> 'See Previous Actions'
END
GO
OR If you really want it without stored-procedure: Here is an example for it:
Example of simple query
DECLARE #caseNumber int = '123'
SELECT DISTINCT
t1.CaseNumber
,t1.DateOpened
,t1.DateClosed
,t1.CaseParticipantId
,t1.CaseParticipantKey
,t1.ParticipantAliasId
,t1.RoleInCase
,t1.RelToOrganization
,t1.Practice
,t1.City
,t1.Issue
,t1.IssueSubCategory
,t1.CaseIssueId
,t1.Outcome
,t1.ParticipantName
,t2.ActionTaken
FROM (
SELECT
Cases.CaseNumber
,Cases.DateOpened
,Cases.DateClosed
,Participant.CaseParticipantId
,Participant.CaseParticipantKey
,Participant.ParticipantAliasId
,Participant.RoleInCase
,Participant.RelToOrganization
,Participant.Practice
,Participant.City
,Issues.Issue
,Issues.IssueSubCategory
,Issues.CaseIssueId
,Issues.Outcome
,ParticipantAlias.ParticipantName
,CaseAction.ActionTaken
FROM [Warehouse].[Table].[Case] as Cases
LEFT JOIN
[Warehouse].[Table].[CaseParticipant] as Participant
ON Cases.CaseNumber = Participant.CaseNumber
LEFT JOIN
[Warehouse].[Table].[CaseIssue] as Issues
ON Participant.CaseParticipantId = Issues.CaseParticipantId
LEFT JOIN
[Warehouse].[Table].[CaseAction] as CaseAction
ON Issues.CaseIssueId = CaseAction.CaseIssueId
LEFT JOIN
[Warehouse].[Table].[ParticipantAlias] as ParticipantAlias
ON Participant.ParticipantAliasId = ParticipantAlias.ParticipantAliasId
WHERE [Cases].[CaseNumber] = #caseNumber --123
) t1
FULL OUTER JOIN (
SELECT
Cases.CaseNumber
,Cases.DateOpened
,Cases.DateClosed
,Participant.CaseParticipantId
,Participant.CaseParticipantKey
,Participant.ParticipantAliasId
,Participant.RoleInCase
,Participant.RelToOrganization
,Participant.Practice
,Participant.City
,Issues.Issue
,Issues.IssueSubCategory
,Issues.CaseIssueId
,Issues.Outcome
,ParticipantAlias.ParticipantName
,CaseAction.ActionTaken
FROM [Warehouse].[Table].[Case] as Cases
LEFT JOIN
[Warehouse].[Table].[CaseParticipant] as Participant
ON Cases.CaseNumber = Participant.CaseNumber
LEFT JOIN
[Warehouse].[Table].[CaseIssue] as Issues
ON Participant.CaseParticipantId = Issues.CaseParticipantId
LEFT JOIN
[Warehouse].[Table].[CaseAction] as CaseAction
ON Issues.CaseIssueId = CaseAction.CaseIssueId
LEFT JOIN
[Warehouse].[Table].[ParticipantAlias] as ParticipantAlias
ON Participant.ParticipantAliasId = ParticipantAlias.ParticipantAliasId
WHERE [Cases].[CaseNumber] = #caseNumber --123
) t2
ON t2.CaseNumber = t1.CaseNumber AND t2.CaseParticipantId = t1.CaseParticipantId
WHERE t2.ActionTaken <> 'See Previous Actions'
Here is the UNION sample also:
However considering performance, if your tables contain hundreds of thousands of records, then the stored procedure is the "fastest" as it only queries the main data once ! (afterwards the temporary tables are in use).
In the other 2 examples, the more you query the tables (which contains many data) the less the performance will be. There won't be much difference if the tables don't contain many data inside.
So it's up to you what way method you'd choose getting the expected output.
UNION example result
DECLARE #caseNumber int = '123'
SELECT
Cases.CaseNumber
,Cases.DateOpened
,Cases.DateClosed
,Participant.CaseParticipantId
,Participant.CaseParticipantKey
,Participant.ParticipantAliasId
,Participant.RoleInCase
,Participant.RelToOrganization
,Participant.Practice
,Participant.City
,Issues.Issue
,Issues.IssueSubCategory
,Issues.CaseIssueId
,Issues.Outcome
,ParticipantAlias.ParticipantName
,CaseAction.ActionTaken
FROM [Warehouse].[Table].[Case] as Cases
LEFT JOIN
[Warehouse].[Table].[CaseParticipant] as Participant
ON Cases.CaseNumber = Participant.CaseNumber
LEFT JOIN
[Warehouse].[Table].[CaseIssue] as Issues
ON Participant.CaseParticipantId = Issues.CaseParticipantId
LEFT JOIN
[Warehouse].[Table].[CaseAction] as CaseAction
ON Issues.CaseIssueId = CaseAction.CaseIssueId
LEFT JOIN
[Warehouse].[Table].[ParticipantAlias] as ParticipantAlias
ON Participant.ParticipantAliasId = ParticipantAlias.ParticipantAliasId
WHERE [Cases].[CaseNumber] = #caseNumber AND [CaseAction].ActionTaken <> 'See Previous Actions'
UNION ALL
SELECT DISTINCT
t1.CaseNumber
,t1.DateOpened
,t1.DateClosed
,t1.CaseParticipantId
,t1.CaseParticipantKey
,t1.ParticipantAliasId
,t1.RoleInCase
,t1.RelToOrganization
,t1.Practice
,t1.City
,t1.Issue
,t1.IssueSubCategory
,t1.CaseIssueId
,t1.Outcome
,t1.ParticipantName
,t2.ActionTaken
FROM (
SELECT
Cases.CaseNumber
,Cases.DateOpened
,Cases.DateClosed
,Participant.CaseParticipantId
,Participant.CaseParticipantKey
,Participant.ParticipantAliasId
,Participant.RoleInCase
,Participant.RelToOrganization
,Participant.Practice
,Participant.City
,Issues.Issue
,Issues.IssueSubCategory
,Issues.CaseIssueId
,Issues.Outcome
,ParticipantAlias.ParticipantName
,CaseAction.ActionTaken
FROM [Warehouse].[Table].[Case] as Cases
LEFT JOIN
[Warehouse].[Table].[CaseParticipant] as Participant
ON Cases.CaseNumber = Participant.CaseNumber
LEFT JOIN
[Warehouse].[Table].[CaseIssue] as Issues
ON Participant.CaseParticipantId = Issues.CaseParticipantId
LEFT JOIN
[Warehouse].[Table].[CaseAction] as CaseAction
ON Issues.CaseIssueId = CaseAction.CaseIssueId
LEFT JOIN
[Warehouse].[Table].[ParticipantAlias] as ParticipantAlias
ON Participant.ParticipantAliasId = ParticipantAlias.ParticipantAliasId
WHERE [Cases].[CaseNumber] = #caseNumber AND [CaseAction].ActionTaken = 'See Previous Actions'
) t1
INNER JOIN (
SELECT
Cases.CaseNumber
,Cases.DateOpened
,Cases.DateClosed
,Participant.CaseParticipantId
,Participant.CaseParticipantKey
,Participant.ParticipantAliasId
,Participant.RoleInCase
,Participant.RelToOrganization
,Participant.Practice
,Participant.City
,Issues.Issue
,Issues.IssueSubCategory
,Issues.CaseIssueId
,Issues.Outcome
,ParticipantAlias.ParticipantName
,CaseAction.ActionTaken
FROM [Warehouse].[Table].[Case] as Cases
LEFT JOIN
[Warehouse].[Table].[CaseParticipant] as Participant
ON Cases.CaseNumber = Participant.CaseNumber
LEFT JOIN
[Warehouse].[Table].[CaseIssue] as Issues
ON Participant.CaseParticipantId = Issues.CaseParticipantId
LEFT JOIN
[Warehouse].[Table].[CaseAction] as CaseAction
ON Issues.CaseIssueId = CaseAction.CaseIssueId
LEFT JOIN
[Warehouse].[Table].[ParticipantAlias] as ParticipantAlias
ON Participant.ParticipantAliasId = ParticipantAlias.ParticipantAliasId
WHERE [Cases].[CaseNumber] = #caseNumber AND [CaseAction].ActionTaken <> 'See Previous Actions'
) t2
ON t2.CaseNumber = t1.CaseNumber AND t2.CaseParticipantId = t1.CaseParticipantId
Related
I'm trying to write a custom report whereby the invoices will either have a PO number in the PO_Number Column or they wont.
If they don't have a PO number I'm looking to get data from 4 Columns (Segment1, Segment2, Segment3 and Segment4) where 2 ID numbers are the same. In the same statement in the event that there is a PO number I want to pull the same 4 columns but where 2 different ID numbers match.
The SQL I have so far is:
SELECT DISTINCT
AID.INVOICE_ID,
AID.AMOUNT,
AID.PERIOD_NAME,
GCC.SEGMENT1 as Organisation,
GCC.SEGMENT2,
GCC.SEGMENT3,
GCC.SEGMENT4,
INV.INVOICE_NUM,
INV.CREATION_DATE,
PO.SEGMENT1 as PO_Number,
SUP.VENDOR_NAME,
AID.LINE_TYPE_LOOKUP_CODE,
LINES.LINE_NUMBER
FROM
AP_INVOICES_All INV
INNER JOIN
AP_INVOICE_LINES_ALL LINES
ON INV.INVOICE_ID = LINES.INVOICE_ID
INNER JOIN
AP_INVOICE_DISTRIBUTIONS_ALL AID
ON INV.INVOICE_ID = AID.INVOICE_ID
INNER JOIN
GL_CODE_COMBINATIONS GCC
ON AID.DIST_CODE_COMBINATION_ID = GCC.CODE_COMBINATION_ID
INNER JOIN
POZ_SUPPLIERS_V SUP
ON INV.VENDOR_ID = SUP.VENDOR_ID
LEFT JOIN
PO_HEADERS_ALL PO
ON LINES.PO_HEADER_ID = PO.PO_HEADER_ID
WHERE
AID.LINE_TYPE_LOOKUP_CODE NOT IN
(
'REC_TAX',
'NONREC_TAX'
)
AND LINES.LINE_TYPE_LOOKUP_CODE NOT IN
(
'TAX'
)
ORDER BY
AID.INVOICE_ID,
LINES.LINE_NUMBER
If there is a PO I want the match to be done on:
PO_DISTRIBUTIONS_ALL.CODE_COMBINATION_ID = GCC.CODE_COMBINATION_ID
And if there isn't a PO:
AID.DIST_CODE_COMBINATION_ID = GCC.CODE_COMBINATION_ID
(I know I need to reference the PO_DISTRIBUTIONS_ALL table somewhere but not sure where)
Any help would be great as I'm pretty new to SQL and don't know how to express what I want to achieve in SQL code.
Let me know if I haven't been clear on any parts
You can left join PO_DISTRIBUTION_ALL and create conditional join on GL_CODE_COMBINATIONS like this:
SELECT DISTINCT
AID.INVOICE_ID,
AID.AMOUNT,
AID.PERIOD_NAME,
GCC.SEGMENT1 as Organisation,
GCC.SEGMENT2,
GCC.SEGMENT3,
GCC.SEGMENT4,
INV.INVOICE_NUM,
INV.CREATION_DATE,
PO.SEGMENT1 as PO_Number,
SUP.VENDOR_NAME,
AID.LINE_TYPE_LOOKUP_CODE,
LINES.LINE_NUMBER
FROM AP_INVOICES_All INV
INNER JOIN AP_INVOICE_LINES_ALL LINES
ON INV.INVOICE_ID = LINES.INVOICE_ID
INNER JOIN AP_INVOICE_DISTRIBUTIONS_ALL AID
ON INV.INVOICE_ID = AID.INVOICE_ID
LEFT JOIN PO_HEADERS_ALL PO
ON LINES.PO_HEADER_ID = PO.PO_HEADER_ID
LEFT JOIN PO_DISTRIBUTIONS_ALL PDA
ON PO.PO_HEADER_ID = PDA.PO_HEADER_ID
INNER JOIN GL_CODE_COMBINATIONS GCC
ON GCC.CODE_COMBINATION_ID = nvl(PDA.CODE_COMBINATION_ID, AID.DIST_CODE_COMBINATION_ID)
INNER JOIN
POZ_SUPPLIERS_V SUP
ON INV.VENDOR_ID = SUP.VENDOR_ID
WHERE AID.LINE_TYPE_LOOKUP_CODE NOT IN ( 'REC_TAX', 'NONREC_TAX')
AND LINES.LINE_TYPE_LOOKUP_CODE NOT IN ('TAX')
ORDER BY
AID.INVOICE_ID,
LINES.LINE_NUMBER
Here is my SQL
SELECT DISTINCT
AID.INVOICE_ID,
AID.AMOUNT,
AID.PERIOD_NAME,
GCC.SEGMENT1 as Organisation,
GCC.SEGMENT2,
GCC.SEGMENT3,
GCC.SEGMENT4,
INV.INVOICE_NUM,
INV.CREATION_DATE,
PO.SEGMENT1 as PO_Number,
SUP.VENDOR_NAME,
AID.LINE_TYPE_LOOKUP_CODE,
LINES.LINE_NUMBER
FROM
AP_INVOICES_All INV
INNER JOIN
AP_INVOICE_LINES_ALL LINES
ON INV.INVOICE_ID = LINES.INVOICE_ID
INNER JOIN
AP_INVOICE_DISTRIBUTIONS_ALL AID
ON INV.INVOICE_ID = AID.INVOICE_ID
INNER JOIN
GL_CODE_COMBINATIONS GCC
ON AID.DIST_CODE_COMBINATION_ID = GCC.CODE_COMBINATION_ID
INNER JOIN
POZ_SUPPLIERS_V SUP
ON INV.VENDOR_ID = SUP.VENDOR_ID
LEFT JOIN
PO_HEADERS_ALL PO
ON LINES.PO_HEADER_ID = PO.PO_HEADER_ID
LEFT JOIN
PO_DISTRIBUTIONS_ALL PDA
ON INV.INVOICE_ID = PDA.PO_DISTRIBUTIONS_ALL
WHERE
AID.LINE_TYPE_LOOKUP_CODE NOT IN
(
'REC_TAX',
'NONREC_TAX'
)
AND LINES.LINE_TYPE_LOOKUP_CODE NOT IN
(
'TAX'
)
END
ORDER BY
AID.INVOICE_ID,
LINES.LINE_NUMBER
I am looking at something similar to the below. What I want to say is that if PO.SEGMENT is null, then select the columns from a table using the join specified. Use different columns in the join depending on whether PO.SEGMENT is null or not. Is this easily achievable?
CASE
WHEN PO.SEGMENT IS NOT NULL
THEN SEGMENT1,SEGMENT2,SEGMENT3,SEGMENT4
FROM GCC.CODE_COMBINATION_ID
WHERE PDA.CODE_COMBINATION_ID = GCC.CODE_COMBINATION_ID
WHEN PO.SEGMENT IS NULL
THEN SEGMENT1,SEGMENT2,SEGMENT3,SEGMENT4
FROM GCC.CODE_COMBINATION_ID
WHERE AID.DIST_CODE_COMBINATION_ID = GCC.CODE_COMBINATION_ID
END
How about rephrasing your logic so that you just have a single ON clause:
ON
(PO.SEGMENT IS NOT NULL AND PDA.CODE_COMBINATION_ID = GCC.CODE_COMBINATION_ID) OR
(PO.SEGMENT IS NULL AND AID.DIST_CODE_COMBINATION_ID = GCC.CODE_COMBINATION_ID)
I left out the other details of the rest of your query, but I suggest this because it may be possible to achieve what you want with careful logic.
Hello i am making schema for purchasing orders these orders can be ordered by certain user and then received by another user.
so i created the below schema.
Schema
The issue is when UserID column in PurchaseOrders and Deliveries tables has different values the query returns no records.
Query
SELECT
dbo.Users.FirstName,
dbo.Users.LastName,
dbo.PurchaseOrders.PurchaseOrderDate,
dbo.Deliveries.ExpectedDeliveryDate,
dbo.Deliveries.ActualDeliveryDate
FROM dbo.PurchaseOrders
INNER JOIN dbo.Users
ON dbo.PurchaseOrders.UserID = dbo.Users.UserID
INNER JOIN dbo.Deliveries
ON dbo.PurchaseOrders.PurchaseOrderID = dbo.Deliveries.PurchaseOrderID
AND dbo.Users.UserID = dbo.PurchaseOrders.UserID
AND dbo.Users.UserID = dbo.Deliveries.UserID
You need two different joins to Users. You also need to learn to use table aliases:
SELECT pu.FirstName as purchase_FirstName, pu.LastName as purchase_LastName,
du.FirstName as delivery_FirstName, du.LastName as delivery_LastName,
po.PurchaseOrderDate,
d.ExpectedDeliveryDate, d.ActualDeliveryDate
FROM dbo.PurchaseOrders po JOIN
dbo.Deliveries d
ON po.PurchaseOrderID = d.PurchaseOrderID JOIN
dbo.Users pu
ON p.UserID = pu.UserID JOIN
dbo.Users du
ON d.UserId = du.UserId;
Your query returns no records because of this condition:
AND dbo.Users.UserID = dbo.PurchaseOrders.UserID
AND dbo.Users.UserID = dbo.Deliveries.UserID
This obviously means that dbo.PurchaseOrders.UserID = dbo.Deliveries.UserID. So, if this is not true, then no records match the condition.
When you use Inner Join, if the data doesnt match, you will not get any records. You need to use LEFT JOIN and also the second join doesnt need condition with user id. Try the below query.
SELECT
dbo.Users.FirstName,
dbo.Users.LastName,
dbo.PurchaseOrders.PurchaseOrderDate,
dbo.Deliveries.ExpectedDeliveryDate,
dbo.Deliveries.ActualDeliveryDate
FROM dbo.PurchaseOrders
INNER JOIN dbo.Users
ON dbo.PurchaseOrders.UserID = dbo.Users.UserID
LEFT JOIN dbo.Deliveries
ON dbo.PurchaseOrders.PurchaseOrderID = dbo.Deliveries.PurchaseOrderID
I am having an issue with an inner join. It executes but when I pass the value to it, it returns nothing.
CREATE PROCEDURE s_Get$Subject$For$Edit
(
#SubjectID int
)
as
select s.SubjectID, s.SubjectName, s.SubjectDescription, i.QuizID, i.GameID, i.VideoID, q.QuizID, q.QuizName, g.GameID, g.GameName,
v.VideoID, v.VideoName
from [Subjects] s
inner join [SubjectInfo] i on i.SubjectID = s.SubjectID
inner join [Quiz] q on q.QuizID = i.QuizID
inner join [Games] g on g.GameID = i.GameID
inner join [Video] v on v.VideoID = i.VideoID
where s.SubjectID = #SubjectID
What am I missing or overlooking?
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
Union is not the correct approach. Union combines or merges to result sets with the same number of columns and data types. I would do something like this instead.
SELECT
( SELECT COUNT(FilteredAppointment.createdbyname)
FROM FilteredBusinessUnit INNER JOIN
FilteredSystemUser ON FilteredBusinessUnit.businessunitid = FilteredSystemUser.businessunitid INNER JOIN
FilteredAppointment ON FilteredSystemUser.systemuserid = FilteredAppointment.createdby
WHERE (FilteredBusinessUnit.name IN (#Branch))
) AS Appointment ,
( SELECT COUNT(FilteredLead.fullname)
FROM FilteredBusinessUnit INNER JOIN
FilteredSystemUser ON FilteredBusinessUnit.businessunitid = FilteredSystemUser.businessunitid INNER JOIN
FilteredLead ON FilteredSystemUser.systemuserid = FilteredLead.createdby
WHERE (FilteredBusinessUnit.name IN (#Branch))
) AS Lead
In response to your comment to be able to compare the numbers to other branches, I would try something like this.
SELECT FilteredBusinessUnit.name ,
SUM(CASE WHEN FilteredAppointment.createdbyname IS NOT NULL THEN 1 ELSE 0 END) AS Appointments,
SUM(CASE WHEN FilteredAppointment.fullname IS NOT NULL THEN 1 ELSE 0 END) AS Leads
FROM FilteredBusinessUnit
INNER JOIN FilteredSystemUser ON FilteredBusinessUnit.businessunitid = FilteredSystemUser.businessunitid
LEFT OUTER JOIN FilteredAppointment ON FilteredSystemUser.systemuserid = FilteredAppointment.createdby
LEFT OUTER JOIN FilteredLead ON FilteredSystemUser.systemuserid = FilteredLead.createdby
WHERE (FilteredBusinessUnit.name IN (#Branch1, #Branch2))
GROUP BY FilteredBusinessUnit.name
The where clause can be left out if you want to look at all branches.
Do this with a cross join rather than union:
select 'Total', Appointment, Leads
from (SELECT COUNT(FilteredAppointment.createdbyname) AS Appointment
FROM FilteredBusinessUnit INNER JOIN
FilteredSystemUser ON FilteredBusinessUnit.businessunitid = FilteredSystemUser.businessunitid INNER JOIN
FilteredAppointment ON FilteredSystemUser.systemuserid = FilteredAppointment.createdby
) c1 cross join
(SELECT COUNT(FilteredLead.fullname) AS Leads
FROM FilteredBusinessUnit INNER JOIN
FilteredSystemUser ON FilteredBusinessUnit.businessunitid = FilteredSystemUser.businessunitid INNER JOIN
FilteredLead ON FilteredSystemUser.systemuserid = FilteredLead.createdby
WHERE (FilteredBusinessUnit.name IN (#Branch))
) t