How do you ignore records not inside the case statement when using CASE/WHEN/THEN?
For example, this statement will update three matching records as expected but will make all student records that do not match a WHEN/THEN clause to NULL
UPDATE table SET student = (CASE WHEN student = '10' THEN '100412'
WHEN student = '17' THEN '100295'
WHEN student = '26' THEN '100981'
END)
WHERE year = '2019';
How can you skip over records not inside the CASE statement and only change records that have a matching clause?
For skipping records not in case statement, you can use something like this
UPDATE table SET student = (CASE WHEN student = '10' THEN '100412'
WHEN student = '17' THEN '100295'
WHEN student = '26' THEN '100981'
END)
WHERE year = '2019' AND student IN ('10','17','26');
There a two solutions I think :
Add a where clause
UPDATE table SET student = (CASE WHEN student = '10' THEN '100412'
WHEN student = '17' THEN '100295'
WHEN student = '26' THEN '100981'
END)
WHERE year = '2019' AND student IN ('10','17','26');
Use else statement but that will scan the whole table for nothing :
UPDATE table SET student = (CASE WHEN student = '10' THEN '100412'
WHEN student = '17' THEN '100295'
WHEN student = '26' THEN '100981'
ELSE student
END)
WHERE year = '2019';
You could use a default case for that:
UPDATE table SET student = (CASE WHEN student = '10' THEN '100412'
WHEN student = '17' THEN '100295'
WHEN student = '26' THEN '100981'
ELSE student
END)
WHERE year = '2019';
Otherwise, if you want to minimize the load, just add all known student values to the WHERE clause. This triggers an update only on those rows that are really affected by a change
You can use in or = operator
WHERE year = '2019' AND (student = '10' or student = '17' or student ='26');
I have in my table one row with a char value. When the value is NULL then a false should be outputted. If the value is not NULL then a true should be outputted.
So when I try to set user_group.tUser to 0 or 1 then I'm getting this error:
Invalid column name 'false'.
Invalid column name 'true'.
SELECT COALESCE((SELECT name
FROM v_company
WHERE companyId = userView.companyId), ' ') AS company,
userView.value AS companyUser,
userView.display AS displayedUser,
CASE
WHEN user_group.tUser IS NULL THEN 0
ELSE 1
END AS userIsMemberOfGroup
FROM v_user userView
LEFT OUTER JOIN cr_user_group user_group
ON ( user_group.group = 'Administrators'
AND user_group.tUser = userView.value )
ORDER BY company ASC,
displayedUser ASC
I think this is the logic you want:
SELECT COALESCE(v.name, ' ') as company,
u.value as companyUser, u.display as displayedUser,
(EXISTS (SELECT 1
FROM cr_user_group ug
WHERE ug.group = 'Administrators' AND
ug.tUser = uv.value
)
) as userIsMemberOfGroup
FROM v_user u LEFT JOIN
v_company c
ON c.companyId = v.companyId
ORDER BY company ASC, displayedUser ASC ;
In general, MySQL is very flexible about going between booleans and numbers, with 0 for false and 1 for true.
You can use MySQL IF function to return 'false' when name IS NULL, else 'true':
SELECT IF(name IS NULL, 'false', 'true')
FROM table;
A simple CASE expression would work here:
SELECT
name,
CASE WHEN name IS NOT NULL THEN true ELSE false END AS name_out
FROM yourTable;
We could also shorten the above a bit using IF:
IF(name IS NOT NULL, true, false)
SELECT
CASE
WHEN name IS NULL THEN 'false'
ELSE 'true'
END
FROM
table1;
I have this query that worked fine:
select isnull(email,'') as Email ,isnull([ERPM First Name],'')+' '+isnull([ERPM Last Name],'')[User Name],
geo,CustomerID,BusinessID,courseid, MIN (CompletionDate) [1st Training Course],
CASE WHEN COURSEID IN (37445,37644,37443,37778,37435,37733,37584,37483,37392,37817,
37259,37597,37391,37393,37792,37816,37256,37257,37258,37484,37485,37486)
THEN 'Yes' ELSE 'No'
END AS [Is it a campaing course?],
CASE WHEN CompletionDate BETWEEN '2017-03-10' AND '2017-09-03' THEN 'Yes' ELSE 'No'
END AS [During Campaign],
CASE WHEN COURSEID IN (37256,37257,37258,37484,37485,37486) AND
CompletionDate BETWEEN '2017-03-10' AND '2017-09-03' THEN 'ON Period Bonus' ELSE '-'
END AS [1st BONUS]
from vw_Training_Cube
where [Is disti or subdisti?] = 'No' and [Is test account?] = 'No'
and Email<>'0'
GROUP BY isnull(email,''),isnull([ERPM First Name],'')+' '+isnull([ERPM Last Name],''),geo,CustomerID,BusinessID,courseid,
CASE WHEN COURSEID IN (37445,37644,37443,37778,37435,37733,37584,37483,37392,
37817,37259,37597,37391,37393,37792,37816,37256,37257,37258,37484,37485,37486)
THEN 'Yes' ELSE 'No'
END,
CASE WHEN CompletionDate BETWEEN '2017-03-10' AND '2017-09-03' THEN 'Yes' ELSE 'No'
END,
CASE WHEN COURSEID IN (37256,37257,37258,37484,37485,37486) AND
CompletionDate BETWEEN '2017-03-10' AND '2017-09-03' THEN 'ON Period Bonus' ELSE '-5'
END
but now instead of grouping by email, I want to group by business id. But simply swapping the order doesnt solve the problem.
Unless you need an aggregate function such as COUNT() MIN() or MAX() then you can simplify your query by using select distinct.
SELECT DISTINCT
ISNULL(email, '')
AS Email
, ISNULL([ERPM First Name], '') + ' ' + ISNULL([ERPM Last Name], '')
[User Name]
, geo
, CustomerID
, BusinessID
, courseid
, MIN(CompletionDate) [1st Training Course]
, CASE
WHEN COURSEID IN (37445, 37644, 37443, 37778, 37435, 37733, 37584, 37483, 37392, 37817,
37259, 37597, 37391, 37393, 37792, 37816, 37256, 37257, 37258, 37484, 37485, 37486) THEN
'Yes'
ELSE
'No'
END AS [Is it a campaing course?]
, CASE
WHEN CompletionDate BETWEEN '2017-03-10' AND '2017-09-03' THEN
'Yes'
ELSE
'No'
END AS [During Campaign]
, CASE
WHEN COURSEID IN (37256, 37257, 37258, 37484, 37485, 37486) AND
CompletionDate BETWEEN '2017-03-10' AND '2017-09-03' THEN
'ON Period Bonus'
ELSE
'-'
END AS [1st BONUS]
FROM vw_Training_Cube
WHERE [Is disti or subdisti?] = 'No'
AND [Is test account?] = 'No'
AND Email <> '0'
ORDER BY BusinessID
To reduce the rows further, you also need to remove columns - OR - start using aggregate functions. e.g. the following would produce the minimum set of rows to list every BusinessID that meets the where conditions.
SELECT DISTINCT
BusinessID
FROM vw_Training_Cube
WHERE [Is disti or subdisti?] = 'No'
AND [Is test account?] = 'No'
AND Email <> '0'
ORDER BY BusinessID
;
Keep adding columns to that to see the effect on number of rows.
What am I doing wrong? It needs to display yes if more people are single than married and no if viceversa. I just want it to display yes or no only.
IF
select COUNT(StudMaritalStatus) from students WHERE StudMaritalStatus = "M"
<
select COUNT(StudMaritalStatus) from students WHERE StudMaritalStatus = "S"
Print 'Yes'
ELSE
Print 'No';
You want something like this:
select case when married >= single then 'M' else 'S' end
from
(
select sum(case when StudMaritalStatus = 'M' then 1 else 0 end) married
, sum (case when StudMaritalStatus = 'S' then 1 else 0 end) single
from students
) derivedTable
Tie breaking depends on your business requirements, which none of us know.
well considering that MySQL uses:
IF expression THEN
expression
ELSE
ENDIF;
you would be better of not doing it with a select statement
DECLARE married int, single int
SET married = select COUNT(StudMaritalStatus) from students WHERE StudMaritalStatus = 'M'
SET single = select COUNT(StudMaritalStatus) from students WHERE StudMaritalStatus = 'S'
IF married < single THEN
PRINT 'YES'
ELSE
PRINT 'NO'
END IF
In answer yes your syntax was wrong
https://dev.mysql.com/doc/refman/5.7/en/if.html
I'm looking to generate a result or create a view that is based on calculating the percentage of one query/another query.
These are the queries:
Query1:
SELECT
count(distinct email), Utility
from elec_cust
where email != 'NULL' and MAILLIST = '1'
GROUP BY Utility
ORDER BY count(distinct email)
And
Query2:
SELECT
count(distinct email), Utility
from elec_cust
where email != 'NULL' and MAILLIST = '0'
GROUP BY Utility
ORDER BY count(distinct email)
I'm looking to generate the result of Query2/Query1 as a percentage:
Utility | Percentage
You can do it with conditional aggregation:
SELECT COUNT(DISTINCT CASE WHEN MAILLIST = '1' THEN email END) as Query_1_result,
COUNT(DISTINCT CASE WHEN MAILLIST = '0' THEN email END) as Query_2_result,
COUNT(DISTINCT CASE WHEN MAILLIST = '1' THEN email END)/COUNT(DISTINCT CASE WHEN MAILLIST = '0' THEN email END)*100 AS Perc_Col ,
Utility
from elec_cust
where email != 'NULL'
GROUP BY Utility
MySQL treats Boolean expressions like 1 for true, and 0 for false. So this will sum the MAILLIST = '1' and will divide it with MAILLIST = '0'
You can remove the first two columns, I've added them for your comfort.