Solved

Query Syntax needed

Posted on 2013-12-04
5
160 Views
Last Modified: 2013-12-04
I want to bring out the latest price for a number of items from a table. In MsAccess I can use the Last() function:

SELECT StockCode.StockName, Last(UnitCost) AS Cost
FROM PurchaseOrderDetails GROUP BY StockCode,StockName

The last() function is not available in SQL Server, but in my table there is an Identity RecordNo field. So I could use the Max() value of that, but I'm unsure of how to bring out the UnitCost field. If I include it within the Group By clause, I get every different price for each item. If I omit it from the Group By clause I get an error.  I just want it to select the UnitCost which is on the record with the maximum value in RecordNo. So how do I modify the following statement in order to make it work?

SELECT  Max(REcordNo),  StockCode.StockName,UnitCost AS Cost
FROM PurchaseOrderDetails GROUP BY StockCode,StockName
0
Comment
Question by:TownTalk
  • 3
  • 2
5 Comments
 
LVL 32

Expert Comment

by:ewangoya
ID: 39695294
try
SELECT  REcordNo,  StockCode.StockName, UnitCost AS Cost
FROM PurchaseOrderDetails A
inner join  (select max(recordno) from PurchaseOrderDetails GROUP BY StockCode, StockName) B on A.RecordNo = B.RecordNo
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 39695302
Correction

SELECT  RecordNo,  StockCode, StockName, UnitCost AS Cost
FROM PurchaseOrderDetails A 
inner join (select max(recordno) [RecordNo] from PurchaseOrderDetails GROUP BY StockCode, StockName) B on A.RecordNo = B.RecordNo

Open in new window

0
 

Author Comment

by:TownTalk
ID: 39695335
Almost there I think. It gives an error: Ambiguous column name 'RecordNo'.
0
 
LVL 32

Accepted Solution

by:
ewangoya earned 500 total points
ID: 39695406
no problem, prefix it with A

SELECT  A.RecordNo,  StockCode, StockName, UnitCost AS Cost
FROM PurchaseOrderDetails A 
inner join (select max(recordno) [RecordNo] from PurchaseOrderDetails GROUP BY StockCode, StockName) B on A.RecordNo = B.RecordNo

Open in new window

0
 

Author Comment

by:TownTalk
ID: 39695461
Ok that works nicely now thank you.

Ian
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

707 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

12 Experts available now in Live!

Get 1:1 Help Now