tags:

views:

667

answers:

3

Hi,

Wonder if anyone can help me understand how to sum up the column of single column, ie a list of costs into one total cost.

I have been looking into this, and I think I'm on the right lines in undertsanding i need to sum the query, treat it as a subquery. However I'm not having much luck - do I need to give the subquery an alias, or is it a straight case of wrapping the query in a sum?

Here is the working query I want to sum up, all my attempts at sum left out for clarity!

SELECT TICKET_TYPE.PRICE AS TOTALSALES, RESERVATION.RESERVATION_ID,
       CINEMA.LOCATION, PERFORMANCE.PERFORMANCE_DATE
    FROM RESERVATION, TICKET, TICKET_TYPE, CINEMA, PERFORMANCE
    WHERE TICKET_TYPE.TICKET_TYPE_ID = TICKET.TICKET_TYPE_ID
      AND TICKET.RESERVATION_ID = RESERVATION.RESERVATION_ID
      AND RESERVATION.PERFORMANCE_ID = PERFORMANCE.PERFORMANCE_ID
      AND CINEMA.LOCATION = 'sometown'
      AND PERFORMANCE.PERFORMANCE_DATE = to_date('01/03/2009','DD/MM/yyyy');

some of the data...

TOTALSALES RESERVATION_ID LOCATION PERFORMANCE_DATE
    2.8     1     sometown     01-MAR-09
    3.5     2     sometown     01-MAR-09
    2.8     3     sometown     01-MAR-09
    2.8     3     sometown     01-MAR-09
    2.8     3     sometown     01-MAR-09
    2       4     sometown     01-MAR-09
    2.8     5     sometown     01-MAR-09

Thanks !

A: 

You can sum a single column with

select sum(mycolumn) from mytable

When you mix aggregators (e.g., sum(), count()) in the select list with fields then you change the meaning of the query. You have to include a GROUP BY clause and the clause must contain every non-aggregate part of the select list.

You could do the sum, by itself, in a nested subquery then include that output in the outer select...

Arnshea
thanks for the advice, i feel i understand sum now, eveerthings working.
A: 

Try:

You need to include a group by if you want the totals per ID.

SELECT SUM(TICKET_TYPE.PRICE) AS TOTALSALES
          , RESERVATION.RESERVATION_ID
          , CINEMA.LOCATION
          , PERFORMANCE.PERFORMANCE_DATE
       FROM RESERVATION
          , TICKET
          , TICKET_TYPE
          , CINEMA
          , PERFORMANCE
      WHERE TICKET_TYPE.TICKET_TYPE_ID = TICKET.TICKET_TYPE_ID 
        AND TICKET.RESERVATION_ID = RESERVATION.RESERVATION_ID 
        AND RESERVATION.PERFORMANCE_ID = PERFORMANCE.PERFORMANCE_ID 
        AND CINEMA.LOCATION = 'sometown' 
        AND PERFORMANCE.PERFORMANCE_DATE = to_date('01/03/2009','DD/MM/yyyy');
     GROUP BY RESERVATION.RESERVATION_ID

** pretty much the same answer as above, I need to get better at refreshing before posting**

northpole
hi - thanks for the comments - really helpful, when I play around with the sql I can now gett the output to sum the reservation total sales up, which is a step in the right direction - but I'm aiming for a total of every row to show the total for the whole period, so basically I will have one row with a big total in the end.
I think I may have cracked it now, I took out the reservation_id in the select and took it out of the group by (i had put all the select fields in) once i did that it seems to be working. Thanks for the help in understanding !
A: 

What you first need to know is what the "non summed part" of the query should be.

You want to calculate a SUM of X by Y (Sum of SALES by MONTH for example). Y can be any number of fields and the resulting data set will contain one record for each distinct combination of the Y fields.

Once you know that we can help you write your query.

Denis Troller
I managed to get it working - thanks for the advice