I want to extract transaction where quantity is -1 on very first date of the same transaction. 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.
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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.