TSQL获取最低值的id

时间:2013-02-07 08:59:24

标签: sql sql-server tsql aggregate-functions

我需要从组(MIN函数)中获取最小值,但也需要从其中获取相关的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),但我还想选择与最低get a.autonomia字段关联的id(a.id字段)。

4 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

更新以反映子查询中的所有条件。

答案 2 :(得分: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 *将生成一个表格,其中包含与每个ID相关联的 autonomia 的所有最小值。

答案 3 :(得分:-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