GVKun编程网logo

PostgreSQL-选择条件成立的distinct(column1,column2)(select 条件)

22

关于PostgreSQL-选择条件成立的distinct和column1,column2的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于Ag-gridcolumnApiapplyColum

关于PostgreSQL-选择条件成立的distinctcolumn1,column2的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于Ag-grid columnApi applyColumnState函数、Column length too big for column ''Flist'' (max = 21845);、COUNT(DISTINCT column_name)SQL Server 2008中的差异与COUNT(column_name)的差异?、df['column1'] != df['column2'] 出现匹配的数字,为什么?等相关知识的信息别忘了在本站进行查找喔。

本文目录一览:

PostgreSQL-选择条件成立的distinct(column1,column2)(select 条件)

PostgreSQL-选择条件成立的distinct(column1,column2)(select 条件)

我有下表和一些示例记录:

  id  | attr1_id | attr2_id |      user_id      | rating_id |------+----------+----------+-------------------+-----------+ 1    |      188 |      201 | user_1@domain.com |         3 | 2    |      193 |      201 | user_2@domain.com |         2 | 3    |      193 |      201 | user_2@domain.com |         1 | 4    |      194 |      201 | user_2@domain.com |         1 | 5    |      194 |      201 | user_1@domain.com |         1 | 6    |      192 |      201 | user_2@domain.com |         1 |

的组合(attr1_idattr2_iduser_id)的UNIQUE,这意味着每个用户只能创建与特定的一对属性id的一个记录。

我的目标是选择(attr1_id,attr2_id)的所有不同组合rating_id =1,但是,仅选择和的每个组合,attr1_id并且仅选择attr2_id一次,并且仅在不存在(由其他用户使用)具有rating_id >1并引用相同attr1_id和的任何其他行的情况下。attr2_id。请注意,组合attr1_idattr2_id可切换左右,所以给这两个记录:

  id  | attr1_id | attr2_id |      user_id       | rating_id | override_comment------+----------+----------+--------------------+-----------+------------------  20  |       5  |       2  | user_1@domain.com  |         3 |------+----------+----------+--------------------+-----------+------------------  21  |       2  |       5  | user_2@domain.com  |         1 |

不应计算任何行,因为这些行指的是的相同组合,attr_ids其中之一具有rating_id > 1

但是,如果存在这两行:

  id  | attr1_id | attr2_id |      user_id       | rating_id | override_comment------+----------+----------+--------------------+-----------+------------------  20  |       5  |       2  | user_1@domain.com  |         1 |------+----------+----------+--------------------+-----------+------------------  21  |       2  |       5  | user_2@domain.com  |         1 |------+----------+----------+--------------------+-----------+------------------  22  |       2  |       5  | user_3@domain.com  |         1 |

所有行应该只能算作一个,因为他们都有着相同的组合attr1_id,并attr2_id和所有有rating_id = 1

另外,我将省略一些联接表列的联接和过滤,但是我还是想提到它。

SQL Fiddle现在对我不起作用,但是我已经
从兼容性表中上传了一些示例数据。

到目前为止,我的查询是这样的:

SELECT distinct(a1, a2),       a1,       a2FROM  ( SELECT c.*,           least(attr1_id, attr2_id) AS a1,           greatest(attr1_id, attr2_id) AS a2   FROM compatibility c   JOIN attribute a ON c.attr1_id = a.id   JOIN PARAMETER pa ON a.parameter_id = pa.id   JOIN problem p ON pa.problem_id = p.id   WHERE p.id = 1   GROUP BY 1,            2 HAVING NOT bool_or(rating_id > 1)) s;

在样本中,总共有144个评分。每个用户都创建了7个具有的rating_id >1评分,在这14个评分中,有2个引用了同一组(attr1_idattr2_id)。因此,我要寻找的号码将是(77-12) =65。但是,这里的结果似乎是77-2 = 75。因此,只有存在两个具有相同属性ID的等级的行才被丢弃。

我还要指出我先前关于这个问题的问题,要求我重新开始一个问题。

答案1

我认为这符合您的描述:

select least(attr1_id, attr2_id) as attr1, greatest(attr1_id, attr2_id) as attr2from table tgroup by least(attr1_id, attr2_id), greatest(attr1_id, attr2_id) having bool_and(rating_d = 1) ;

我不理解查询中的其他表,因为您从拥有所需所有内容的单个表开始。

Ag-grid columnApi applyColumnState函数

Ag-grid columnApi applyColumnState函数

检查您的AgGrid版本。 ColumnApi.applyColumnState()是在版本24.0.0中添加的。

,

如果您坚持使用较低的 ag-grid 版本 (gridOptions.columnApi.setColumnState(prevState)

使用 ag-grid 24.0.0 及以上版本,您可以使用 -

gridOptions.columnApi.applyColumnState({
         state:prevState,applyOrder: true,});

Column length too big for column ''Flist'' (max = 21845);

Column length too big for column ''Flist'' (max = 21845);

建表语句报如下错误:


CREATE TABLE test_1 (  
                        Fid bigint(20) unsigned NOT NULL,  
                        Ftype tinyint(4) unsigned NOT NULL,  
                        Flist varchar(65532) DEFAULT NULL,  
                        Fstatus tinyint(3) unsigned DEFAULT ''0'',  
                        Ftime bigint unsigned DEFAULT ''0'',  
                        Faddtime bigint unsigned DEFAULT ''0'',  
                        PRIMARY KEY (Fid,Ftype)  
                    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;  
建表报如下错误:  
ERROR 1074 (42000): Column length too big for column ''Flist'' (max = 21845);         use BLOB or TEXT instead  

原因分析

虽然知道mysql建表的时候列长度有65535的限制,但是一直没有时间整理这个问题,接着今天整理一下。

ERROR 1074 (42000): Column length too big for column ''Flist'' (max = 21845); use BLOB or TEXT instead
从上面的报错我们知道Flist列指定值不能大于21845 字节(mysql官方手册中定义,创建表的字段长度限制为65535 bytes,这个是指所有列指定的长度和,当然不包括TEXT和BLOB类型的字段)。
还有一点我们需要注意的是我们定义列长度时指定的长度单位为字符,上面提到的65535限制的单位为字节。不同字符集下每个字符占用的字节数不同,utf8下每个字符占用3个字节(65535/3=21845),gbk下每个字符占用2个字节(65535/2=32767),latin1字符集下一个字符占用一个字节。

操作验证

<span>实验1:  
CREATE TABLE test_1 (  
                        Flist varchar(21845) DEFAULT NULL  
                    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;  
ERROR 1118 (42000): Row size too large. The maximum row size for the used            
table type, not counting BLOBs, is 65535. This includes storage overhead,
 check the manual. You have to change some columns to TEXT or BLOBs  
</span><span>##我们发现给表指定一个列,
列长度为21845字符(utf8下最大长度限制),但是建表依然报错。这是因为还有别的一些开销,
所以我们不能指定列长度为最大限制21845(测试发现指定长度为21844后建表成功)</span>


<span>实验2:  
CREATE TABLE test_1 (  
                        Fid bigint(20) unsigned NOT NULL,  
                        Ftype tinyint(4) unsigned NOT NULL,  
                        Flist varchar(21844) DEFAULT NULL,  
                        Fstatus tinyint(3) unsigned DEFAULT ''0'',  
                        Ftime bigint unsigned DEFAULT ''0'',  
                        Faddtime bigint unsigned DEFAULT ''0'',  
                        PRIMARY KEY (Fid,Ftype)  
                    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;  
报如下错误:  
ERROR 1118 (42000): Row size too large. The maximum row size for the used 
table type, not counting BLOBs, is 65535. This includes storage overhead,
 check the manual. You have to change some columns to TEXT or BLOBs  
 
</span><span>##虽然Flist 长度指定为21844,但是因为还
有其他非TEXT和BLOB字段存在,所以报错。这是因为65535长度限制是针对表中所有列的长
度和的最大限制,如果列的长度和超过该值,建表依然会报错。(除了TEXT和BLOB类型的字
段的)</span>    

COUNT(DISTINCT column_name)SQL Server 2008中的差异与COUNT(column_name)的差异?

COUNT(DISTINCT column_name)SQL Server 2008中的差异与COUNT(column_name)的差异?

我遇到一个让我疯狂的问题.
当运行下面的查询时,我得到一个233,769的计数
SELECT COUNT(distinct  Member_List_Link.UserID)  
 FROM Member_List_Link  with (nolock)   
 INNER JOIN MasterMembers with (nolock)  
     ON Member_List_Link.UserID = MasterMembers.UserID   
  WHERE MasterMembers.Active = 1 And
        Member_List_Link.GroupID = 5 AND 
        MasterMembers.ValidUsers = 1 AND 
        Member_List_Link.Status = 1

但是如果我运行相同的查询而没有distinct关键字,748的计数

SELECT COUNT(Member_List_Link.UserID)  
 FROM Member_List_Link  with (nolock)   
 INNER JOIN MasterMembers with (nolock)
   ON Member_List_Link.UserID = MasterMembers.UserID   
 WHERE MasterMembers.Active = 1 And Member_List_Link.GroupID = 5 
  AND MasterMembers.ValidUsers = 1 AND Member_List_Link.Status = 1

要测试,我重新创建了所有的表,并将它们放入临时表并再次运行查询:

SELECT COUNT(distinct  #Temp_Member_List_Link.UserID)  
  FROM #Temp_Member_List_Link  with (nolock)   
  INNER JOIN #Temp_MasterMembers with (nolock)
    ON #Temp_Member_List_Link.UserID = #Temp_MasterMembers.UserID   
  WHERE #Temp_MasterMembers.Active = 1 And 
        #Temp_Member_List_Link.GroupID = 5 AND 
        #Temp_MasterMembers.ValidUsers = 1 AND 
        #Temp_Member_List_Link.Status = 1

而没有distinct关键字

SELECT COUNT(#Temp_Member_List_Link.UserID)  
  FROM #Temp_Member_List_Link  with (nolock)   
  INNER JOIN #Temp_MasterMembers with (nolock)
    ON #Temp_Member_List_Link.UserID = #Temp_MasterMembers.UserID   
  WHERE #Temp_MasterMembers.Active = 1 And 
        #Temp_Member_List_Link.GroupID = 5 AND 
        #Temp_MasterMembers.ValidUsers = 1 AND 
        #Temp_Member_List_Link.Status = 1

在旁注,我通过简单地运行(select * from Member_List_Link到#temp …)重新创建了临时表

现在,当我检查这些temp表之间的COUNT(列)与COUNT(不同的列)之间的差异时,我看不到任何的!

那么为什么原始表格有差异?

我正在运行sql Server 2008(开发版).

更新 – 包括统计资料

PhysicalOp列仅用于第一个查询(不区分)

NULL
Compute Scalar
Stream Aggregate
Clustered Index Seek

PhysicalOp列仅用于第一个查询(具有不同)

NULL
Compute Scalar
Stream Aggregate
Parallelism
Stream Aggregate
Hash Match
Hash Match
Bitmap
Parallelism
Index Seek
Parallelism
Clustered Index Scan

第一个查询的行和执行(不区分)

1   1
0   0
1   1
1   1

第二个查询的行和执行(不同)

Rows    Executes
1   1
0   0
1   1
16  1
16  16
233767  16
233767  16
281901  16
281901  16
281901  16
234787  16
234787  16

将OPTION(MAXDOP 1)添加到第二个查询(具有不同的)

Rows Executes

1           1
0           0
1           1
233767          1
233767          1
281901          1
548396          1

并产生PhysicalOp

NULL
Compute Scalar
Stream Aggregate
Hash Match
Hash Match
Index Seek
Clustered Index Scan

解决方法

从 http://msdn.microsoft.com/en-us/library/ms187373.aspx
NOLOCK相当于READUNCOMMITTED.有关更多信息,请参阅本主题后面的READUNCOMMITTED.

READUNCOMMITED将读取行两次,如果它们是转换的主题 – 因为当事务处于IN进程时,滚动后退和回滚行均存在于数据库中.

默认情况下,所有查询都被读取,不包括未提交的行

当您插入临时表时,选择将仅给出已提交的行 – 我相信这将涵盖您尝试解释的所有症状

df['column1'] != df['column2'] 出现匹配的数字,为什么?

df['column1'] != df['column2'] 出现匹配的数字,为什么?

如何解决df[''column1''] != df[''column2''] 出现匹配的数字,为什么??

所以我试图将重新计算的字段与熊猫表中的现有字段进行比较,以识别差异。当我查找不一致的值时,它返回看起来一致的值。

为什么会这样?

enter image description here

更新数据类型

enter image description here

请注意,我可以通过将舍入应用于我的计算来修复它,但是如果有人知道为什么会发生这种情况,我仍然很感兴趣。绝对不是我所期望的。另请注意,我的大约一半行确实匹配并且没有显示在结果中。

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

今天关于PostgreSQL-选择条件成立的distinctcolumn1,column2的讲解已经结束,谢谢您的阅读,如果想了解更多关于Ag-grid columnApi applyColumnState函数、Column length too big for column ''Flist'' (max = 21845);、COUNT(DISTINCT column_name)SQL Server 2008中的差异与COUNT(column_name)的差异?、df['column1'] != df['column2'] 出现匹配的数字,为什么?的相关知识,请在本站搜索。

本文标签: