简体   繁体   中英

MySQL Query - data not showing as expected, problem in code

I am trying to query a database for the number of individuals who did not arrive for their booking on a given date. However, the results given are not as expected.

From manual checking, the results for 3rd May 2021 should be displayed as 3. I have a feeling that the customer id's are being added together with the result being displayed rather than just the count of individual customer id's.

select
    count(c.CUSTOMER_ID) AS 'No Shows',
    date(checkins.POSTDATE) as date
from
  customers c, checkins
where
    checkins.postdate >= date_sub(curdate(), interval 7 day)
    and
  (
    c.archived = 0
    and (
      (
        (
          (
            (
              (
                c.GUID in (
                  select
                    sb1.customer_guid
                  from
                    schedule_bookings sb1
                    join schedule_events se1 on sb1.course_guid = se1.course_guid
                    and sb1.OFFERING_ID in (
                      '2915911', '3022748', '3020740', '2915949',
                      '2914398', '2916147', '3022701',
                      '3020699', '2916185', '2915168',
                      '2916711', '3022403', '3020455',
                      '2916785', '2916478', '2915508',
                      '3022538', '3020582', '2915994',
                      '2914547', '2916069', '3022648',
                      '3020658', '2916107', '2915290',
                      '2928786', '2914729', '3022854',
                      '3020812', '2914694', '2914659',
                      '3041801', '2920756', '2920834',
                      '2920795', '2916223', '3022788',
                      '3020783', '2916239', '2915013'
                    )
                    and sb1.CANCELLED in ('0')
                )
              )
              or (
                c.GUID in (
                  select
                    sp.customer_guid
                  from
                    schedule_participants sp
                    join schedule_bookings sb2 on sp.BOOKING_ID = sb2.BOOKING_ID
                    join schedule_events se2 on sb2.course_guid = se2.course_guid
                    and sb2.OFFERING_ID in (
                      '2915911', '3022748', '3020740', '2915949',
                      '2914398', '2916147', '3022701',
                      '3020699', '2916185', '2915168',
                      '2916711', '3022403', '3020455',
                      '2916785', '2916478', '2915508',
                      '3022538', '3020582', '2915994',
                      '2914547', '2916069', '3022648',
                      '3020658', '2916107', '2915290',
                      '2928786', '2914729', '3022854',
                      '3020812', '2914694', '2914659',
                      '3041801', '2920756', '2920834',
                      '2920795', '2916223', '3022788',
                      '3020783', '2916239', '2915013'
                    )
                    and sb2.CANCELLED in ('0')
                )
              )
            )
          )
        )
        and (
          (
            (
              not (
                (
                  (
                    select
                        count(CHECKIN_ID)
                    from
                      checkins
                    where
                      checkins.CUSTOMER_ID = c.CUSTOMER_ID
                  ) between 1
                  and 9999
                )
              )
            )
          )
        )
      )
    )
    and not c.customer_id in (1008, 283429, 2507795)
  )
group by date(checkins.POSTDATE)

Here are the results:

+----------+------------+
| No Shows | date       |
+----------+------------+
|    30627 | 2021-04-27 |
|    37638 | 2021-04-28 |
|    34071 | 2021-04-29 |
|    33579 | 2021-04-30 |
|    29274 | 2021-05-01 |
|    30135 | 2021-05-02 |
|    48339 | 2021-05-03 |
|     8979 | 2021-05-04 |
+----------+------------+
8 rows in set (8.71 sec)

As you can see, the count is nowhere near as intended.

The query parameters are:

Customer is a participant/bookee on the listed specific offerings (offering_id) Customer's 'Check-in' count was not between 1 and 9999. Display these results by count per date.

Can anyone see why this query would be not displaying the results as intended?

Kind Regards Tom

Lets try to reverse this out some. You are dealing with a very finite set of Offering IDs. How about something like starting with the finite list of offerings you are concerned with and join on from that. Additionally, there does not appear to be any need for the join to the schedule events table. If something is booked, its booked. You are never getting any additional context from the event itself.

So, lets start with a very simplified union. You are looking at the bookings table for the possible customer IDs. Then from the actual participants for those same bookings. My GUESS is not every person doing the actual booking may be a participant, likewise, all participants may not be the booking party.

None of this has to do with the actual final customer, archive status or even the events for the booking. We are just getting people - period. Once you have the people and dates, then get the counts.

select
        date(CI.POSTDATE) as date,
        count( JustCustomers.customer_guid ) AS 'No Shows'
    from
    (
        select
                sb1.customer_guid
            from
                schedule_bookings sb1
            where
                    sb1.CANCELLED = 0
               -- if "ID" are numeric, dont use quotes to imply character
                and sb1.OFFERING_ID in 
                (   2915911, 3022748, 3020740, 2915949,
                    2914398, 2916147, 3022701, 3020699, 
                    2916185, 2915168, 2916711, 3022403, 
                    3020455, 2916785, 2916478, 2915508,
                    3022538, 3020582, 2915994, 2914547, 
                    2916069, 3022648, 3020658, 2916107, 
                    2915290, 2928786, 2914729, 3022854,
                    3020812, 2914694, 2914659, 3041801, 
                    2920756, 2920834, 2920795, 2916223, 
                    3022788, 3020783, 2916239, 2915013 
                )
        UNION
        select
                sp.customer_guid
            from
                schedule_bookings sb2
                    JOIN schedule_participants sp
                        on sb2.BOOKING_ID = sp.BOOKING_ID
            where
                    sb2.CANCELLED = 0
                and sb2.OFFERING_ID in 
                (   2915911, 3022748, 3020740, 2915949,
                    2914398, 2916147, 3022701, 3020699, 
                    2916185, 2915168, 2916711, 3022403, 
                    3020455, 2916785, 2916478, 2915508,
                    3022538, 3020582, 2915994, 2914547, 
                    2916069, 3022648, 3020658, 2916107, 
                    2915290, 2928786, 2914729, 3022854,
                    3020812, 2914694, 2914659, 3041801, 
                    2920756, 2920834, 2920795, 2916223, 
                    3022788, 3020783, 2916239, 2915013 
                )
        ) JustCustomers
             JOIN customers c
                on JustCustomers.customer_guid = c.customer_id
                AND c.archived = 0
                AND NOT c.customer_id IN (1008, 283429, 2507795)

                JOIN checkins CI
                    on c.CUSTOMER_ID = CI.CUSTOMER_ID
                    AND CI.postdate >= date_sub(curdate(), interval 7 day)
   group by 
      date(ci.POSTDATE)

The strange thing I notice though is that you are looking for "No shows", but explicitly looking for those people who DID check in. Now, if you are looking for all people who WERE SUPPOSED to be at a given event, then you are probably looking for where the customer DID NOT check in. If that is the intended case, there would be no check-in date to be associated. If that is the case, I would expect a date in some table such as the EVENT Date... such as going on a cruise, the event is when the cruise is, regardless of who makes it to the ship.

If I am way off, I would suggest you edit your existing post, provide additional detail / clarification.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM