NAS_2339
NAS_2339

Reputation: 353

How to select 1 column value based on a condition in SQL

I have a table

Date trial value
2020-12-10 false 100
2020-12-10 true 200
2020-12-11 false 150
2020-12-11 true 350

I want result

Date total value1
2020-12-10 300 100
2020-12-11 500 150

Where value1 is the value when trial=false. what is the SQL query to do this? Thanks in advance

Upvotes: 0

Views: 1771

Answers (3)

Durgesh Singh
Durgesh Singh

Reputation: 29

You can use the case statement in select clause:

SELECT
..., case when ColumnA > x then ColumnA else ColumnB end as C from ...

for more details visit : http://msdn.microsoft.com/en-us/library/ms181765.aspx

Upvotes: 0

GMB
GMB

Reputation: 222402

You can use cnoditional aggregation:

select date, sum(value) as total,
    sum(case when trial = 'false' then value else 0 end) as value1
from mytable
group by date

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1269443

You can use conditional aggregation:

select date, sum(value),
       sum(case when trial = 'false' then value end) as value1
from t
group by date;

Note: If your database support boolean values and trial is boolean, then:

       sum(case when trial then value end) as value1

Upvotes: 1

Related Questions