2

I'm trying to exclude rows where all gross columns have blank values.

Sample data:

market   item_id    gross_1     gross_2   gross_3   period
POLAND   1111       1           2         3         20190301
ESTONIA  2222       blank       2         3         20190302
POLAND   3333       1           blank     3         20190303
POLAND   3333       1           blank     blank     20190304
POLAND   4444       blank       blank     blank     20190305
POLAND   5555       1           2         3         20190306`

I'd like to receive:

market   item_id    gross_1     gross_2   gross_3   period
POLAND   1111       1           2         3         20190301
ESTONIA  2222       blank       2         3         20190302
POLAND   3333       1           blank     3         20190303
POLAND   3333       1           blank     blank     20190304
POLAND   5555       1           2         3         20190306`

I've found something like this, but it doesn't work in snowflake:

WHERE NOT ROW(gross_1, gross_2, gross_3) IS NULL
1
  • 1
    What does "blank" mean? Commented Sep 20, 2019 at 12:48

2 Answers 2

5

Use coalesce():

select * from tablename
where coalesce(gross_1, gross_2, gross_3) is not null
Sign up to request clarification or add additional context in comments.

2 Comments

Elegant, if he means NULL. +1 Wrapping each element in a NULLIF for an empty string or the string 'blank' would work with the COALESCE, too. Nice one.
All 3 columns contain integers so I guess it's about nulls.
2

It depends what "blank" means. As shown in the data:

select t.*
from t
where not (gross_1 = 'blank' and gross_2 = 'blank' and gross_3  = 'blank')

If "blank" means empty string, then:

select t.*
from t
where not (gross_1 = '' and gross_2 = '' and gross_3 = '')

If it means NULL, then:

select t.*
from t
where not (gross_1 is null and gross_2 is null and gross_3 is null)

All these can be rephrased with or:

select t.*
from t
where gross_1 is not null or gross_2 is not null or gross_3 is not null

2 Comments

He wants to exclude them rather than include them, but yeah, this is how it's done.
@dvo . . . I left out the not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.