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 |
andrewcw
Posting Yak Master
133 Posts |
Posted - 2015-03-17 : 23:49:03
|
After looking at the web I have the idea this problem may be solved by use of Row_Number() over ( partition... but I dont understand how.Imagine I have a table likeCustomerID, PartNum, QtyinOrder, shipped1 6 3 01 6 2 02 6 1 02 5 1 02 5 2 02 5 3 02 5 4 11 6 4 12 6 2 1But I wanted to return CustomerID, PartNum, MaxQtyOrderedNotShipped That would be just the rows 1 6 3 02 6 1 02 5 3 0If I use this:Select CustomerId,PartNum, shipped, QtyInOrder AS MaxOrderedNotShippedfrom (SELECT [CustomerID] ,[PartNum] ,[QtyInOrder] ,shipped , row_number() over (partition by [CustomerID], PartNum order by QtyInOrder desc) as recid from [SILK].[dbo].[MaxofGroup]) as f where recid =1there is no restriction, so I get the shipped...If I alter the where clause to work only on not shipped, I get no records...as belowSelect CustomerId,PartNum, shipped, QtyInOrder AS MaxOrderedNotShippedfrom (SELECT [CustomerID] ,[PartNum] ,[QtyInOrder] ,shipped , row_number() over (partition by [CustomerID], PartNum order by QtyInOrder desc) as recid from [SILK].[dbo].[MaxofGroup]) as f where recid =1 and shipped=0What to try ?Thanks.andrewcw |
|
bitsmed
Aged Yak Warrior
545 Posts |
Posted - 2015-03-18 : 01:53:06
|
Try this:select customerid ,partnum ,max(qtyinorder) as max_qtyinorder from silk.dbo.maxofgroup where shipped=0 group by customerid ,partnum |
|
|
andrewcw
Posting Yak Master
133 Posts |
Posted - 2015-03-19 : 00:46:38
|
Thank you !!! That's exactly what I needed !!!andrewcw |
|
|
|
|
|
|
|