English 中文(简体)
• 如何过滤带有聚合的几行
原标题:How to filter out certain rows with aggregation
  • 时间:2023-12-11 02:22:53
  •  标签:
  • sql
  • mysql

I am a sql newbie. I have data in a mysql db that looks like

col1. col2. col3. col4. col5.
a b true true c
a d false false e
a b false false c
a f false false g
a b false false c
i b false false j
a d true true e

我谨提出一个问题:

(a) 发现所有有col3和col4的行文均为实(见上文表格第一行和最后一行)

b) 发现所有与(a)行的col1和col2相同(因此第三和第五行的col1和col2与第一行相同,第二行的col1和col2与最后一行相同)

c) 移走(a)和(b) 退还所有其他各行

因此,询问结果将归结

col1. col2. col3. col4. col5.
a f false false g
i b false false j

For a) I could create a sub-table like

选择col1、col2、col3、col4、col5, 摘自表格中, col3=真实和col4=真实

(b) 我可能不得不研究(a)

(c) 从完整表格中减去(b)的结果

是否有更快速的办法与团体合作?

问题回答

您可以使用一个子座确定col3 &col4 = 真实条件,然后利用左边通过col1 &加入该子局;2位你发现所有配对区。 最后,仅通过IS NUL条件过滤未配电网:

SELECT t.*
FROM the_table t
LEFT JOIN (
    SELECT col1, col2
    FROM the_table
    -- cols 3 & 4 = true
    WHERE col3 =  true  AND col4 =  true 
          --  match these rows via cols 1 & 2
    ) t34 ON t.col1 = t34.col1 AND t.col2 = t34.col2
-- now return only the unmatched rows
WHERE t34.col1 IS NULL

页: 1 注

SELECT * FROM YOUR_TABLE T
WHERE NOT EXISTS 
 (SELECT 1 FROM YOUR_TABLE TT
  WHERE TT.COL1 = T.COL1 AND TT.COL2 = T.COL2
    AND TT.COL3 =  true  AND TT.COL4 =  true );

是的,汇总可以帮助这里。 你们想要找到没有col3=true/col4=true row的Col1/col2集团。

MAX(col3 and col4) OVER (PARTITION BY col1, col2)

给你1个col-1col2集团,这个集团有col3=true/col4=true row,而其他集团则有零,因为在MySQL真实=1,虚假=0。 因此,(<bool expression>>,如果至少一行与情况不符,则将给你1。

select col1, col2, col3, col4, col5
from
(
  select t.*, max(col3 and col4) over (partition by col1, col2) as flag
  from mytable t
) checked
where flag = 0;

必须在分局进行这项工作,因为窗口功能(% over><>>>>>>>>上下限。 目前有<代码>的房舍管理处。 质量/代码 在窗户按照执行命令履行功能之后,这方面的条款没有体现这一条款。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...