I have below query:
SELECT users_name, first_name, last_name,
(CASE
WHEN flag1==1 AND flag2==1 AND flag3==0 THEN 'Active'
WHEN flag1==0 AND flag2==1 AND flag3==0 THEN 'Deactive'
WHEN flag1==0 AND flag2==0 AND flag3==0 THEN 'Disabled'
ELSE ''
END) as Status FROM `users`
I have multiple flag conditions before defining it's status but on running the above query it gives me.
Syntax error near==1
What's the correct way to achieve it?
Intended Result:
username|firstname|lastname|status
Alex | Alex | mark | Active
April | April | mark | Deactive
In SQL the comparision operator is = so use = (and not == ):
SELECT users_name, first_name, last_name,
(CASE
WHEN flag1 = 1 AND flag2 = 1 AND flag3 = 0 THEN 'Active'
WHEN flag1 = 0 AND flag2 = 1 AND flag3 = 0 THEN 'Deactive'
WHEN flag1 = 0 AND flag2 = 0 AND flag3 = 0 THEN 'Disabled'
ELSE ''
END) as Status FROM `users`
Related
I have a table as follows
table_user
id name status flag country
====================================================================================================
1 AB 1 0 US
2 BC 1 0 UK
3 CD 0 0 IN
4 DE 3 0 BR
5 EF 3 0 UK
6 FG 2 0 IN
7 GH 4 0 IN
I want to count the no. of records where
status = 1 as totalactiverecords,
status = 0 as totalinactiverecords,
status = 3 as totalrecentactiverecords,
status = 2 as totalemailnotverifiedrecords,
status = 4 as totalemailverifiedrecords,
, and country is UK, all in a single SELECT statement.
Is there any specific way to do it?
I have thought of something like
SELECT COUNT(*) as totalactiverecords WHERE status=1
COUNT(*) as totalinactiverecordsWHERE status=0,
COUNT(*) as totalemailnotverifiedrecords WHERE status=2,
COUNT(*) as totalrecentactiverecords WHERE status=3,
COUNT(*) as totalemailverifiedrecords WHERE status=4
FROM table_user where country=IN
,but that does not seem to work.
You can try this query:
SELECT count(case status when '1' then 1 else null end) as totalactiverecords,
count(case status when '0' then 1 else null end) as totalinactiverecords,
count(case status when '2' then 1 else null end) as totalemailnotverifiedrecords,
count(case status when '3' then 1 else null end) as totalrecentactiverecords,
count(case status when '4' then 1 else null end) as totalemailverifiedrecords
FROM table_user where country='IN'
I usually use CASE WHEN in this kind of problem:
SELECT sum(case when status = 1 then 1 end) as totalactiverecords,
COUNT sum(case when status = 0 then 1 end) as totalinactiverecords,
COUNT sum(case when status = 2 then 1 end) as totalemailnotverifiedrecord,
COUNT sum(case when status = 3 then 1 end) as totalrecentactiverecords,
COUNT sum(case when status = 4 then 1 end) as totalemailverifiedrecords
FROM table_user where country=IN
I have 2 different queries but uses same date column group. Is there any way to use them in one sql query?
status = 1 means certain
status = 0 means optional
1. Query = 'SELECT book_day, sum(pax) as pax, count(*) FROM rezervations WHERE book_day BETWEEN :first_day AND :end_day AND status = 1 GROUP BY book_day';
2. Query = 'SELECT book_day, sum(pax) as pax, count(*) FROM rezervations WHERE book_day BETWEEN :first_day AND :end_day AND status = 0 GROUP BY book_day';
i would like to have result like
'2020-08-15' | 50 pax status 1(certain) | 20 pax status 0 (optional)
'2020-08-18' | 10 pax status 1(certain) | 5 pax status 0 (optional)
.....
...
..
.
Thanks in advance.
Case when is answered my question. Here is the query:
select
book_day,
sum(case when status = 1 then pax else 0 end) as certain,
sum(case when status = 0 then pax else 0 end) as optional
from
rezervations
group by
book_day
And result is:
(`book_day`, `certain`, `optional`)
('2020-07-26', '15', '0'),
('2020-07-29', '20', '7'),
('2020-08-15', '15', '0'),
('2020-08-27', '20', '28'),
('2020-08-28', '0', '22');
I have the following tables Job description and Candidate.
Each job description can be related to n candidates. The candidates are obtained from different sources(a, b, c d) - candidate.source.
I want a single query to list me the JD ids, with count of candidates for each source for each JD, as shown below:
JD id | candidate name | count of candidates - source a | count of candidates - source b | count of candidates - source | c..............
Try using the query as a template:
select
JobDescriptionName,
SUM(ACount) CountOfCandidatesOfA ,
SUM(BCount) CountOfCandidatesOfB,
SUM(CCount) CountOfCandidatesOfC ,
SUM(DCount) CountOfCandidatesOfD
from
( select JobDescriptionID, (CASE WHEN Source = 'a' THEN 1 ELSE 0 END) AS ACount,
(CASE WHEN Source = 'b' THEN 1 ELSE 0 END) AS BCount,
(CASE WHEN Source = 'c' THEN 1 ELSE 0 END) AS CCount,
(CASE WHEN Source = 'd' THEN 1 ELSE 0 END) AS DCount
from Candidate) AS DerivedCandidate
inner join JobDescription ON JobDescription.JobDescriptionID = DerivedCandidate.JobDescriptionID group by JobDescriptionID;
I know there is already an accepted answer but in the effort of learning myself more about SQL here is an alternative way applying the case directly in the initial select without the sub-select:
Select
jd.id,
jd.name,
sum(case when c.source = 'a' then 1 else 0 end) as sourceA,
sum(case when c.source = 'b' then 1 else 0 end) as sourceB,
sum(case when c.source = 'c' then 1 else 0 end) as sourceC,
sum(case when c.source = 'd' then 1 else 0 end) as sourceD
from JobDescription as jd
join Candidate as c on c.jobId = jd.id
group by jd.id, jd.name
SQL Fiddle Demo
SELECT JD id, COUNT(Candidate), source
FROM table1
GROUP BY JD id,source
I have an EMP table in SQL Server that looks something like this
ID T_Date Jid Emp_Cost Con_Cost IsActive
--------------------------------------------------------
13178 null 214 0 0 0
12797 null 214 0 55 1
11906 null 214 0 55 1
12916 null 214 0 58 1
I am executing the below query
SELECT
AVG(CASE WHEN IsActive = 1 THEN Con_Cost ELSE Emp_Cost END) cost
FROM
EMP
WHERE
Jid = 214
AND (T_Date IS NULL OR T_Date >= sysdate);
My expected cost value must be 56.
Can anybody help me with this?
Arun
Since aggregate functions ignore null values, you can use nullif() to change 0 to null like so:
select avg(nullif(case
when IsActive = 1 then Con_Cost
else Emp_Cost
end
,0)) as cost
from EMP
where Jid = 214
and (T_Date is null or T_Date >= sysdatetime());
rextester demo: http://rextester.com/NPBOP19522
returns
+------+
| cost |
+------+
| 56 |
+------+
note: I changed sysdate to sysdatetime(), though I do not know if that is the intent of your query.
AVG actually works fine, since it does an average of the sum of 0+55+55+58 divided by the number of rows in the result set.
I believe what you are looking for is:
select distinct
sum(case when IsActive = 1 then con_cost else emp_cost end) over () /
count(case when IsActive = 1 then 1 else null end) over ()
from emp
where Jid = 214
and (T_Date is null or T_Date >= sysdatetime());
or maybe are you looking for:
select avg(con_cost)
from emp
where isActive = 1
and Jid = 214
and (T_Date is null or T_Date >= sysdatetime());
This can work too. http://rextester.com/QIXA57971
SELECT
AVG(CASE WHEN IsActive = 1 THEN Con_Cost ELSE Emp_Cost END) cost
FROM
EMP
WHERE
Jid = 214
AND (T_Date IS NULL OR T_Date >= GETDATE()) AND IsActive = 1;
Result
cost
-----
56
How do I get the Total value of Yes, No, Other fields of each username?
I like to add Total field.
SELECT Username,
SUM(CASE WHEN type = 'Yes' THEN 1 ELSE NULL END) as Yes,
SUM(CASE WHEN type = 'No' THEN 1 ELSE NULL END) as No,
SUM(CASE WHEN type = '' THEN 1 ELSE NULL END) as Other
//How to get total of Yes/No/Other
FROM table
WHERE source = 'CompanyName' ";
Also the highest Total goes at the top order.
use 0 instead of NULL, add the missing group by and use COUNT(*) to get the total of each group and order the result:
SELECT Username,
SUM(CASE WHEN type = 'Yes' THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN type = 'No' THEN 1 ELSE 0 END) as No,
SUM(CASE WHEN type = '' THEN 1 ELSE 0 END) as Other,
COUNT(*) as TOTAL
FROM table
WHERE source = 'CompanyName'
group by Username
order by TOTAL desc;
This assumes that type can only be 'Yes', 'No' or ''.
Use a sub query to sum up your results and add a sort:
select yes, no, other, yes + no + other as Total
from (
SELECT Username,
SUM(CASE WHEN type = 'Yes' THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN type = 'No' THEN 1 ELSE 0 END) as No,
SUM(CASE WHEN type = '' THEN 1 ELSE 0 END) as Other
FROM table
WHERE source = 'CompanyName'
)
order by (yes + no + other) desc
Don't use SUM(null), the SUM of (1,1,1,null) = null, not 3.
SELECT s.*, s.yes+s.no+s.other as all FROM (
SELECT Username,
SUM(CASE WHEN type = 'Yes' THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN type = 'No' THEN 1 ELSE 0 END) as No,
SUM(CASE WHEN type = '' THEN 1 ELSE 0 END) as Other
FROM table
WHERE source = 'CompanyName'
GROUP BY Username
) s
ORDER BY all DESC