I am trying to figure out which ids only have one type of transaction.
I have tried joining and selecting distinct but I am doing it wrong
select transactions.type, id
from datasource
group by id, transactions.type
This gives me a table with two transaction types: either dep or withdraw. Most IDs have two rows, one being dep and the other withdraw. I want to select only the ids that have only the withdraw transaction type
use where condition and not exists
select transactions.type, id from datasource t1
where type='withdraw'
and not exists( select 1 from datasource t2 where t1.id=t2.id
and type='dep')
You can use group by
with having count(*)=1
clause
select id
from datasource
where transactions_type in
(
select transactions_type, id
from datasource
group by transactions_type
having count(*)=1 )
Since "You are trying to figure out which ids only have one type of transaction"
If you specifically look for transactions_type = 'withdraw'
, then add and transactions_type = 'withdraw'
to the end of the above Select Statement.
PS I suppose there's a type for transactions.type
, and that should be transactions_type
, isn't that?
I think aggregation does what you want. However, I am confused what your data structure is. A join
is needed somewhere:
select ds.id, min(t.type)
from datasource ds join
transactions t
on ds.? = t.? -- what is the join key?
group by ds.id
having min(t.type) = max(t.type);
If transactions.type
is actually a column in datasource
, then:
select ds.id, min("transactions.type")
from datasource ds
group by ds.id
having min("transactions.type") = max("transactions.type");
Similar to other answers, but simpler:
select id, count(distinct transactions.type)
from datasource
group by id
having count(distinct transactions.type) = 1
It is unclear, however, what transactions.type is. It doesn't appear to be valid as a column name. Or did you mean to write transactions_type
?
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.