如何在PostgreSQL中比较同一条目的两行?
下面是我对Postgres数据库运行的查询如何在PostgreSQL中比较同一条目的两行?,sql,postgresql,comparison,aggregate-functions,Sql,Postgresql,Comparison,Aggregate Functions,下面是我对Postgres数据库运行的查询 select distinct(col1),col2,col3,col4 from tableNot where col5 = 100 and col6 = '78' order by col4 DESC limit 100; 下面是我在控制台上得到的输出- col1 col2 col3 col4 entry.one 1.2.3 18
select distinct(col1),col2,col3,col4
from tableNot
where col5 = 100 and col6 = '78'
order by col4 DESC limit 100;
下面是我在控制台上得到的输出-
col1 col2 col3 col4
entry.one 1.2.3 18 subject
entry.one 1.2.8 18 newSubject
entry.two 3.4.9 20 lifePerfect
entry.two 3.4.5 17 helloPartner
- 现在,如果您看到我上面的输出,
和entry.one
将出现两次,因此我将比较entry.two
的entry.one
值,以较高者为准,我将保留该行。所以对于col2
条目。我会将
与1.2.3
进行比较,1.2.8
对于1.2.8
,所以我只为条目更高。一个
李>条目保留此行。一个
- 与
entry.two类似,我将
与3.4.9
进行比较,3.4.5
entry.two的
更高。因此,我将仅为3.4.9
entry.two保留此行
- 与
col1 col2 col3 col4
entry.one 1.2.8 18 newSubject
entry.two 3.4.9 20 lifePerfect
这可以在SQL中实现吗
p.S任何小提琴的例子都很好。试试这个查询:
SELECT * FROM
(select distinct(col1),col2,col3,col4 from tableNot
where col5 = 100 and col6 = '78' order by col4 DESC limit 100)t1
WHERE t1.col2 = (SELECT MAX(t2.col2) FROM (select distinct(col1),col2,col3,col4
from tableNot
where col5 = 100 and col6 = '78' order by col4 DESC limit 100)t2
WHERE t1.col1 = t2.col1 group by t2.col1)
请尝试以下查询:
SELECT * FROM
(select distinct(col1),col2,col3,col4 from tableNot
where col5 = 100 and col6 = '78' order by col4 DESC limit 100)t1
WHERE t1.col2 = (SELECT MAX(t2.col2) FROM (select distinct(col1),col2,col3,col4
from tableNot
where col5 = 100 and col6 = '78' order by col4 DESC limit 100)t2
WHERE t1.col1 = t2.col1 group by t2.col1)
请尝试以下查询:
SELECT * FROM
(select distinct(col1),col2,col3,col4 from tableNot
where col5 = 100 and col6 = '78' order by col4 DESC limit 100)t1
WHERE t1.col2 = (SELECT MAX(t2.col2) FROM (select distinct(col1),col2,col3,col4
from tableNot
where col5 = 100 and col6 = '78' order by col4 DESC limit 100)t2
WHERE t1.col1 = t2.col1 group by t2.col1)
请尝试以下查询:
SELECT * FROM
(select distinct(col1),col2,col3,col4 from tableNot
where col5 = 100 and col6 = '78' order by col4 DESC limit 100)t1
WHERE t1.col2 = (SELECT MAX(t2.col2) FROM (select distinct(col1),col2,col3,col4
from tableNot
where col5 = 100 and col6 = '78' order by col4 DESC limit 100)t2
WHERE t1.col1 = t2.col1 group by t2.col1)
您可以尝试另一个简单查询:
SELECT DISTINCT t1.col1, t1.col2, t1.col3, t1.col4
FROM tableNot t1
INNER JOIN (SELECT MAX(col2) col2
FROM tableNot GROUP BY col1) t2 ON t1.col2=t2.col2
WHERE col5 = 100 AND col6 = '78' ORDER BY col4 DESC LIMIT 100;
您可以尝试另一个简单查询:
SELECT DISTINCT t1.col1, t1.col2, t1.col3, t1.col4
FROM tableNot t1
INNER JOIN (SELECT MAX(col2) col2
FROM tableNot GROUP BY col1) t2 ON t1.col2=t2.col2
WHERE col5 = 100 AND col6 = '78' ORDER BY col4 DESC LIMIT 100;
您可以尝试另一个简单查询:
SELECT DISTINCT t1.col1, t1.col2, t1.col3, t1.col4
FROM tableNot t1
INNER JOIN (SELECT MAX(col2) col2
FROM tableNot GROUP BY col1) t2 ON t1.col2=t2.col2
WHERE col5 = 100 AND col6 = '78' ORDER BY col4 DESC LIMIT 100;
您可以尝试另一个简单查询:
SELECT DISTINCT t1.col1, t1.col2, t1.col3, t1.col4
FROM tableNot t1
INNER JOIN (SELECT MAX(col2) col2
FROM tableNot GROUP BY col1) t2 ON t1.col2=t2.col2
WHERE col5 = 100 AND col6 = '78' ORDER BY col4 DESC LIMIT 100;
您可能正在查找
distinct on
,它是distinct
操作符的Postgres特定扩展
select distinct on (col1) col1, col2, col3, col4
from tableNot
where col5 = 100
and col6 = '78'
order by col1, col4 DESC
limit 100;
请注意,
distinct
不是一个函数selectdistinct(col1),col2,col3
与selectdistinct col1,col2,col3
完全相同
(标准SQL)distinct
运算符始终对选择列表的所有列进行操作,而不仅仅是一列
select distinct(col1)、col2
和select distinct col1、col2
之间的差异与select(col1)、col2
和select col1之间的差异相同,col2
您可能正在查找distinct on
,它是distinct
操作符的Postgres特定扩展
select distinct on (col1) col1, col2, col3, col4
from tableNot
where col5 = 100
and col6 = '78'
order by col1, col4 DESC
limit 100;
请注意,
distinct
不是一个函数selectdistinct(col1),col2,col3
与selectdistinct col1,col2,col3
完全相同
(标准SQL)distinct
运算符始终对选择列表的所有列进行操作,而不仅仅是一列
select distinct(col1)、col2
和select distinct col1、col2
之间的差异与select(col1)、col2
和select col1之间的差异相同,col2
您可能正在查找distinct on
,它是distinct
操作符的Postgres特定扩展
select distinct on (col1) col1, col2, col3, col4
from tableNot
where col5 = 100
and col6 = '78'
order by col1, col4 DESC
limit 100;
请注意,
distinct
不是一个函数selectdistinct(col1),col2,col3
与selectdistinct col1,col2,col3
完全相同
(标准SQL)distinct
运算符始终对选择列表的所有列进行操作,而不仅仅是一列
select distinct(col1)、col2
和select distinct col1、col2
之间的差异与select(col1)、col2
和select col1之间的差异相同,col2
您可能正在查找distinct on
,它是distinct
操作符的Postgres特定扩展
select distinct on (col1) col1, col2, col3, col4
from tableNot
where col5 = 100
and col6 = '78'
order by col1, col4 DESC
limit 100;
请注意,
distinct
不是一个函数selectdistinct(col1),col2,col3
与selectdistinct col1,col2,col3
完全相同
(标准SQL)distinct
运算符始终对选择列表的所有列进行操作,而不仅仅是一列
select distinct(col1),col2
和select distinct col1,col2
之间的区别与select(col1),col2
和select col1,col2之间的区别相同。感谢Hamidreza,运行查询后我得到以下错误:错误:的参数和必须是布尔类型,not type integer LINE 2:…t1.col6='91'order by t1.col4 DESC limit 100^*********错误*********错误:AND的参数必须是布尔类型,不输入整型SQL state:42804字符:181不客气,我的朋友。我的查询只是给你一些线索。因为我看不到你真正的表。在SQL fiddle
中查看我的查询,它将对你查找结果非常有帮助。我编辑我的查询,我的朋友,它基于你的查询,如果你的查询返回值,我的查询也将返回值。在查询结束时,您有groupbyt2.col)
这里col是什么意思?是col2还是col3?你说得对,我的朋友,这是我的错,我现在编辑它是groupbycol1
谢谢Hamidreza,运行查询后我得到以下错误:error:and的参数必须是boolean类型,not type integer LINE 2:…t1.col6='91'order by t1.col4 DESC limit 100^*********错误*********错误:AND的参数必须是布尔类型,不输入整型SQL state:42804字符:181不客气,我的朋友。我的查询只是给你一些线索。因为我看不到你真正的表。在SQL fiddle
中查看我的查询,它将对你查找结果非常有帮助。我编辑我的查询,我的朋友,它基于你的查询,如果你的查询返回值,我的查询也将返回值。在查询结束时,您有groupbyt2.col)
这里col是什么意思?是col2还是col3?你说得对,我的朋友,这是我的错,我现在编辑它是groupbycol1
谢谢Hamidreza,运行你的查询后,我得到以下错误: