SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Joins


Joins

Author
Message
cdl_9009
cdl_9009
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 Visits: 60
I am trying to pull out the vendor name and how much they owe. I am not getting the results I am looking for.

Vendor tableSadvendor_id, vendor_name)
1, Ace
2, Bond
3, Computer
4 , House

Order holds: (ord_id, vender_id, total_price)
1, 1, 100
2, 2, 100
3, 3, 150
4, 2, 300

account_payableSadap_id, ord_id)
1, 1
2, 2
3, 3

Expected results: Vendor_name, total_price
Bond, 300 --this is the order not paid yet

select VENDOR.vendor_name, (ORDERS.ORD_TOTAL_COST + Orders.ord_total_cost)as "Total Amount Owed"
from ORDERS LEFT JOIN VENDOR ON
ORDERS.VENDOR_ID = VENDOR.VENDOR_ID
LEFT JOIN ACCOUNT_PAYABLE ON
ORDERS.ORD_ID = ACCOUNT_PAYABLE.ORD_ID
WHERE ORDERS.ORD_ID = ORDERS.ORD_ID
cdl_9009
cdl_9009
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 Visits: 60
I think this is close but still isn't working. It is saying no data found but i know there is.

select VENDOR.vendor_name, (ORDERS.ORD_TOTAL_COST + Orders.ord_total_cost)as "Total Amount Owed"
from VENDOR JOIN ORDERS ON
ORDERS.VENDOR_ID = VENDOR.VENDOR_ID
JOIN ACCOUNT_PAYABLE ON
ACCOUNT_PAYABLE.ORD_ID = ORDERS.ORD_ID
WHERE ACCOUNT_PAYABLE.ORD_ID NOT IN ( SELECT ORDERS.ORD_ID
FROM ORDERS
WHERE ORDERS.ORD_ID = ACCOUNT_PAYABLE.ORD_ID );
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)

Group: General Forum Members
Points: 91821 Visits: 38954
cdl_9009 (10/18/2013)
I think this is close but still isn't working. It is saying no data found but i know there is.

select VENDOR.vendor_name, (ORDERS.ORD_TOTAL_COST + Orders.ord_total_cost)as "Total Amount Owed"
from VENDOR JOIN ORDERS ON
ORDERS.VENDOR_ID = VENDOR.VENDOR_ID
JOIN ACCOUNT_PAYABLE ON
ACCOUNT_PAYABLE.ORD_ID = ORDERS.ORD_ID
WHERE ACCOUNT_PAYABLE.ORD_ID NOT IN ( SELECT ORDERS.ORD_ID
FROM ORDERS
WHERE ORDERS.ORD_ID = ACCOUNT_PAYABLE.ORD_ID );



Didn't feel like creating the DDL for your tables or the scripts to populate them, so you will have to tell me if this works or not:



select
v.vendor_name,
o.total_price
from
Vendor v
inner join Order o
on (v.vendor_id = o.vendor_id)
where
not exists(select 1 from account_payable ap where ap.ord_id = o.ord_id);




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search