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 |
y0n_1p
Starting Member
3 Posts |
Posted - 2007-04-19 : 06:41:24
|
I have two tables, a master and details table, and I want to create a query to return a set of records containing unique itemcodes with their latest costs. The tables are actually delivery tables with some of the following fields:MASTER TABLEIDReferenceNoTransactionDateDETAILS TABLEIDReferenceNoItemCodeItemCostThe two tables are linked by the reference number field.How do I select a unique list of ALL the itemcodes showing ONLY their latest costs? The items have different delivery dates and some of them are delivered several times with costs possibly changing on each delivery.Thanks in advance. |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-19 : 06:46:10
|
It is not that tricky if you have paid attention in class.You have to use MAX and INNER JOIN.Peter LarssonHelsingborg, Sweden |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-19 : 07:01:14
|
Or correlated subquerySELECT d.*FROM DetailsTable AS dWHERE d.ReferenceNo = (SELECT TOP 1 m.ReferenceNo FROM MasterTable AS m WHERE m.ReferenceNo = d.ReferenceNo ORDER BY m.TransactionDate DESC) Peter LarssonHelsingborg, Sweden |
|
|
y0n_1p
Starting Member
3 Posts |
Posted - 2007-04-19 : 08:18:14
|
Thank you very much for your rather polite answer. But, if you have paid attention to my question, you might have given me a working solution.To better understand my question, here is an example:MASTERTABLEID ReferenceNo TransactionDate1 ABC-123 01/01/20072 ABC-456 01/02/20073 ABC-789 01/03/2007DETAILSTABLEID ReferenceNo ItemCode ItemCost1 ABC-123 00001 10.002 ABC-123 00002 12.003 ABC-456 00001 11.004 ABC-456 00003 15.005 ABC-456 00004 14.006 ABC-789 00001 9.007 ABC-789 00004 15.008 ABC-789 00005 8.00This is the resultset I need:ItemCode ItemCost00001 9.0000002 12.0000003 15.0000004 15.0000005 8.00Again, thank you in advance. |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-04-19 : 08:40:14
|
[code]-- prepare sample datadeclare @master table( ID int, referenceno varchar(20), TransactionDate datetime)declare @details table( ID int, ReferenceNo varchar(20), ItemCode varchar(10), ItemCost numeric(25,2))insert @masterselect 1, 'ABC-123', '01/01/2007' union allselect 2, 'ABC-456', '01/02/2007' union allselect 3, 'ABC-789', '01/03/2007'insert @detailsselect 1, 'ABC-123', '00001', 10.00 union allselect 2, 'ABC-123', '00002', 12.00 union allselect 3, 'ABC-456', '00001', 11.00 union allselect 4, 'ABC-456', '00003', 15.00 union allselect 5, 'ABC-456', '00004', 14.00 union allselect 6, 'ABC-789', '00001', 9.00 union allselect 7, 'ABC-789', '00004', 15.00 union allselect 8, 'ABC-789', '00005', 8.00-- final queryselect t1.ItemCode, t1.ItemCostfrom @details t1 join @master t2 on t1.referenceno = t2.referencenoJoin( select d.ItemCode, max(TransactionDate) as TransactionDate from @details d join @master m on d.referenceno = m.referenceno group by d.ItemCode) t3on t1.ItemCode= t3.ItemCode and t2.TransactionDate = t3.TransactionDateorder by 1[/code]Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-19 : 08:59:22
|
Shorter, based on clearer specifications...SELECT d.*FROM @Details AS dWHERE d.ID = (SELECT TOP 1 q.ID FROM @Master AS m INNER JOIN @Details AS q ON q.ReferenceNo = m.ReferenceNo WHERE q.ItemCode = d.ItemCode ORDER BY m.TransactionDate DESC)ORDER BY d.ItemCode Peter LarssonHelsingborg, Sweden |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-04-19 : 09:14:07
|
Peter, SELECT * FROM ...is bad practice, don't you know that? Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2007-04-19 : 10:55:00
|
quote: But, if you have paid attention to my question, you might have given me a working solution.
This makes me want to type out every swear word I know.[Signature]For fast help, follow this link:http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspxLearn SQLhttp://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
|
|
y0n_1p
Starting Member
3 Posts |
Posted - 2007-04-19 : 11:55:00
|
My deepest thanks to both Harsh and Peter. Both solutions give me the resultset I needed.quote: Originally posted by DonAtWork
quote: But, if you have paid attention to my question, you might have given me a working solution.
This makes me want to type out every swear word I know.
I'm so sorry DonAtWork. I wasn't aware that new forum members must keep mum when insulted. You may insult me too if you want. |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2007-04-19 : 12:04:57
|
You asked for these folks to do your work for you, not explain how it works, or to help you understand. Then you get insulting when they attempt to HELP you instead of DOING YOUR WORK FOR YOU. Now do you understand?[Signature]For fast help, follow this link:http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspxLearn SQLhttp://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
|
|
|
|
|
|
|