If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > db2

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-31-09, 03:07
emmapat emmapat is offline
Registered User
 
Join Date: Mar 2009
Posts: 8
db2

hello i am trying to find the sum of one column from table A and subtract it from a value of table B...how this can be done
Reply With Quote
  #2 (permalink)  
Old 03-31-09, 04:27
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
b.value_x - SUM(a.column_y)

or

b.value_x - ( SUM(a.column_y) OVER(PARTITION BY ... ) )

depending on your requirement(sum over what group?).

Last edited by tonkuma; 03-31-09 at 04:32.
Reply With Quote
  #3 (permalink)  
Old 03-31-09, 04:48
emmapat emmapat is offline
Registered User
 
Join Date: Mar 2009
Posts: 8
actualy i'm a beginer in DB2 and i dont know how to compare those statements my actual problem is a booking flights annd im trying to find out the available seats in the table flights i have FlightID , FlightDate, Origin,Destination, MaxCapacity and in the other table te table flightbooking i have BookingID,CustomerID,FlightID,NumSeats,Status and BookingTime and i have to sum the numseats and subtruct from the max capacity.

But i cant find how to do the equation with alla the select statments... i mean to select...from...
select...from... where Flight.FLIGHTID = Flightbooking.Flightid
...and then i dont know how to complete that...can you help me??
Reply With Quote
  #4 (permalink)  
Old 03-31-09, 05:12
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Here is an example.

Code:
SELECT
       flt.FlightDate
     , flt.FlightID
     , MaxCapacity
     , MAX(Origin)      AS Origin
     , MAX(Destination) AS Destination
     , MaxCapacity - SUM(NumSeats) AS available_seats
     , COUNT(DISTINCT CustomerID) AS number_of_customer
  FROM
       flight        flt
  LEFT OUTER JOIN
       flightbooking fbk
   ON  flt.Flightid   = fbk.Flightid
   AND flt.FlightDate = fbk.FlightDate
 GROUP BY
       flt.FlightDate
     , flt.Flightid
     , MaxCapacity
;

Last edited by tonkuma; 03-31-09 at 05:28.
Reply With Quote
  #5 (permalink)  
Old 03-31-09, 05:59
emmapat emmapat is offline
Registered User
 
Join Date: Mar 2009
Posts: 8
thanks very match is working=D
Reply With Quote
  #6 (permalink)  
Old 03-31-09, 09:16
emmapat emmapat is offline
Registered User
 
Join Date: Mar 2009
Posts: 8
can i ask you something more??
------------------------------ Commands Entered ------------------------------
SELECT DISTINCT
flightBooking.FlightID,
Customer.CustomerID,
Customer.NAME
FROM
Customer
LEFT OUTER JOIN
flightbooking
ON flightbooking.flightid = 100;
------------------------------------------------------------------------------
SELECT DISTINCT flightBooking.FlightID, Customer.CustomerID, Customer.NAME FROM Customer LEFT OUTER JOIN flightbooking ON flightbooking.flightid = 100

FLIGHTID CUSTOMERID NAME
----------- ----------- --------------------
100 1 Andrew Brown
100 2 Andriani Papakwsta
100 3 Pra3ulla Theodoulou

3 record(s) selected.



here i'm trying to generate a list with the customer who are going to travel in a specific flight (100) but the result is a list with all the customers

i tried and..... ON flightbooking.status = 'r';
which mean that they are Reserved but i have the same problem

can you help me?
Reply With Quote
  #7 (permalink)  
Old 03-31-09, 10:16
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Why are you using outer join?
Reply With Quote
  #8 (permalink)  
Old 03-31-09, 10:36
emmapat emmapat is offline
Registered User
 
Join Date: Mar 2009
Posts: 8
because i'm using the flightid the customerid and the customer name
Reply With Quote
  #9 (permalink)  
Old 03-31-09, 10:52
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
OK then, do you know the difference between inner and outer joins? If not, I suggest you find some SQL guide for beginners and read it before you ruin your employer's reputation.
Reply With Quote
  #10 (permalink)  
Old 04-01-09, 06:45
emmapat emmapat is offline
Registered User
 
Join Date: Mar 2009
Posts: 8
that helps a lot thanks
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On