简体   繁体   English

如何选择未超过不同列的多个值的记录

[英]How do I select records where multiple values of different columns have not been exceeded

I've been looking for a solution for the below question, sadly to no avail. 我一直在寻找以下问题的解决方案,可惜没有结果。 This is for Microsoft SQL Server Management Studio on Microsoft SQL Server. 这适用于Microsoft SQL Server上的Microsoft SQL Server Management Studio。

The database looks more or less like this: 数据库看起来或多或少像这样:

ID    LegID   TripID     
1     0       0  
1     0       1  
1     1       0  
2     0       0  
2     1       0 
2     2       0 
2     2       1  
3     0       0 
3     0       1 
3     0       2

I'm looking for a result where all records will selected be for the ID's where the legID does not exceed 1 and the TripID does not exceed 1. The result then should be: 我正在寻找一个结果,其中所有记录都将选择为legID不超过1且TripID不超过1的ID。那么结果应为:

ID    LegID   TripID     
1     0       0  
1     0       1  
1     1       0

Could you please help me with this? 你能帮我这个忙吗?

One method use not exists : 一种方法not exists

select t.*
from t
where not exists (select 1
                  from t t2
                  where t2.id = t.id and
                        (t2.LegId > 1 or t2.TripId > 1)
                 );

For performance, you want an index on (id, legid, tripid) . 为了提高性能,您需要在(id, legid, tripid)上建立索引。

You could also use window functions: 您还可以使用窗口功能:

select t.*
from (select t.*,
             max(legid) over (partition by id) as max_legid,
             max(tripid) over (partition by id) as max_tripid
      from t
     ) t
where max_legid <= 1 and max_tripid <= 1;

Use a Derived table (subquery) to get all the id values matching your requirements, utilizing conditional aggregation based filtering. 通过基于条件聚合的过滤,使用派生表(子查询)来获取所有符合您要求的id值。 And, then join back to the main table to get all the rows for those id value(s). 并且,然后返回主表以获取那些id值的所有行。

SELECT 
  t1.* 
FROM 
  your_table_name t1 
JOIN (SELECT ID
      FROM your_table_name 
      GROUP BY ID 
      HAVING MAX(LegID) <= 1 
         AND MAX(TripID) <= 1) t2 ON t2.ID = t1.ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何选择未超过值的记录 - How do I select records where a value has not been exceeded 选择1条记录,其中两个记录在不同的列中具有相同的值 - Select 1 record where two records have the the same values in different columns 如何选择仅在特定列上具有不同值的所有记录? - How to select all the records that have different values only on particular columns? 如何选择多列具有相同值的行 - How to select rows where multiple columns have same values 如何选择不同的列以及它们具有的记录数 - How do I select distinct columns together with the count of records they have SQL 返回列名列表,其中列在组内的多条记录中具有不同的值 - SQL Return a list of column names where the columns have different values across multiple records within a group 在续集中,如何选择与多行的不同值匹配的记录? - In sequelize, how do I select records that match different values of multiple rows? 如何在sql中选择具有多个值的记录? - How to select records that have multiple values in sql? 如何选择不同的行,其中一列可能具有许多相同的值,但所有第二列均具有相同的值? - How do I select distinct rows where a column may have a number of the same values but all their 2nd columns have the same value? Oracle SQL-比较列-选择记录以查找不匹配的值 - Oracle SQL - Compare Columns - SELECT records for values that do not have a match
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM