0

I have 2 tables.

The output I need is:

  1. Supplier_id
  2. supplier_name
  3. Grade_id
  4. grade_name
  5. customer_name
  6. customer_id

Query I am using is:

select distinct gs.Supplier_id,s.supplier_name,gs.Grade_id,g.grade_name,
c.customer_name,c.customer_id
 from tbl_Customer c, tbl_GradeSupplier gs
left join tbl_Suppliers s on s.supplier_ID = gs.Supplier_id
left join tbl_Grades g on g.grade_id = gs.Grade_id

However now i need to add to the return of this query the highest date valid from which is before the current date for each one of these as well as the old prices for that date This information can be found on the Prices table which holds

price_id    grade_id    supplier_id price_purchase_value    price_validfromdate customer_id price_sell_value    price_deleted
41  8   24  2190.00 2012-09-09 00:00:00.000 4   2280.00 NULL

I tried to do this:

select distinct gs.Supplier_id,s.supplier_name,gs.Grade_id,g.grade_name,
c.customer_name,c.customer_id
 from tbl_Customer c, tbl_GradeSupplier gs
left join tbl_Suppliers s on s.supplier_ID = gs.Supplier_id
left join tbl_Grades g on g.grade_id = gs.Grade_id
inner join
(select  grade_id,customer_id,max(price_validfromdate) as validfrom
 from  tbl_Prices a
where price_validfromdate <= GETDATE()
group by supplier_id,grade_id,customer_id) prices
on prices.customer_id = c.customer_id and prices.grade_id = gs.Grade_id

which gives me this error: The multi-part identifier "c.customer_id" could not be bound.

Any ideas?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    [Bad habits to kick : using old-style JOINs](https://sqlblog.org/2009/10/08/bad-habits-to-kick-using-old-style-joins) - that old-style *comma-separated list of tables* style was discontinued with the ANSI-**92** SQL Standard - stop using it – marc_s Nov 25 '12 at 20:15
  • Obvious question; does tbl_Customer (which is aliased with c) have a field named customer_id? – Bert Nov 25 '12 at 20:26

0 Answers0