Thursday, November 11, 2010

How can I pull records from several different tables into a single view in SQL Server 2008 R2?

Programmer Question

I'm attempting to create a sort of 'summary' view, where values should be pulled from several different tables ([Account],[Branch],[Customer],[Employee],[Transaction],[FlaggedTrans]).



Since I can't do a slew of SELECT statements in a view, I'm trying to accomplish the task using INNER JOINs, which is where my issue lies.



Here's what I've got so far:



CREATE VIEW Summary
AS
SELECT NEWID() AS Summary_Id,
COUNT(C.Customer_Id) AS CustCount,
COUNT(A.Account_Id) AS AcctCount,
COUNT(T.Transaction_Id) AS TransCount,
COUNT(B.Branch_Id) AS BranchCount,
COUNT(E.Employee_Id) AS EmplCount,
CAST(SUM(A.Balance) AS INT) AS BalTotal,
COUNT(F.FlaggedTrans_Id) AS FlagCount
FROM dbo.Account A INNER JOIN
dbo.Customer C ON A.Customer_Id = C.Customer_Id INNER JOIN
dbo.Employee E ON A.Employee_Id = E.Employee_Id INNER JOIN
dbo.[Transaction] T ON A.Account_Id = T.Account_Id INNER JOIN
dbo.FlaggedTrans F ON E.Employee_Id = F.Employee_Id CROSS JOIN
dbo.Branch B


Seems logical enough, but it's causing all of the values to come back as NULL or 0. What am I doing wrong?



Find the answer here

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails