ScoobyNet.com - Subaru Enthusiast Forum

ScoobyNet.com - Subaru Enthusiast Forum (https://www.scoobynet.com/)
-   Computer & Technology Related (https://www.scoobynet.com/computer-and-technology-related-34/)
-   -   database query - can't get this to work :( (https://www.scoobynet.com/computer-and-technology-related-34/361755-database-query-cant-get-this-to-work.html)

midget1500 14 September 2004 09:25 AM

database query - can't get this to work :(
 
simplistic view

3 tables
--------

booking accomodation activity
------- ------------- --------

id id id
num_people status status
total total
deposit deposit

where the id is common across all tables
status = paid or invoiced
debt = total-deposit

so, if i want to know the debt for each booking (i.e. where status = invoiced) how do i go about it. i've tried union queries, nested sql, etc but can't get a solution.

cheers
steven

Frosty The Snowman 14 September 2004 09:43 AM

Sorry if I am misunderstanding but it looks like you can just get the information from the one table, accomodation, unless the tabbing is really off the wall.

select id, (total-deposit)
from accomodation
where status = "invoiced"

If the tables are different from what they look like let me know and I'll have another bash.


Mark

Mark

midget1500 14 September 2004 11:36 AM

sorry, the accomodation element may have a status of paid, but perhaps the activity is invoiced but not paid, so the status's and amounts outstanding can differ.

SCOSaltire 14 September 2004 11:57 AM

dont know if this will definately work as ive tried it blind...

select booking.id, isnull(accomodation.total,0) + isnull(activity.total,0) as debt
from booking
join accomodation on
(booking.id=accomodation.id and accomodation.status='invoiced')
join activity on
(booking.id=activity.id and activity.status='invoiced')

tommy1980 14 September 2004 06:37 PM

quickly went through it and came up with this hopefully this is right


select booking.id,
(isnull(accomodation.total,0) - isnull(accomodation.deposit,0)) + (isnull(activity.total,0) - isnull(activity.deposit,0)) as debt
from booking, accomodation, activity where booking.id = accomodation.id and booking.id = activity.id and
(accomodation.status = 'invoiced' or activity.status = 'invoiced')


All times are GMT +1. The time now is 07:34 PM.


© 2024 MH Sub I, LLC dba Internet Brands