对于每个不同的ID采用不同的值。

huangapple go评论48阅读模式
英文:

Taking different values for each different id

问题

我想从一列中仅使用每个ID的最高值,但在另一列中有多个值时,也要使用最低值:

对于每个不同的ID采用不同的值。

就像在这种情况下,我们有2行具有相同的ID 11024,我希望结果只显示每个ID的一行,但dtStart必须是最低的,而dtEnd必须是最高的:

(此图像是虚构的,只是为了显示应该是什么样子)

对于每个不同的ID采用不同的值。

我的选择是:

SELECT top 100 
id,					
min(dtStart) dtStart,
max(dtEnd) dtEnd		
FROM T_registable
WHERE sup_id = 3356
GROUP by 
id, dtStart, dtEnd

我尝试使用“min”/“max”值,尝试使用union all,尝试使用分区。

英文:

I want to use only the highest value from a column when there's multiples for each id, but also the lowest from another column when theres multiples:

对于每个不同的ID采用不同的值。

Like in this case we have 2 lines with the same id 11024, I want the result to only show a single line for each id, but the dtStart must be the lowest and the dtEnd must be the higher:

(this image was fabricated, just to show how it should be)

对于每个不同的ID采用不同的值。

My select is:

	SELECT top 100 
	id ,					
	min (dtStart	) dtStart,
	max (dtEnd	) dtEnd		

	FROM T_registable

	WHERE sup_id = 3356

	GROUP by 
	id, dtStart, dtEnd	

I tried to use "min"/"max" value, tried union all, tried to use partition by.

答案1

得分: 2

从你的GROUP BY子句中移除dtStart和dtEnd应该解决这个问题。TOP通常与ORDER BY一起使用。

SELECT
    id,
    MIN(dtStart) AS dtStart,
    MAX(dtEnd) AS dtEnd
FROM T_registable
WHERE sup_id = 3356
GROUP BY id
英文:

Removing dtStart and dtEnd from your GROUP BY clause should solve the issue. TOP is usually used with an ORDER BY.

SELECT
    id,
    MIN(dtStart) AS dtStart,
    MAX(dtEnd) AS dtEnd
FROM T_registable
WHERE sup_id = 3356
GROUP BY id

huangapple
  • 本文由 发表于 2023年2月14日 05:31:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441372.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定