Question : Combing MS SQL Select statement

Hello Experts,

I've got a select statement (below) that selects from three tables the two important values in it being -
LA.LiveAdverts,
EA.ExpiredAdverts

I want to add an additional column which would be TotalAdverts which would be the combination of both of these -

LA.LiveAdverts,
EA.ExpiredAdverts


A little complicated I guess given that they are from different tables, but would be grateful for your thoughts please -

thank you
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
SELECT 
E.JBEID,
E.JBEName,
E.JBEUsername,
E.JBELevel,
LA.LiveAdverts,
EA.ExpiredAdverts
From dbo.JBEmployee E
Left Join (Select LA.JBAEmployeeID, COUNT(LA.JBAID) LiveAdverts FROM dbo.JBAdvert LA GROUP BY LA.JBAEmployeeID) LA on LA.JBAEmployeeID = E.JBEID  
Left Join (Select EA.JBAEmployeeID, COUNT(EA.JBAID) ExpiredAdverts FROM dbo.JBExpiredAdvert EA GROUP BY EA.JBAEmployeeID) EA on EA.JBAEmployeeID = E.JBEID
where E.JBEClientID = 18 AND E.JBEID <> 30

Answer : Combing MS SQL Select statement

Hello garethtnash,

SELECT
E.JBEID,
E.JBEName,
E.JBEUsername,
E.JBELevel,
LA.LiveAdverts,
EA.ExpiredAdverts,
(ISNULL(LA.LiveAdverts,0) + ISNULL( EA.ExpiredAdverts, 0) ) as totalAdverts
From dbo.JBEmployee E
Left Join (Select LA.JBAEmployeeID, COUNT(LA.JBAID) LiveAdverts FROM dbo.JBAdvert LA GROUP BY LA.JBAEmployeeID) LA on LA.JBAEmployeeID = E.JBEID  
Left Join (Select EA.JBAEmployeeID, COUNT(EA.JBAID) ExpiredAdverts FROM dbo.JBExpiredAdvert EA GROUP BY EA.JBAEmployeeID) EA on EA.JBAEmployeeID = E.JBEID
where E.JBEClientID = 18 AND E.JBEID <> 30

Regards,

Aneesh
Random Solutions  
 
programming4us programming4us