2013-01-15 85 views

回答

0

你可以使用row_number()

select shop_id, 
    shop_name, 
    shop_address 
from 
(
    select t1.shop_id, 
    t1.shop_name, 
    t2.shop_address, 
    row_number() over(partition by t1.shop_id 
         order by t1.shop_name, t2.shop_address) rn 
    from table_1 t1 
    inner join table_2 t2 
    on t1.shop_id = t2.shop_id 
) src 
where rn = 1 

看到一個demo on SQL Fiddle

您還可以使用聚合函數:

select t1.shop_id, 
    t1.shop_name, 
    max(t2.shop_address) shop_address 
from Table1 t1 
inner join table2 t2 
    on t1.shop_id = t2.shop_id 
group by t1.shop_id, t1.shop_name 

SQL Fiddle with Demo

+0

感謝您的回答,問題已解決:)),非常感謝:) – hackchick

+0

歡迎您幫助 – Taryn

+0

但row_number()over(分區)無法轉換爲linq,linq不支持row_number() :( – hackchick

相關問題