| Author |
Topic |
|
ryniers
Starting Member
5 Posts |
Posted - 2011-01-25 : 04:22:29
|
| Hi guysI need some helpI have a product table and a orders tableI have multiple orders for a product but I only need the earliest order to show.Products tableID Branch BranchCode Productcode ProductName1 Alpha za 123 Bread2 Beta xa 321 MilkOrders table ID BranchCode ProductCode DelDate1 za 123 10 Aug2 za 123 15 Aug3 za 321 9 Aug4 za 321 11 Aug 5 xa 123 11 Aug6 xa 123 16 Aug7 xa 321 10 Aug8 xa 321 12 Aug I will join on branch code and Product code, what I need to get is this:Branch BranchCode ProductCode ProductName DelDateAlpha za 123 Bread 10Aug Alpha za 321 Milk 9AugBeta xa 123 Bread 11AugBeta xa 321 Milk 10AugHow will I achieve this? Thanks for your helpRynier |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2011-01-25 : 04:39:18
|
SELECT p.Branch, p.BranchCode, p.ProductCode, p.ProductName, o.DelDateFROM Products AS pINNER JOIN (SELECT BranchCode, ProductCode, MIN(DelDate) AS DelDateFROM OrdersGROUP BY BranchCode, ProductCode) AS o ON o.BranchCode = p.BranchCodeAND o.ProductCode = p.ProductCode N 56°04'39.26"E 12°55'05.63" |
 |
|
|
ryniers
Starting Member
5 Posts |
Posted - 2011-01-25 : 04:50:16
|
| Thanks for the quick reply. Will give it a go. |
 |
|
|
ryniers
Starting Member
5 Posts |
Posted - 2011-01-25 : 05:49:19
|
| Hi PesoThat works, thank you. Just one thing AS o ON o.BranchCode = o.BranchCodeAND o.ProductCode = o.ProductCodeShould be AS o ON p.BranchCode = o.BranchCodeAND p.ProductCode = o.ProductCodeConfused me there for a moment.Thanks again. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2011-01-25 : 06:05:03
|
Yes. I changed my code above. N 56°04'39.26"E 12°55'05.63" |
 |
|
|
ryniers
Starting Member
5 Posts |
Posted - 2011-01-25 : 06:59:33
|
| Hi PescoAlthough your solution works in the example I gave you, it doesn't seem to work when I apply it my actual table. I must be missing something.Let me show you. Here I have 4 outstanding orders for a branch of which I only need the earliest one:This is my query: ( I am not selecting all the fields but the ones I ommitted are identical in this result anyway)SELECT [SupplierOrderID] ,[DimTimeFinancialID] ,[PurchaseOrderNumber] ,[DeliveryDate] ,[NoLaterThanDate] ,[OrderQty] ,[DeliveryDateCalendar] As DeliveryDate2 FROM FactSupplierOrders WHERE DimProductHierarchyID = '1200388' AND DimCompanyHierarchyID = '907' AND OrderStatusID < '3'This is the result:SupplierOrderID DimTimeFinancialID PurchaseOrderNumber DeliveryDate NoLaterThanDate OrderQty DeliveryDate2151218973 2157 55721100425503 2267 2281 10 2011-03-30151224124 2157 55721000425503 2224 2238 10 2011-02-15151236592 2211 55757300425503 2328 2342 7 2011-05-30151276484 2211 55757200425503 2298 2312 17 2011-04-30Then I run This Query:SELECT [SupplierOrderID] ,[DimTimeFinancialID] ,[PurchaseOrderNumber] ,[DeliveryDate] ,[NoLaterThanDate] ,[OrderQty] ,MIN([DeliveryDateCalendar]) As DeliveryDate2 FROM FactSupplierOrders WHERE DimProductHierarchyID = '1200388' AND DimCompanyHierarchyID = '907' AND OrderStatusID < '3' GROUP BY [SupplierOrderID] ,[DimTimeFinancialID] ,[PurchaseOrderNumber] ,[DeliveryDate] ,[NoLaterThanDate] ,[OrderQty] And this is the result:SupplierOrderID DimTimeFinancialID PurchaseOrderNumber DeliveryDate NoLaterThanDate OrderQty DeliveryDate2151218973 2157 55721100425503 2267 2281 10 2011-03-30151224124 2157 55721000425503 2224 2238 10 2011-02-15151236592 2211 55757300425503 2328 2342 7 2011-05-30151276484 2211 55757200425503 2298 2312 17 2011-04-30I should only be getting back one line with deliveryDate 2011-02-15What am I missing?Thanks Rynier |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2011-01-25 : 11:08:45
|
quote: Originally posted by ryniers Hi PescoAlthough your solution works in the example I gave you, it doesn't seem to work when I apply it my actual table. I must be missing something.
Common sense? Just kidding...If you want a solution for your actual problem, why post a pseudo-problem?Keep it simple. Post your actual problem and you'll get an actual solution. Simple as that. N 56°04'39.26"E 12°55'05.63" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2011-01-25 : 11:12:29
|
[code];WITH cteSourceAS ( SELECT SupplierOrderID, DimTimeFinancialID, PurchaseOrderNumber, DeliveryDate, NoLaterThanDate, OrderQty, DeliveryDateCalendar, ROW_NUMBER() OVER (PARTITION BY PurchaseOrderNumber ORDER BY DeliveryDateCalendar) AS RecID FROM dbo.FactSupplierOrders WHERE DimProductHierarchyID = '1200388' AND DimCompanyHierarchyID = '907' AND OrderStatusID < '3')SELECT SupplierOrderID, DimTimeFinancialID, PurchaseOrderNumber, DeliveryDate, NoLaterThanDate, OrderQty, DeliveryDateCalendarFROM cteSourceWHERE RecID = 1[/code] N 56°04'39.26"E 12°55'05.63" |
 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2011-01-25 : 13:14:04
|
| This is the first time in history that the Peso Mind Reading Act™ has failed!http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspxHow to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2011-01-25 : 14:34:45
|
Am too busy.Writing a new presenation about Relational Division and writing on my new book.Pregnant wife (expecting on March 19-21), and inducting my youngest to kindergarten. N 56°04'39.26"E 12°55'05.63" |
 |
|
|
ryniers
Starting Member
5 Posts |
Posted - 2011-01-26 : 04:20:09
|
| Peso Thanks for your help.Got it working. |
 |
|
|
|