Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6850555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:08:20+00:00 2026-05-27T01:08:20+00:00

This problem has been sending me insane, although admittedly I’ve not written SQL for

  • 0

This problem has been sending me insane, although admittedly I’ve not written SQL for a long time.

I have 2 tables:

CREATE TABLE [Entities].[Events](
[EventID] [int] IDENTITY(1,1) NOT NULL,
[EventVenueID] [int] NULL,
[EntityID] [int] NOT NULL,
[OrganisationID] [int] NULL,
[Title] [nvarchar](300) NOT NULL,
[DateStart] [datetime] NOT NULL,
[DateFinish] [datetime] NOT NULL,
[Notes] [nvarchar](max) NULL,
[MinimumProviders] [int] NOT NULL,
[MinimumAttendees] [int] NOT NULL,
[ShowCalendar] [bit] NOT NULL,
[CreationDate] [datetime] NOT NULL,
[IsEnabled] [bit] NOT NULL)

CREATE TABLE [Entities].[EventParticipants](
[ParticipantID] [int] IDENTITY(1,1) NOT NULL,
[EventID] [int] NOT NULL,
[PersonID] [int] NOT NULL,
[ParticipantType] [int] NOT NULL,
[ParticipantStatus] [int] NOT NULL,
[AttendanceStatus] [int] NULL)

And a query:

SELECT
e.EventID,
ev.VenueName,
e.EntityID,
o.Name AS 'Organisation',
e.Title,
e.DateStart,
e.DateFinish,
e.Notes,
e.MinimumProviders,
e.MinimumAttendees,
e.CreationDate,
COUNT(epp.ParticipantID) AS 'ProvidersConfirmed',
COUNT(epa.ParticipantID) AS 'AttendeesConfirmed',
e.IsEnabled
  FROM
Entities.Events e
  LEFT OUTER JOIN
Entities.EventVenues ev
  ON
e.EventVenueID = ev.EventVenueID
  LEFT OUTER JOIN
Entities.Organisations o
  ON
e.OrganisationID = o.OrganisationID
  LEFT OUTER JOIN
Entities.EventParticipants epp
  ON
e.EventID = epp.EventID AND epp.ParticipantType = 1 AND epp.ParticipantStatus = 3
  LEFT OUTER JOIN
Entities.EventParticipants epa
  ON
e.EventID = epa.EventID AND epa.ParticipantType = 2 AND epa.ParticipantStatus = 3
  GROUP BY
e.EventID,
ev.VenueName,
e.EntityID,
o.Name,
e.Title,
e.DateStart,
e.DateFinish,
e.Notes,
e.MinimumProviders,
e.MinimumAttendees,
e.CreationDate,
e.IsEnabled

The count is producing strange results, for example in the EventParticipants table I have the following data:

Participant Type = 1 (3 records)
Participant Type = 2 (1 record)
Participant Status = 3 (4 records)

The queries count should return:

ProvidersConfirmed = 3
AttendeesConfirmed = 1

However, it’s returning

ProvidersConfirmed = 3
AttendeesConfirmed = 3

Can anyone help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:08:21+00:00Added an answer on May 27, 2026 at 1:08 am

    Basically all your joins are throwing off this query. When you join from Event to EventParticipants There is one event record and three EventParticipant records, this equates to three duplicate eventIDs in your dataset. Then you join to EventParticipants again. Because you’re joining 3 rows with one row, you get the result repeated 3 times.

    There are ways to trim down the results, but it would probably be better to only JOIN once and do some selective math in your aggregate, like this:

    SELECT
    e.EventID,
    ev.VenueName,
    e.EntityID,
    o.Name AS 'Organisation',
    e.Title,
    e.DateStart,
    e.DateFinish,
    e.Notes,
    e.MinimumProviders,
    e.MinimumAttendees,
    e.CreationDate,
    COUNT(CASE WHEN ep.ParticipantType = 1 THEN 1 ELSE NULL END) AS 'ProvidersConfirmed',
    COUNT(CASE WHEN ep.ParticipantType = 2 THEN 1 ELSE NULL END) AS 'AttendeesConfirmed',
    e.IsEnabled
      FROM
    Entities.Events e
      LEFT OUTER JOIN
    Entities.EventVenues ev
      ON
    e.EventVenueID = ev.EventVenueID
      LEFT OUTER JOIN
    Entities.Organisations o
      ON
    e.OrganisationID = o.OrganisationID
      LEFT OUTER JOIN
    Entities.EventParticipants epp
      ON
    e.EventID = epp.EventID AND epp.ParticipantStatus = 3
      GROUP BY
    e.EventID,
    ev.VenueName,
    e.EntityID,
    o.Name,
    e.Title,
    e.DateStart,
    e.DateFinish,
    e.Notes,
    e.MinimumProviders,
    e.MinimumAttendees,
    e.CreationDate,
    e.IsEnabled
    

    And SQL’s COUNT will throw away all the null values so it will only count unique values.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This problem has been kicking my butt for a few days now. I have
This problem has been solved thanks to your suggestions. See the bottom for details.
This problem has been solved! Thanks a lot to Brad, Denis and junkie! You're
EDIT: This problem has been solved. See below. Hey all. I'm building an iPhone
I'm sure this problem has been solved before and I'm curious how its done.
UPDATE It looks like this problem has been quietly fixed in iOS 4.3. Up
Edit : The bug that caused this problem has been fixed. The @version tag
This Jquery problem has been bugging me for a while now. I developed a
There was this problem that has been asked about implementing a load byte into
A coworker has been struggling with this problem. The desired result is an installable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.