你好 刚刚问你的Mysql在pgsql中遇到个问题 可以帮忙看下?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了你好 刚刚问你的Mysql在pgsql中遇到个问题 可以帮忙看下?相关的知识,希望对你有一定的参考价值。

select * from (select *, concat(name,code) as __f from test order by date desc) __t group by __f;

在pgsql中报
错误: 字段 "__t.id" 必须出现在 GROUP BY 子句中或者在聚合函数中使用
LINE 1: select * from (select *, concat(name,code) as __f from tes...
^
********** 错误 **********

错误: 字段 "__t.id" 必须出现在 GROUP BY 子句中或者在聚合函数中使用
SQL 状态: 42803
字符:8

如果写成这样就行 可是不能去重复数据了
select __t.id from (select *, concat(name,code) as __f from test order by date desc) __t group by __t.id,__f;
上面的sql(select * from (select *, concat(name,code) as __f from test order by date desc) __t group by __f;
)在mysql运行没问题

表结构 & 数据

id date name code

1 2015-1-1 张三 1111
2 2015-2-1 李四 2222
3 2015-3-1 张三 1111
4 2015-4-1 王五 3333
5 2015-5-1 赵六 4444

根据字段name和code的值 如果两个值都相同的数据 就按date最大的保留

例如
id为1和3的 name和code都一样 就保留date最大即Id为3的这条数据

参考技术A

PostgreSQL的话,用这个:

select * from test where id in (select max(id) from test group by concat(name,code));

来自:求助得到的回答
参考技术A 你写的两个SQL都是错误的。。你可以把你的表结构说明一下然后想要的结果也说一下,看看能不能帮你写一下吧追问

你好 问题已经补充了

追答SELECT *
FROM AAA_TEST T1
WHERE NOT EXISTS(SELECT 1 FROM AAA_TEST T2 WHERE T1.NAME=T2.NAME AND T1.CODE=T2.CODE AND T2.DATE>T1.DATE)
;

追问

谢谢你哈

本回答被提问者采纳

如何纠正 PL/pgSQL 中缺少引用的“一对多表”?

【中文标题】如何纠正 PL/pgSQL 中缺少引用的“一对多表”?【英文标题】:How to correct ‘One to Many table’ with missing references in PL/pgSQL? 【发布时间】:2019-02-10 16:01:51 【问题描述】:

我有一个大型数据文件,在每个相关子集的第一条记录上都有一个唯一的 11 位引用,但在属于相同子集的后续记录上没有,而是具有虚假的 1 或 2 位引用。该表按照最初创建的顺序排列,因此子集仍然正确分组在一起,包含这些引用的字段称为“旧引用”。我刚刚创建了一个名为“New Ref”的新字段,我想用每个子集的所有适当的 11 位引用填充该字段。如何在我可以在 PL/pgSQL 上运行的脚本中最好地实现这一点?

以下是数据的示例(当前),我希望它更新:

   Current datafile
__________________________________
ID  | Old Ref       | New Ref
==================================
1   | 14740807000   |
2   | 1             |    
3   | 2             |    
4   | 3             |
5   | 58            |
6   | 14735113000   | 
7   | 1             |               
8   | 2             |    
9   | 39            |   
10  | 4             |    
11  | 5             |    
12  | 14915146000   | 
13  | 9             |        
14  | 27            | 
15  | 14915146000   | 
16  | 3             | 
17  | 4             |    
==================================

Sought updated datafile
__________________________________
ID  | Old Ref       | New Ref
==================================
1   | 14740807000   | 14740807000
2   | 1             | 14740807000    
3   | 2             | 14740807000    
4   | 3             | 14740807000 
5   | 58            | 14740807000
6   | 14735113000   | 14735113000 
7   | 1             | 14735113000               
8   | 2             | 14735113000    
9   | 39            | 14735113000    
10  | 4             | 14735113000    
11  | 5             | 14735113000    
12  | 14915146000   | 14915146000
13  | 9             | 14915146000        
14  | 27            | 14915146000
15  | 14915175959   | 14915175959
16  | 3             | 14915175959
17  | 4             | 14915175959    
==================================

我有我认为在 MS Access 中执行此操作的一种巧妙方法,但它无法处理我需要更新的数据文件的大小,这也是我切换到 PostgreSQL 的原因之一。

这是 MS Access 脚本:

Sub UpdateRef()
On Error GoTo ErrorHandler

Dim strSQL, Var1 As String
Dim rs As New ADODB.Recordset

rs.Open "SELECT test.* FROM test", CurrentProject.Connection, adOpenDynamic, adLockOptimistic

rs.MoveFirst
Var1 = rs![Old ref]
rs![New Ref] = Var1
rs.Update

Do Until rs.EOF
  'SysCmd acSysCmdUpdateMeter, n
  If len(rs![Old ref]) > 2 Then
    Var1 = rs![field2]
  End If
rs![New Ref] = Var1
rs.Update
rs.MoveNext
Loop
  rs.Close
ExitSub:
  Set rs = Nothing
  Exit Sub
ErrorHandler:
  Resume ExitSub
End Sub

【问题讨论】:

感谢您的建议 - 我现在已将其包含在内。 Old Ref的数据类型是什么? (因及时响应澄清请求而受到支持 - 干得好!) 【参考方案1】:

您可以使用update 声明来做到这一点:

update test t
set newref = (select tt.oldref from test tt where tt.id = (
  select max(id) from test where id <= t.id and length(oldref) = 11
));

见demo 这个:

select max(id) from test where id <= t.id and length(oldref) = 11

获取包含第 11 位 oldref 的行的 id

【讨论】:

以上是关于你好 刚刚问你的Mysql在pgsql中遇到个问题 可以帮忙看下?的主要内容,如果未能解决你的问题,请参考以下文章

使用py-mysql2pgsql 简单实现mysql数据库迁移到pgsql

你好,问一下你的mysql主从同步中show master stat

面试官之问:知道你的接口“QPS”是多少吗? 怎么办

pgsql添加limit变慢

你好 我想问你下 你使用的telerik 的kendo for ASP.NET MVC是收费的吗

你好,问一下你的mysql主从同步中show master status,结果为空时怎么解决的?