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 |
|
mdavis
Starting Member
4 Posts |
Posted - 2009-06-29 : 12:46:30
|
| Table SalesCUSIP PurchDate SaleDate UnitsSold Proceeds74112E109 08/05/08 4/17/09 5,950 8,291.0474112E109 08/05/08 4/20/09 3,365 4,996.9004648X107 10/08/08 4/20/09 7,500 569.9804648X107 10/08/08 4/20/09 192,500 12,377.4204648X107 10/09/08 4/20/09 7,500 482.2304648X107 10/09/08 4/20/09 142,500 11,256.70Table InventoryCUSIP PurchDate OrigUnits OrigPrice 74112E109 08/05/08 15,068 3.3025 816288104 08/08/07 50,300 0.772499 04648X107 10/08/08 200,000 1.485 04648X107 10/09/08 200,000 1.25 023164106 12/14/00 16,500 0.58023164106 06/05/03 206,786 0.723998023164106 06/05/03 280,896 0.7241What I would like to do is run a SELECT query that will display each of the Inventory columns, plus an additional column AS TotalSold which is equal to the sum of the total units sold for each row of the inventory. My ouput would look like this: Table InventoryCUSIP PurchDate OrigUnits OrigPrice TotalSold74112E109 08/05/08 15,068 3.3025 9,315816288104 08/08/07 50,300 0.772499 004648X107 10/08/08 200,000 1.485 200,00004648X107 10/09/08 200,000 1.25 150,000023164106 12/14/00 16,500 0.58 0 023164106 06/05/03 206,786 0.723998 0023164106 06/05/03 280,896 0.7241 0From the Sales table, I've been able to put together a query that will give me the TotalSold.SELECT Sales.PurchDate, Sales.CUSIP, Sum(Sales.UnitsSold) AS TotalSoldFROM SalesGROUP BY Sales.PurchDate, Sales.CUSIP;I'm struggling with finding a way to get the output I'm looking for though.Mark DavisJ.D. Cloud & Co.www.jdcloud.com |
|
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2009-06-29 : 13:12:46
|
| [code]SELECT I.CUSIP, I.PurchDate, I.OrigUnits, I.OrigPrice ,COALESCE(D.TotalSold, 0) AS TotalSoldFROM Inventory I LEFT JOIN ( SELECT S.CUSIP, S.PurchDate, SUM(S.UnitsSold) As TotalSold FROM Sales S GROUP BY S.CusIP, S.PurchDate ) D ON I.CUSIP = D.CUSIP AND I.PurchDate = D.PurchDate[/code] |
 |
|
|
mdavis
Starting Member
4 Posts |
Posted - 2009-06-30 : 09:36:04
|
| Thanks. Would this query work if not on SQL server?Mark DavisJ.D. Cloud & Co.www.jdcloud.com |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-06-30 : 09:40:00
|
quote: Originally posted by mdavis Thanks. Would this query work if not on SQL server?Mark DavisJ.D. Cloud & Co.www.jdcloud.com
Most likely it should workMadhivananFailing to plan is Planning to fail |
 |
|
|
mdavis
Starting Member
4 Posts |
Posted - 2009-06-30 : 10:20:18
|
| Hm, because when I run it I get an "undefined function 'COALESCE' in expression" error.Mark DavisJ.D. Cloud & Co.www.jdcloud.com |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
mdavis
Starting Member
4 Posts |
Posted - 2009-06-30 : 11:08:43
|
quote: Originally posted by X002548
quote: Originally posted by mdavis Hm, because when I run it I get an "undefined function 'COALESCE' in expression" error.Mark DavisJ.D. Cloud & Co.www.jdcloud.com
Let's play guess the platform!!!!!Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam
I take it this an error unique to MS Access? I'm new to this.Mark DavisJ.D. Cloud & Co.www.jdcloud.com |
 |
|
|
|
|
|
|
|