Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Hi!I'm having trouble with the Min function. I have a set of data looking like this (this is a very small sample):Order_No.....Order_Line.....Order_Suffix---------------------------------------------------100123........1.................1100123........1.................2100123........2.................0I'm trying to find (by select) the unique combination of this order by first picking the lowest Order_Line by using MIN and then the lowest Order_Suffix, also by using MIN. I'm expecting a result looking like this:Order_No Order_Line Order_Suffix---------------------------------------------------100123........1.................1However, I'm getting a result containing to rows, like this:Order_No Order_Line Order_Suffix---------------------------------------------------100123........1.................1100123........1.................2How do I solve this? Best regards,Henrik
tkizer
Almighty SQL Goddess
38200 Posts
Posted - 2006-02-13 : 17:46:10
Try this:
SELECT y.Order_No, y.Order_Line, MIN(Order_Suffix) AS Order_SuffixFROM YourTable yINNER JOIN( SELECT Order_No, MIN(Order_Line) AS Order_Line FROM YourTable GROUP BY Order_No) tON y.Order_No = t.Order_No AND y.Order_Line = t.Order_LineGROUP BY y.Order_No, y.Order_Line
Tara Kizeraka tduggan
mmarovic
Aged Yak Warrior
518 Posts
Posted - 2006-02-14 : 03:49:17
Assuming suffix < @N (e.g. suffix is tinyInt so @N = 256):