简体   繁体   English

如何根据最小日期和其他条件(其中每个事务在SQL中可以具有多个日期)过滤记录?

[英]How to filter records based on minimum date and some other criteria where each transaction can have multiple dates in SQL?

I want to extract transaction where quantity is -1 on very first date of the same transaction. 我想在同一笔交易的第一天提取数量为-1的交易。 Assume that single transaction have multiple rows. 假设单个事务有多行。

We have following table 我们有下表

tXid_ID     Date        Qty
---------------------------------------
1       1/2/2014    -1
2       2/3/2014    1
2       3/3/2014    1
3       4/3/2014    1
4       5/3/2014    -1
4       6/3/2014    1
5       7/3/2014    -1
5       8/3/2014    1
6       9/3/2014    1
7       10/3/2014   1
7       11/3/2014   -1
----------------------------------

All I want to extract only those transactions which do not have quantity -1 on its first entry within same transaction ID. 我只想提取那些在同一交易ID中的第一个条目中没有数量为-1的交易。

Expected results should be like this 预期结果应该是这样的

tXid_ID     Date        Qty
---------------------------------------
2       2/3/2014    1
2       3/3/2014    1
3       4/3/2014    1
6       9/3/2014    1
7       10/3/2014   1
7       11/3/2014   -1
----------------------------------

try like below using subquery 尝试像下面使用子查询

 select a.* from table_name a
 where tax_id not in (  select tax_id from table_name t1
    where t1.date= (select min(date) from 
                table_name t2 where t1.tax_id=t2.tax_id
               ) and  t1.qty=-1
)

this one should work 这个应该工作

select
  *
from
  tbl
where
  tXid_ID in (
    select
      t.tXid_ID
    from
      tbl t
      inner join (
        select
          min(date) as minDate,
          tXid_ID
        from
          tbl
        group by
          tXid_ID
      ) t1 on t1.minDate = t.date
    where
      t.Qty != -1
  )

I think the simplest method is aggregation: 我认为最简单的方法是聚合:

select txid_id
from t
group by txid_id
having min(date) = min(case when qty = -1 then date end);

The having clause simply says that the minimum date is the minimum of the date where qty = -1 . having子句只是说最小日期是qty = -1的最小日期。

暂无
暂无

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

相关问题 选择日期为 where 条件的记录,并根据每个日期对结果进行分组 - select records with dates in where condition and group the results based on each date 当某些记录有月/日/年而其他记录只有年时,如何存储日期? - How to store dates when some records have month/day/year while other records have just the year? MYSQL 如何根据日期和响应的多个标准求和 - MYSQL How can I sum based on multiple criteria by date and by response MySQL根据多个日期返回查询,如果不存在其他记录 - MySQL to return query based on multiple dates and if no other records exist SQL如何按日期和多个条件聚合 - SQL how to aggregate by date and multiple criteria 如何创建WHERE子句,其中SQL查询中有一些必填字段和一些其他可选字段 - How can I create a WHERE clause where there are some mandatory fields and some other optional fields in a SQL query 如何编写 where 子句以动态获取上个月某个日期和当前月份某个日期之间的日期? - How can I write a where clause to get dates between some date in the last month and some date in the current month, dynamically? 如何根据SQL中未出现的记录来过滤记录? - How to filter records based on no of their occurrence in SQL? 如何根据条件过滤 SQL 中的唯一记录? - How to filter unique records in SQL based on conditons? 如何编写基于特定WHERE子句条件从多个表中计数的My(SQL)查询 - How do I write a My(SQL) query that counts from multiple tables based on specific WHERE clause criteria
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM