现在搜索一段时间,但没有找到合适的答案来解决我的问题。
说明:
ID | Year | Factor1 | Number1 | Number2 1 | 2010 | 213 | 1 | 1 2 | 2010 | 213 | 1 | 2 3 | 2010 | 214 | 2 | 1 4 | 2010 | 214 | 2 | 2 6 | 2010 | 210 | 3 | 1 7 | 2010 | 210 | 3 | 2 8 | 2011 | 250 | 3 | 5 5 | 2012 | 214 | 2 | 4
编辑: 忘记了某事,纠正了上表中的内容。 我需要2个组合:year和factor1只有一次,然后是min(number1)和last min(number2)。 例如上面我需要ID 1,3,5,6,8(抱歉它们是混合的,以便更好地读取其他值)。
任何人都知道怎么认识到这一点?
答案 0 :(得分:2)
SELECT id , year, number1, number2
FROM @table A
WHERE number1 IN (SELECT MIN(number1) FROM @table WHERE year = A.year)
OR number2 IN (SELECT MIN(number2) FROM @table WHERE year = A.year)
其中@table是你的表
答案 1 :(得分:1)
现在从另一个论坛获取代码,对我来说很好用:
SELECT tab.* FROM t33 tab INNER JOIN ( SELECT m1.y, m1.factor1, m1.min_1, m2.min_2 FROM ( SELECT y, factor1, MIN(number1) AS min_1 FROM t33 tab GROUP BY y, factor1 ) m1 INNER JOIN ( SELECT y, factor1, number1, MIN(number2) AS min_2 FROM t33 GROUP BY y, factor1, number1 ) m2 ON m1.y = m2.y AND m1.factor1 = m2.factor1 AND m1.min_1 = m2.number1 ) sel ON tab.y = sel.y AND tab.factor1 = sel.factor1 AND tab.number1 = sel.min_1 AND tab.number2 = sel.min_2
答案 2 :(得分:0)
好的,这样的事情呢?
delete from mb_test
where ID not in
( select id
from mb_test mbt
,(select year, min(number1) as min1, min(number2) as min2
from mb_test
group by year) mb_mins
where mbt.year = mb_mins.year
and (mbt.number1 = mb_mins.min1 OR mbt.number2 = mb_mins.min2)
)