2013-02-07 85 views
0

我需要從組中獲取最小值(MIN函數),還需要獲取其相關ID。TSQL獲取最低值的ID

SELECT 
    pv.id, 
    MIN(pv.auto)+MIN(a.autonomia) as autonomia, 
    -->> a.id as minid 
from @autonomie a 
join VARDB v on a.id=v.IDARTICO 
join RIDIBA r on v.id=r.IDVARDB and r.idartico IS NOT NULL 
join @PvTable pv on pv.id=r.IDARTICO 
group by pv.id 

在這裏,我計算出所選字段autonomia通過MIN(a.autonomia),但我還想選擇ID(a.id場)與最低getted a.autonomia字段關聯。

+0

你的意思是'MIN(a.id)'?或者那個有'MIN(pv.auto)+ MIN(a.autonomia)'的id?在這種情況下,你不能在同一個查詢中用'group by pv.id'來做到這一點。 –

+1

聽起來好像你想要每個id的最小值,你可以使用'MIN(pv.auto + a.autonomia)OVER(PARTITION BY id)'。 –

回答

-1

對值進行分組後,您可以對結果集進行排序並從結果集中獲取排名前1。

SELECT TOP 1 
pv.id, 
MIN(pv.auto)+MIN(a.autonomia) as autonomia, 
-->> a.id as minid 
from @autonomie a 
join VARDB v on a.id=v.IDARTICO 
join RIDIBA r on v.id=r.IDVARDB and r.idartico IS NOT NULL 
join @PvTable pv on pv.id=r.IDARTICO 
group by pv.id 
ORDER BY autonomia 
+1

不,我很抱歉,我需要Min(a.autonomia)選擇的a.id(不是pv.id) – Tobia

0

你可以用這個嘗試:

SELECT pv.id 
     , ROW_NUMBER() OVER (PARTITION BY pv.id ORDER BY pv.auto) pv_auto 
     , ROW_NUMBER() OVER (PARTITION BY pv.id ORDER BY a.autonomia) a_autonomia 
     , a.id a_id 
     , pv.auto 
     , a.autonomia 
INTO #temp 
FROM @autonomie a 
JOIN VARDB v ON a.id=v.IDARTICO 
JOIN RIDIBA r ON v.id=r.IDVARDB and r.idartico IS NOT NULL 
JOIN @PvTable pv ON pv.id=r.IDARTICO 

SELECT temp_pv_auto.id 
     , temp_pv_auto.auto + temp_a_autonomia.autonomia AS autonomia 
     , temp_a_autonomia.a_id 
FROM #temp temp_pv_auto 
JOIN #temp temp_a_autonomia ON 
     temp_pv_auto.id = temp_a_autonomia.id 
AND  temp_pv_auto.pv_auto = 1 
AND  temp_a_autonomia.a_autonomia = 1 

您也可以嘗試此查詢:

SELECT pv.id 
     , MIN(pv.auto) pv_auto_min 
     , MIN(a.autonomia) a_autonomia_min 
INTO #temp 
FROM @autonomie a 
JOIN VARDB v ON a.id=v.IDARTICO 
JOIN RIDIBA r ON v.id=r.IDVARDB and r.idartico IS NOT NULL 
JOIN @PvTable pv ON pv.id=r.IDARTICO 
GROUP BY 
     pv.id 


SELECT pv.id 
     , #temp.pv_auto_min + #temp.a_autonomia_min AS autonomia 
     , a.id a_id 
FROM @autonomie a 
JOIN VARDB v ON a.id=v.IDARTICO 
JOIN RIDIBA r ON v.id=r.IDVARDB and r.idartico IS NOT NULL 
JOIN @PvTable pv ON pv.id=r.IDARTICO 
JOIN #temp ON 
     #temp.id = pv.id 
AND  #temp.a_autonomia_min = a.autonomia 
+0

我在8分鐘後中止查詢...原始查詢大約需要3秒。 – Tobia

+0

好吧,我踢出了CTE,現在應該會更快,否則,我會再次重寫它,而不用'ROW_NUMBER'和'PARTITION BY'。你可以再試一次嗎? –

+0

我添加了另一個查詢,你可以試試嗎? –

0
SELECT pv.id, 
    MIN(pv.auto)+MIN(a.autonomia) as autonomia, 
    (
     SELECT a2.id from @autonomie a2 
      join VARDB v2 on a2.id=v2.IDARTICO 
      join RIDIBA r2 on v2.id=r2.IDVARDB and r2.idartico IS NOT NULL 
      join @PvTable pv2 on pv2.id=r2.IDARTICO 
     where a2.autonomia = MIN(a.autonomia) 
    ) 
    -->> a.id as minid 
from @autonomie a 
join VARDB v on a.id=v.IDARTICO 
join RIDIBA r on v.id=r.IDVARDB and r.idartico IS NOT NULL 
join @PvTable pv on pv.id=r.IDARTICO 
group by pv.id 

更新,以反映子查詢的所有條件。

+0

您的子查詢忽略連接限制,MIN(a.autonomia)未從整個自治表中獲取最小值,但是此表與VARDB RIDIBA和PvTable連接的結果。 – Tobia

+0

如果從子查詢返回多個記錄(也就是說,@ autonomie.autonomia中的多個記錄的值爲'MIN(a.autonomia)'),則會出現錯誤「子查詢返回多個值」。 –

+0

@Tobia,你會相應地改變子查詢。因此,使它具有相同的條件/連接。我不確定如果沒有子查詢列,你會怎麼做,但我有興趣看到。 –

0

我不能完全肯定我明白的要求,但你可能會使用子查詢嘗試:

SELECT 
    ,pv.id 
    ,(MIN(pv.auto) + autonomie_min.autonomia) as autonomia 
    ,autonomie_min.id as autonomia_id 
from 
    VARDB v 
    JOIN 
    (
     SELECT 
      a.id 
      ,MIN(a.autonomia) as autonomia 
     FROM 
      @autonomie a 
     GROUP BY 
      a.id 
    ) AS autonomie_min ON 
     (autonomie_min.id = v.IDARTICO) 
    JOIN 
    RIDIBA r ON 
     (r.IDVARDB = v.id) AND 
     (r.idartico IS NOT NULL) 
    JOIN 
    @PvTable pv ON 
     (pv.id = r.IDARTICO) 
GROUP BY 
    pv.id 

子查詢* autonomie_min *會產生一個表與各相關autonomia的所有最小值ID。