473,385 Members | 1,429 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

sql select max note for a row and join

Shashi Sadasivan
1,435 Expert 1GB
hi,
i have an issue with joining some table.
i have 3 tables.
product - (productID, total)
items - (productID, itemNo, total)
note - (noteID, productID, note)

each product has one or more items
each product has zero or more notes to it to the note

i need to show each product along with the last note put on it (there might be zero or more notes per product)

what i want to select is productID,total,item_no,note
the note should be the latest note for that productID
managed to get those rows which dont have a note. but cant work the other way around!

Thanks
-shashi
Aug 23 '07 #1
3 2472
ak1dnar
1,584 Expert 1GB
hi,
i have an issue with joining some table.
i have 3 tables.
product - (productID, total)
items - (productID, itemNo, total)
note - (noteID, productID, note)

each product has one or more items
each product has zero or more notes to it to the note

i need to show each product along with the last note put on it (there might be zero or more notes per product)

what i want to select is productID,total,item_no,note
the note should be the latest note for that productID
managed to get those rows which dont have a note. but cant work the other way around!

Thanks
-shashi


Hi Shashi,

When you joining note table with others two table you can Select the Maximum noteid for the specific product.

Do you think this is the one you looking for. Post back if not.

Expand|Select|Wrap|Line Numbers
  1. select items.productID,items.total,items.itemno,note.note from note
  2. Left join items on items.productID=note.productID 
  3. where note.productid=1001 and note.noteid=(select max(noteid) from note where note.productid=1001)
Aug 23 '07 #2
Shashi Sadasivan
1,435 Expert 1GB
Hi Shashi,

When you joining note table with others two table you can Select the Maximum noteid for the specific product.

Do you think this is the one you looking for. Post back if not.

Expand|Select|Wrap|Line Numbers
  1. select items.productID,items.total,items.itemno,note.note from note
  2. Left join items on items.productID=note.productID 
  3. where note.productid=1001 and note.noteid=(select max(noteid) from note where note.productid=1001)
Not really....
1. i have to list it for all products.
2. if there is no note for that product the product should still be shown
in the above case, i would need to programatically run this query for each product
and if there is a product without any note, it would not be shown
thanks
Aug 23 '07 #3
Shashi Sadasivan
1,435 Expert 1GB
Hi Shashi,

When you joining note table with others two table you can Select the Maximum noteid for the specific product.

Do you think this is the one you looking for. Post back if not.

Expand|Select|Wrap|Line Numbers
  1. select items.productID,items.total,items.itemno,note.note from note
  2. Left join items on items.productID=note.productID 
  3. where note.productid=1001 and note.noteid=(select max(noteid) from note where note.productid=1001)
Sorry about the last reply...
I did not realize about the left join and took it for granted.
Yes it does work great now....thanks mate
Aug 28 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Denis St-Michel | last post by:
Hello All, Hope some Guru will be able to help me with this. Let's take this example table A ------------------------------------------------------------------------------- id | ...
0
by: Denis St-Michel | last post by:
Hello all. Here are my Tables Tickets ------- id Notes -----
3
by: Ian T | last post by:
Hi, I've got what I think (probably incorrectly) should be a simple SELECT : Two colums with data like col1 col2 1 50 1 51 2 50
5
by: malcolm | last post by:
Example, suppose you have these 2 tables (NOTE: My example is totally different, but I'm simply trying to setup the a simpler version, so excuse the bad design; not the point here) CarsSold {...
4
by: jimh | last post by:
I'm not a SQL expert. I want to be able to write a stored procedure that will return 'people who bought this product also bought this...'. I have a user table that links to a transaction table...
17
by: kalamos | last post by:
This statement fails update ded_temp a set a.balance = (select sum(b.ln_amt) from ded_temp b where a.cust_no = b.cust_no and a.ded_type_cd = b.ded_type_cd and a.chk_no = b.chk_no group by...
3
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
1
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
0
by: Susan Bricker | last post by:
The following error: "The current field must match the join key '?' in the table that seves as t the 'one' side of one-to-many relationship. Enter a record in the 'one' side table with the...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.