Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 11th, 2005, 06:54 AM
Registered User
 
Join Date: Apr 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Only one record as a result???

Perhaps an easy question... But I can't solve it.

I have two tables:

Tab 1: Includes itno, date and price
Tab 2: Includes itno och quantity

By connecting these two by itno I would like to get one (1) record, showing itno, latest date and a sum (price * quantity).

Example:

Tab 1:

Itno Date Price
1 Jan-10-2005 2,60
1 Feb-14-2005 3,30
1 Feb-7-2005 3,10
2 Jan-5-2005 5,50

Tab 2:
Itno Quant
1 120
2 330


I would like a record like this:

Itno Date Sum
1 Feb-14-2005 396


Please help me...


 
Old April 11th, 2005, 09:17 AM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 303
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am not convince that this will be an optimum solutions but give it a try

SELECT T1.ItemNo, Max(T1.[date]), (Max(T1.Price) * T2.Quant) As SUM FROM Table4 T1 INNER JOIN Table5 T2 ON T1.ItemNo = T2.ItemNo
Group By T1.ItemNo,T2.Quant

 
Old April 12th, 2005, 01:46 AM
Registered User
 
Join Date: Apr 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Almost there... The result from the solution above gives three lines for Itno 1, I would just like one row as a result, showing the result for the record of the itno with the highest date.

 
Old April 12th, 2005, 10:58 AM
Friend of Wrox
 
Join Date: Sep 2003
Posts: 155
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to leehambly
Default

********* There goes me stumbling around in the SQL board... doh! err, this is access, but should work! ****************************


You need to hide the price away to establish the "latest" date first and then include the price back in again to establish the "sum" (err, product... but anyway)... so, for me, that means a pair of queries...

query1:

SELECT [tab 1].itno, Max([tab 1].date) AS MaxOfdate
FROM [tab 1]
GROUP BY [tab 1].itno;


query2:

SELECT Query1.itno, Query1.MaxOfdate, [tab 1].price, [tab 2].quant, [price]*[quant] AS [Sum]
FROM (Query1 INNER JOIN [tab 1] ON (Query1.MaxOfdate = [tab 1].date) AND (Query1.itno = [tab 1].itno)) INNER JOIN [tab 2] ON Query1.itno = [tab 2].itno;


I am sure this is probably do-able in one nice clean query by someone with far better logic than me... but I like to keep it simple...
 
Old April 13th, 2005, 03:51 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Does this do what you want?

SELECT T1.ItemNo, T1.[Date], T1.Price * T2.Quant As TheSum
FROM [Tab 1] T1 INNER JOIN [Tab 2] T2 ON T1.Itno = T2.Itno
WHERE T1.[Date] = (SELECT MAX(Date) FROM [Tab 1])





Similar Threads
Thread Thread Starter Forum Replies Last Post
GridView and SQL More than one result = one result DarkForce ASP.NET 2.0 Basics 0 July 20th, 2007 04:29 AM
Delete a record row, not just the record. Coby Access VBA 1 April 30th, 2007 06:29 AM
how to add new record as first record in dataset [email protected] ASP.NET 1.0 and 1.1 Professional 4 April 21st, 2006 05:23 AM
Record locking - user needs the next queued record cbtoolkit SQL Server 2000 0 December 6th, 2004 08:29 AM
Sub record not associating with Main record Ron V Access 1 August 31st, 2004 09:21 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.