0

我需要从一个组(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),但我还想选择与获取的最低字段相关联的 id(a.id 字段)a.autonomia

4

4 回答 4

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 关联的所有自治权最小值。

于 2013-02-07T14:22:19.623 回答
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
于 2013-02-07T09:31:47.053 回答
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

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

于 2013-02-07T10:14:23.790 回答
-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
于 2013-02-07T09:23:28.290 回答