SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 innerjoin to get rest of fields.with function
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

andrewcw
Posting Yak Master

USA
129 Posts

Posted - 05/21/2012 :  22:06:18  Show Profile  Reply with Quote
I have a calculated field that varies by customer. I need to get the min of the calculated fields for that customer, plus additional field at the record. I think this is a common pattern & I think I should be able to create an inner join back to get the add'l values.

The #temp is the table of values I pull back from another stored procedure:
Select MIN(ydiff)as Nearest, Customer from
(
Select * from #temp
) T
Group by Customer

EXAMPLE :

MIN for FRED 0.1080322
0.1080322 FRED
0.01831055 JACK

& the #Temp

FRED ITEM X -6.66314 ITEM Y 29.73999 0.260009766
FRED ITEM X -6.752899 ITEM Y 30.10803 0.108032227
FRED ITEM X -6.842802 ITEM Y 30.43213 0.432128906

The red line is what needs to be found & must match both customer & the min value.
and the min for all other customers and their respective rpws....

Thanks ...



andrewcw

khtan
In (Som, Ni, Yak)

Singapore
16746 Posts

Posted - 05/21/2012 :  22:15:12  Show Profile  Reply with Quote

select *
from
(
    select *, rn = row_number() over (partition by customer order by ydiff)
    from   #temp
) d
where d.rn = 1



KH
Time is always against us

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47136 Posts

Posted - 05/21/2012 :  22:18:27  Show Profile  Reply with Quote

SELECT t.*
FROM #temp t
INNER JOIN
(
Select MIN(ydiff)as Nearest, Customer 
from #temp
Group by Customer 
)t1
ON t1.Customer = t.Customer
AND t1.Nearest = t.ydiff


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

andrewcw
Posting Yak Master

USA
129 Posts

Posted - 05/21/2012 :  22:52:04  Show Profile  Reply with Quote
Thank you very much !!!! Works !!!

andrewcw
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000