Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Joins Expand / Collapse
Author
Message
Posted Friday, October 18, 2013 7:01 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, July 29, 2014 9:15 AM
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 tablevendor_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_payableap_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
Post #1506350
Posted Friday, October 18, 2013 7:43 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, July 29, 2014 9:15 AM
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 );
Post #1506353
Posted Saturday, October 19, 2013 8:20 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 6:26 PM
Points: 20,732, Visits: 32,496
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);





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)
Post #1506404
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse