At this time, I’ll show you how to create SQL codes to use conditional statement.
It includes how to use WHERE, like, and % functions.
It would be useful when you want to filter the specific date range and text condition.
Here is the SQL codes with some examples below:
-- WHERE conditional statement
-- AND OR for connecting several conditions
-- GROUP BY for grouping the data
-- WHERE & like '%'
-- code which starts from CC or DD
-- % means that some letters can be included
SELECT *
FROM DB.ADMIN.TABLENAME
WHERE CODE like 'CC%' or CODE like 'DD%'
-- Output by group
SELECT GROUPVAR1, COUNT (*) AS N
FROM DB.ADMIN.TABLENAME
WHERE CODE like 'CC%' or CODE like 'DD%'
GROUP BY GROUPVAR1
-- Connect multiple conditions with AND
SELECT GROUPVAR1, COUNT (*) AS N
FROM DB.ADMIN.TABLENAME
WHERE SEX = 'FEMALE' and CODE like 'CC%' or CODE like 'DD%'
GROUP BY GROUPVAR1
-- Set the date range
-- YMD is a variable composed of YYYY.MM.DD
-- Code to print data between 2008 and 2016
SELECT DISTINCT SEX, COUNT (*) AS COUNT
FROM DB.ADMIN.TABLENAME
WHERE YMD>'2007.12.31' and YMD<'2017.01.01'
GROUP BY SEX