How to calculate total tickets available and total tickets sold using MySQL?

forseth31

I need to produce the below output using MySQL, which can calculate the total tickets available and the total tickets sold for each event;

event_id  event_name     start_date    tickets_available   tickets_sold
-------------------------------------------------------------------------
1         Test Event 1   2017-04-01    200                 20
2         Test Event 2   2017-04-25    100                 30

My tables are as follows;

Table: events

event_id  user_id    event_name     start_date    event_active   
-------------------------------------------------------------------------
1         47         Test Event 1   2017-04-01    0                
2         47         Test Event 2   2017-04-25    0 

Table: ticket_types

ticket_type_id  event_id  ticket_type quantity_released quantity_remaining
---------------------------------------------------------------------------
1               1         General     100               90
2               1         VIP         100               90
3               2         General     50                30
4               2         VIP         50                40

Here's my SQL query so far;

SELECT events.event_id, events.event_name, events.start_date, events.event_active,
(
    SELECT SUM(ticket_types.quantity_released) 
    FROM ticket_types
    WHERE ticket_types.event_id = events.event_id
) AS tickets_available,
(
    SELECT (ticket_types.quantity_released - ticket_types.quantity_remaining)
    FROM ticket_types
    WHERE ticket_types.event_id = events.event_id
) AS tickets_sold
FROM events
INNER JOIN ticket_types
ON events.event_id=ticket_types.event_id
WHERE user_id = 47;

However, this is not working as the logic for the columns tickets_available and tickets_sold is incorrect.

Can someone help me out with this query please?

Pons
SELECT  EVENTS.EVENT_ID
    , EVENTS.EVENT_NAME
    , EVENTS.START_DATE
    , SUM(TICKET_TYPES.QUANTITY_RELEASED) AS TICKETS_AVAILABLE
    , SUM(TICKET_TYPES.QUANTITY_RELEASED) - SUM(TICKET_TYPES.QUANTITY_REMAINING) AS TICKETS_SOLD
FROM EVENTS
    INNER JOIN TICKET_TYPES
        ON EVENTS.EVENT_ID = TICKET_TYPES.EVENT_ID
GROUP BY EVENTS.EVENT_ID
    , EVENTS.EVENT_NAME
    , EVENTS.START_DATE;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Getting total count of tickets sold for a table. Rails

From Dev

How to calculate % of total in MySQL

From Dev

How to calculate % of total in MySQL

From Dev

How an user choose a number of tickets and then ask for the prize of each ticket an make a total?

From Dev

Write a program that will ask the user for the number of tickets and output total

From Dev

how to get total and total of subset using mysql?

From Dev

How to get total no of sold products in magento

From Dev

How to get total no of sold products in magento

From Dev

how to minus sold stock from total stock?

From Dev

How to calculate the running total using aggregate

From Dev

How to calculate total disk space using df?

From Dev

Javascript: how to calculate the total

From Dev

Calculate percentage to total using rowPercents

From Dev

how to return total sold p / year for several years in columnar format?

From Dev

How to find total available and used memory using df command

From Dev

How to calculate total time between datetime stamps in MySQL

From Dev

how to calculate total number of saturday and sunday between two dates in mysql

From Dev

How to calculate the total of a sum in JSTL

From Dev

How to calculate "running total" in SQL

From Dev

How to calculate total price in php

From Dev

how to calculate total Sum in Excel

From Dev

How to calculate the total distance traveled?

From Dev

How to Calculate Total Less Duplicates?

From Dev

How to calculate Last column total

From Dev

How to calculate a running total in view?

From Dev

How to calculate total price of order?

From Dev

How to Calculate Bill Total JS

From Dev

how to calculate sum (Total) of DataTable Columns using C#

From Dev

How to calculate the total sum of items in array element using AngularJS

Related Related

  1. 1

    Getting total count of tickets sold for a table. Rails

  2. 2

    How to calculate % of total in MySQL

  3. 3

    How to calculate % of total in MySQL

  4. 4

    How an user choose a number of tickets and then ask for the prize of each ticket an make a total?

  5. 5

    Write a program that will ask the user for the number of tickets and output total

  6. 6

    how to get total and total of subset using mysql?

  7. 7

    How to get total no of sold products in magento

  8. 8

    How to get total no of sold products in magento

  9. 9

    how to minus sold stock from total stock?

  10. 10

    How to calculate the running total using aggregate

  11. 11

    How to calculate total disk space using df?

  12. 12

    Javascript: how to calculate the total

  13. 13

    Calculate percentage to total using rowPercents

  14. 14

    how to return total sold p / year for several years in columnar format?

  15. 15

    How to find total available and used memory using df command

  16. 16

    How to calculate total time between datetime stamps in MySQL

  17. 17

    how to calculate total number of saturday and sunday between two dates in mysql

  18. 18

    How to calculate the total of a sum in JSTL

  19. 19

    How to calculate "running total" in SQL

  20. 20

    How to calculate total price in php

  21. 21

    how to calculate total Sum in Excel

  22. 22

    How to calculate the total distance traveled?

  23. 23

    How to Calculate Total Less Duplicates?

  24. 24

    How to calculate Last column total

  25. 25

    How to calculate a running total in view?

  26. 26

    How to calculate total price of order?

  27. 27

    How to Calculate Bill Total JS

  28. 28

    how to calculate sum (Total) of DataTable Columns using C#

  29. 29

    How to calculate the total sum of items in array element using AngularJS

HotTag

Archive