你可以试试这个:
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