Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
titsiros
Starting Member
2 Posts |
Posted - 2012-02-24 : 15:43:38
|
| I have two tables which I am trying to join, one holding pricing and another invoices.table1type|minqty|maxqty|date|priceA|1|6|02/24/2012|123A|3|5|02/25/2012|136table2invoice|type|qty123456|A|2456789|A|3I join them using :select invoice,type,qty,price from table1 inner jointable2 on table1.type=table2.type and qty between minqty and maxqtyBut I get a total of 3 records, I would like to only get 2 (one for each invoice). The rule is that if more than one pricing record qualifies then I wish to only get the one with the largest date.The desired resultset should be:invoice|type|qty|date|price123456 |A |2|02/24/2012|123456789 |A |3|02/25/2012|136Any idea on how to get rid of the duplicate record for invoice 456789? |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-25 : 11:43:34
|
quote: Originally posted by titsiros I have two tables which I am trying to join, one holding pricing and another invoices.table1type|minqty|maxqty|date|priceA|1|6|02/24/2012|123A|3|5|02/25/2012|136table2invoice|type|qty123456|A|2456789|A|3I join them using :select invoice,type,qty,price from table1 inner jointable2 on table1.type=table2.type and qty between minqty and maxqtyBut I get a total of 3 records, I would like to only get 2 (one for each invoice). The rule is that if more than one pricing record qualifies then I wish to only get the one with the largest date.The desired resultset should be:invoice|type|qty|date|price123456 |A |2|02/24/2012|123456789 |A |3|02/25/2012|136Any idea on how to get rid of the duplicate record for invoice 456789?
SELECT t1.invoice,t1.type,t1.qty,t2.date,t2.priceFROM table2 t1CROSS APPLY (SELECT TOP 1 date,price FROM table1 WHERE type=t1.type AND t1.qty BETWEEN minqty AND maxqty ORDER date DESC )t2 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
titsiros
Starting Member
2 Posts |
Posted - 2012-02-27 : 18:02:55
|
| thanks so much visakh16!I was able to find an alternative solution using Rank() over partition and only selecting the first record BUT your sql seems to be much faster than mine...SELECT * FROM (SELECT t1.invoice,t1.type,t1.qty,t2.date,t2.price,RANK() OVER( PARTION BY t1.invoice,t1.type ORDER BY t2.date desc) AS rankingFROM table1 INNER JOINtable2 ON table1.type=table2.type AND qty BETWEEN minqty AND maxqty) AS RANKED WHERE RANKING=1I guess it works both ways..? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-27 : 19:24:34
|
| welcomeit will work both ways------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|