Solved

Get latest date from table join

Posted on 2013-11-24
10
390 Views
Last Modified: 2013-11-24
Hello there,

I have this query which gets back the records of products for a particular supplier. but now I want to filter the records to the latest purchase price date. see below my returned data.i am using mssql 2008 db. it should return id 6 and 8

cheers
Zolf

select
  pp.id,
  pp.priceDate,
  pp.purchasePrice,
  pp.salesPrice,
  pp.consumerPrice,
  pp.productID,
  pp.description
 from
  productPricing pp cross 
 join
  product p 
 where
  pp.productID=p.id 
  and p.supplierID=3 

Open in new window


id      priceDate              purchasePrice      salesPrice          consumerPrice      productID
4       2013-10-23 00:00:00      3000      3200             3400                               3
5       2013-11-20 00:00:00      3600      3700             3800                               3
6       2013-11-23 00:00:00      4000      4200             4500                               3
7       2012-11-21 00:00:00      500      600             800                               4
8       2013-06-30 00:00:00      950      1050             1200                               4
0
Comment
Question by:zolf
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 39673812
try this

select
  pp.id,
  pp.priceDate,
  pp.purchasePrice,
  pp.salesPrice,
  pp.consumerPrice,
  pp.productID,
  pp.description
 from
  productPricing pp inner
 join
  product p
 where
  pp.productID=p.id
  and p.supplierID=3
and  pp.priceDate in (

Select Max(p1.priceDate) from productPricing p1
where pp.productID = p1.id
group by p1.productID)
0
 

Author Comment

by:zolf
ID: 39673828
thanks for your feedback but no record is returned.....there was a mistake in your query(I replaced the where to ON near the inner join)
0
 
LVL 11

Expert Comment

by:Louis01
ID: 39673829
select pp.id, pp.priceDate, pp.purchasePrice, pp.salesPrice, pp.consumerPrice, p.productID, pp.description
  from product p
        inner join productPricing pp1 
    on pp1.productID = p.ProductID
 where p.supplierID=3 
   and exists (select pp2.ProductID, MAX(pp2.priceDate) as max_priceDate 
                 from productPricing pp2 
                where pp2.productID = pp1.productID 
                group by pp2.ProductID 
               having MAX(pp2.priceDate) = pp1.priceDate)

Open in new window

0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 39673831
try only this

Select p1.productID ,Max(p1.priceDate) from productPricing p1
group by p1.productID

this will return you two records you want
then you need to join this table with your query
This needs to work.
Let me know if this query gives you appropriate result
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 39673833
select
  pp.id,
  pp.priceDate,
  pp.purchasePrice,
  pp.salesPrice,
  pp.consumerPrice,
  pp.productID,
  pp.description
 from
  productPricing pp cross
 join
  product p  
inner join
(
Select p1.productID ,Max(p1.priceDate) from productPricing p1
group by p1.productID

) X on X.productID = pp.productID
 where
  pp.productID=p.id
  and p.supplierID=3
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

Author Comment

by:zolf
ID: 39673840
I get this error when I run your second query

 No column name was specified for column 2 of 'X'.
0
 

Author Comment

by:zolf
ID: 39673842
Louis01

I get error when I run your query

Invalid column name 'ProductID'. 2) [Error Code: 4104, SQL State: S1000]  The multi-part identifier "pp.id" could not be bound. 3) [Error Code: 4104, SQL State: S1000]  The multi-part identifier "pp.priceDate" could not be bound. 4) [Error Code: 4104, SQL State: S1000]  The multi-part identifier "pp.purchasePrice" could not be bound. 5) [Error Code: 4104, SQL State: S1000]  The multi-part identifier "pp.salesPrice" could not be bound. 6) [Error Code: 4104, SQL State: S1000]  The multi-part identifier "pp.consumerPrice" could not be bound. 7) [Error Code: 207, SQL State: 42S22]  Invalid column name 'productID'. 8) [Error Code: 4104, SQL State: S1000]  The multi-part identifier "pp.description" could not be bound.
0
 
LVL 11

Accepted Solution

by:
Louis01 earned 500 total points
ID: 39673850
My apologies pp. should be pp1.
select pp1.id, pp1.priceDate, pp1.purchasePrice, pp1.salesPrice, pp1.consumerPrice, p.productID, pp1.description
  from product p
        inner join productPricing pp1 
    on pp1.productID = p.ProductID
 where p.supplierID=3 
   and exists (select pp2.ProductID, MAX(pp2.priceDate) as max_priceDate 
                 from productPricing pp2 
                where pp2.productID = pp1.productID 
                group by pp2.ProductID 
               having MAX(pp2.priceDate) = pp1.priceDate)

Open in new window

0
 
LVL 9

Expert Comment

by:QuinnDex
ID: 39673856
select
  pp.id,
  pp.priceDate,
  pp.purchasePrice,
  pp.salesPrice,
  pp.consumerPrice,
  pp.productID,
  pp.description
 from
  productPricing pp cross
 join
  product p  
inner join
(
Select p1.productID ,Max(p1.priceDate) from productPricing p1
group by p1.productID

) productPricing X on X.productID = pp.productID
 where
  pp.productID=p.id
  and p.supplierID=3
0
 

Author Closing Comment

by:zolf
ID: 39673857
thanks it worked perfect
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now