我使用两种不同的Database POSTGRE-SQL和MSSQL与java-hibernate Criteria API。标准的Sql翻译就像:
Select * From table t WHERE (t.a, t.b) IN
(SELECT max(this_.a) AS y0_,
this_.b AS y1_
FROM table this_
GROUP BY this_.id
)
这个带有Where-in-multi-columns的查询在PostGre数据库中运行良好但不适用于MSSQL。可以建议我使用聚合函数进行多列比较的SQL解决方法吗?
答案 0 :(得分:1)
使用联接:
Select *
From table t
inner join (
SELECT max(this_.a) AS y0_
, this_.b AS y1_
FROM table this_
Group by this_.b) a on t.a = a.y0_ and t.b = y1_
答案 1 :(得分:0)
如果是varchar
SELECT *
FROM table t
WHERE (t.a + '-' + t.b)
IN (SELECT (max(this_.a) + '-' + this_.b)
FROM table this_ )
如果是int
SELECT *
FROM table t
WHERE (CONVERT(nvarcahr(32), t.a) + '-' + CONVERT(nvarcahr(32), t.b))
IN (SELECT (CONVERT(nvarcahr(32), max(this_.a)) + '-' + CONVERT(nvarcahr(32),this_.b))
FROM table this_ )