SQL Query help?

Hello, I am trying to figure out the proper syntax to retrieve basic account details + payment details & a guest (GID) count() into a single gridview based on a SQL select query.  Thanks in advance.

Gridview should contain:
a.RegID,a.FirstName, a.Last Name, a.Email FROM dbo.Account a
p.PayType, p.Amount, (SELECT COUNT(p.AID) WHERE GID > 0) AS GuestCount FROM dbo.Payment p

SELECT a.AID,a.FirstName,a.LastName,a.Email,p.AID,p.PayType,p.Amount FROM [Accounts] a INNER JOIN [Payment] p ON a.AID=p.AID WHERE p.GID=0

Open in new window


Accounts table:
SELECT [AID]
      ,[Email]
      ,[Pword]
      ,[FirstName]
      ,[LastName]
      ,[BadgeName]
      ,[Gender]
      ,[Country]
      ,[Title]
      ,[Affiliation]
      ,[Address]
      ,[Phone]
      ,[CellPhone]
      ,[AltEmail]
      ,[EmgContact]
      ,[EmgPhone]
      ,[EmgEmail]
      ,[Notes]
      ,[CreatedDate]
      ,[LastLoginDate]
      ,[isROAdmin]
      ,[isAdmin]
      ,[isGlobalAdmin]
      ,[PPID]
  FROM [Accounts]

Open in new window


Payment table:
SELECT [PID]
      ,[AID]
      ,[Email]
      ,[GID]
      ,[PayType]
      ,[Amount]
      ,[isPaid]
  FROM [Payment]

Open in new window

payment
AhelblingAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian CroweDatabase AdministratorCommented:
try this...

SELECT A.AID, A.FirstName, A.LastName, A.Email,
	P.AID, P.PayType, P.Amount,
	G.GuestCount
FROM Accounts AS A
INNER JOIN Payment AS P
	ON A.AID = P.AID
LEFT APPLY
(
	SELECT COUNT(AID) AS GuestCount
	FROM Payment
	WHERE AID = A.AID
		AND GID = 0
) AS G

Open in new window

AhelblingAuthor Commented:
SS
Brian CroweDatabase AdministratorCommented:
What is your compatibility level?

SELECT compatibility_level
FROM sys.databases WHERE name = '<your database name here>';
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Brian CroweDatabase AdministratorCommented:
my bad...

change "LEFT APPLY" to "OUTER APPLY"
AhelblingAuthor Commented:
This is what I am looking for:

SS
AhelblingAuthor Commented:
Outer Apply:

SS
AhelblingAuthor Commented:
Disregard the WIRE and CASH records for AID 1123.  They are test records and will never be more than 1 set of PayType records associated with a single AID.
Brian CroweDatabase AdministratorCommented:
I think we're getting there...

SELECT A.AID, A.FirstName, A.LastName, A.Email,
	P.AID, P.PayType, P.Amount,
	G.GuestCount
FROM Accounts AS A
INNER JOIN Payment AS P
	ON A.AID = P.AID
LEFT APPLY
(
	SELECT COUNT(AID) AS GuestCount
	FROM Payment
	WHERE AID = A.AID
		AND GID > 0
) AS G
WHERE P.GID = 0

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AhelblingAuthor Commented:
Throw in the DISTINCT and used OUTER APPLY and I think we got it!  Thanks!

SELECT DISTINCT A.AID, A.FirstName, A.LastName, A.Email,
	P.AID, P.PayType, P.Amount,
	G.GuestCount
FROM [GCP21DB].[GCP21DBUser].[Accounts] AS A
INNER JOIN [GCP21DB].[GCP21DBUser].[Payment] AS P
	ON A.AID = P.AID
OUTER APPLY
(
	SELECT COUNT(AID) AS GuestCount
	FROM Payment
	WHERE AID = A.AID
		AND GID > 0
) AS G

Open in new window

AhelblingAuthor Commented:
SS
SELECT DISTINCT A.AID, A.FirstName, A.LastName, A.Email,
	P.AID, P.PayType, P.Amount,
	G.GuestCount
FROM [GCP21DB].[GCP21DBUser].[Accounts] AS A
INNER JOIN [GCP21DB].[GCP21DBUser].[Payment] AS P
	ON A.AID = P.AID
OUTER APPLY
(
	SELECT COUNT(AID) AS GuestCount
	FROM Payment
	WHERE AID = A.AID
		AND GID > 0
) AS G

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.