Skip to content
Advertisement

How to use two where conditions in SQL?

Following is the query I have written and I need to where conditions.

  1. Admin_Level_3_palika is not null
  2. Year = ‘2021’

However, the following query is still giving me null values for Admin_Level_3_palika

SELECT
Admin_Level_3_palika,
COUNT(CASE WHEN Week_number = '21' THEN 1 END) AS count_Week_21,
COUNT(CASE WHEN Week_number  = '22' THEN 1 END) AS count_Week_22,
(COUNT(CASE WHEN Week_number  = '22' THEN 1 END) -
    COUNT(CASE WHEN Week_number  = '21' THEN 1 END)) AS Difference
FROM `interim-data.casedata.Interim Latest` 
where  Admin_Level_3_palika is not null or YEAR = '2021'
GROUP BY
Admin_Level_3_palika
ORDER BY 
count_Week_22 desc limit 20

Please help me with how to work with this. Following is an example of my dataset, Epid_ID being unique for each row.

Admin_Level_3_palika  Week_number  YEAR  Epid_ID
 Lamkichuha MC            21       2020  COV-NEP-PR5-RUP-20-00022
 Lamkichuha MC            21       2021  COV-NEP-PR5-RUP-20-00023

Advertisement

Answer

If these are your conditions:

1. Admin_Level_3_palika is not null
2. Year = '2021' 

Then you need and:

where Admin_Level_3_palika is not null and Year = '2021'

If year is an integer (as I would expect it to be), drop the single quotes. Don’t mix data types in comparisons.

For performance, you might also want to limit the week number:

where Admin_Level_3_palika is not null and
      Year = '2021' and
      week_number in ('21', '22')

And finally, BigQuery offers countif() which I recommend:

SELECT Admin_Level_3_palika,
       COUNTIF(Week_number = '21') AS count_Week_21,
       COUNTIF(Week_number = '22') AS count_Week_22,
       (COUNTIF(Week_number = '22') - COUNTIF(Week_number = '21')) AS Difference
FROM `interim-data.casedata.Interim Latest` 
WHERE Admin_Level_3_palika is not null AND
      YEAR = '2021' AND
      week_number IN ('21', '22')
GROUP BY Admin_Level_3_palika
ORDER BY count_Week_22 desc 
LIMIT 20
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement