I'm not very good at MySQL. I don't know how to do the following result. I would be grateful if you help
SELECT
skill ,
COUNT(*),
COUNT(CASE WHEN answer = 'A' THEN 1 END) AS _TRUE,
COUNT(CASE WHEN answer <> 'B' OR answer <> 'C' THEN 1 END) AS _FALSE
FROM tbl_skill_and_answer
WHERE skill = "Text Types"
GROUP BY skill
How can I do this in mysql-php?
My table is as below
My Table
RESULT
The asker resolved the issue with the following query (no explanation given):
SELECT
skill,
COUNT(*),
COUNT(CASE WHEN answer= result THEN 1 END) AS _TRUE,
COUNT(CASE WHEN answer<> result THEN 1 END) AS _FALSE
COUNT(CASE WHEN answer IS NULL THEN 1 END) AS _EMPTY
FROM tbl_skill_and_answer
WHERE skill= "Text Types"
GROUP BY skill
Related
I'm seeking for solution on how to get count for 3 faculty code then sum all the count in one variable. I didn't found the right query to combine the count and then sum it.
This is the code that I have:
SELECT COUNT(CASE WHEN `fac_code` LIKE '%JABPN%' THEN 1 END) AS count1,
COUNT(CASE WHEN `fac_code` LIKE '%JABFNT%' THEN 1 END) AS count2,
COUNT(CASE WHEN `fac_code` LIKE '%FPKF%' THEN 1 END) AS count3
FROM `list_faculty` WHERE active = 'Y';
And the output that I got:
count1
count2
count3
69
184
36
The output that I need is the sum of all the count which is 289
How about adding this?
COUNT(CASE WHEN fac_code LIKE '%FPKF%' OR fac_code LIKE %JABFNT%' OR fac_code LIKE '%JABPN%' THEN 1 END)
SQL doesn't allow you to use aliases in the same SELECT, so the alternative would be a CTE or subquery.
You can combine these three counts in one condition of case .. when or you can use + between them as follows:
SELECT COUNT(CASE WHEN `fac_code` LIKE '%JABPN%'
OR `fac_code` LIKE '%JABFNT%'
OR `fac_code` LIKE '%FPKF%'
THEN 1 END) AS count1_3
FROM `list_faculty` WHERE active = 'Y';
OR you can use + between them as follows:
SELECT COUNT(CASE WHEN `fac_code` LIKE '%JABPN%' THEN 1 END) +
COUNT(CASE WHEN `fac_code` LIKE '%JABFNT%' THEN 1 END) +
COUNT(CASE WHEN `fac_code` LIKE '%FPKF%' THEN 1 END) AS count1_3
FROM `list_faculty` WHERE active = 'Y';
And the best way in your case is to use condition in WHERE clause as follows:
SELECT COUNT(*)
FROM `list_faculty`
WHERE active = 'Y'
AND (`fac_code` LIKE '%JABPN%'
OR `fac_code` LIKE '%JABFNT%'
OR `fac_code` LIKE '%FPKF%')
I'm working with Yii2 and using ActiveRecords. I am trying find ethnic distribution in a department. I have a query that groups the staff into their tribes and further groups them according to gender and returns the total count in each case.
The end result should look similar to this photo:
The ethnic subtotal is given by adding all the totals of all the tribes in the department. So far I have all the totals for all the tribes in each department. How can I add these totals to get the ethnic subtotal?
My code:
$query = StaffEmploymentListView::find()
->select([
'DEPT_NAME',
'TRIBE_NAME',
"COUNT(CASE WHEN GENDER='MALE' THEN 1 END) AS MALE_COUNT",
"COUNT(CASE WHEN GENDER='FEMALE' THEN 1 END) AS FEMALE_COUNT",
"COUNT(TRIBE_NAME) AS TRIBE_COUNT",
])
->groupBy(['DEPT_NAME','TRIBE_NAME']);
Raw SQL answers are also welcome.
I am not familiar with yii, but generally speaking you can just use + to add values, so your selection criteria would be something like count(...) + count(...) will select the sum of those two counts.
select t.DEPT_NAME,
SUM(CASE WHEN GENDER = 'MALE' OR GENDER = 'FEMALE' THEN 1 END) as SUM,
tt.MALE_COUNT,
tt.FEMALE_COUNT,
tt.TRIBE_COUNT,
tt.TRIBE_NAME from 'StaffEmploymentListView' t
left join (
select DEPT_NAME,
TRIBE_NAME,
SUM(CASE WHEN GENDER = 'MALE' THEN 1 END) as MALE_COUNT,
SUM(CASE WHEN GENDER = 'FEMALE' THEN 1 END) as FEMALE_COUNT,
count(TRIBE_NAME) as TRIBE_COUNT
from 'StaffEmploymentListView' t
group by DEPT_NAME, TRIBE_NAME) tt on t.DEPT_NAME = tt.DEPT_NAME group by t.DEPT_NAME, tt.MALE_COUNT, tt.FEMALE_COUNT, tt.TRIBE_COUNT, tt.TRIBE_NAME;
This maybe can help you, i plat with postgresql
The scenario is I have a column named "States" in a table, States can be 0,1,2,3 or 4. What I want to do is get a count of each state using WHERE State = in a single query.
The main purpose is I want to show the count of records (identified by their state). Like this, 20 records have State 0 etc.
Is this possible? If yes, then how can I achieve this?
Edit: I know about Count. Here's what I have tried:
SELECT State, Date_Created, (SELECT COUNT(Id) FROM [ECOS].[eco].[tb_projects_details] WHERE State=1) as State_One, (SELECT COUNT(Id) FROM [ECOS].[eco].[tb_projects_details] WHERE State=2) as State_Two, (SELECT COUNT(Id) FROM [ECOS].[eco].[tb_projects_details] WHERE State=0) as State_Zero, (SELECT COUNT(Id) FROM [ECOS].[eco].[tb_projects_details] WHERE State=4) as State_Four FROM [ECOS].[eco].[tb_projects_details] WHERE Date_Created < dateadd(week,-3,getdate());
If I understand it correctly, you want to group and count:
SELECT mt.States, COUNT(*) total
FROM my_table mt
GROUP BY mt.States
To generate a list of values and their counts you can do this:
SELECT State, COUNT(*) AS C
FROM mytable
GROUP BY State
To generate one row that contains value counts as columns you can do this:
SELECT
COUNT(CASE State WHEN 0 THEN 1 ELSE NULL END) AS State_0_Count,
COUNT(CASE State WHEN 1 THEN 1 ELSE NULL END) AS State_1_Count,
COUNT(CASE State WHEN 2 THEN 1 ELSE NULL END) AS State_2_Count,
COUNT(CASE State WHEN 3 THEN 1 ELSE NULL END) AS State_3_Count,
COUNT(CASE State WHEN 4 THEN 1 ELSE NULL END) AS State_4_Count
FROM [...]
WHERE [...]
It will be very easy if you use group by clause after where condition. Then you will get the number of each state very easily.
SELECT state, count(*)
FROM table_name
GROUP BY state
Next time read a sql for beginers book first.
Select state, count(*)
from table
group by state
This is a snapshot of my MySQL table:
Is it possible to write a query to get such a pivot table like output...
How about something like
SELECT url_host,
SUM(CASE WHEN post_id = -1 THEN 1 ELSE 0 END) as post_id_minus1,
SUM(CASE WHEN post_id = 0 THEN 1 ELSE 0 END) as post_id_0,
etc...
FROM YOUR_TABLE
GROUP BY url_host
You can use CASE statement on this to pivot your table.
SELECT url_host,
COUNT(CASE WHEN post_ID = -1 THEN 1 ELSE NULL END) Negative_One,
COUNT(CASE WHEN post_ID = 0 THEN 1 ELSE NULL END) Zero,
COUNT(CASE WHEN post_ID > 0 THEN 1 ELSE NULL END) Greater_Zero
FROM tableName
GROUP BY url_host
SQLFiddle Demo
I want to have an only query with 2 queries but i don't know how to start...
My queries count the positive/negative votes for a comment.
SELECT COUNT(id) AS votes_no FROM comments_votes WHERE vote = 0 AND id_comment = 1
SELECT COUNT(id) AS votes_no FROM comments_votes WHERE vote = 1 AND id_comment = 1
I set vars to put negative and positives votes : $votes_no and $votes_yes
Then i have a final var : $votes_calc = $votes_yes - $votes_no;
How can i get the number of votes_yes, votes_no and votes_calc in only one query?
Thanks a lot!
select votes_no, votes_yes, votes_yes-votes_no as votes_calc
from (select sum(case when vote = 0 then 1 else 0 end) as votes_no,
sum(case when vote = 1 then 1 else 0 end) as votes_yes
from comments_votes
where id_comment = 1) a
select vote,count(id)
from Comment_votes
group by vote
WHERE id_comment = 1
with rollup
The with Rollup will add a row with a NULL value in the vote column and the total in the second column
I merge the query getting the comments and the comments votes and it seems to work :)
SELECT a.*, nb_votes, votes_yes-votes_no AS votes_calc
FROM comments AS a
LEFT JOIN (
SELECT id_comment, COUNT(id) AS nb_votes,
SUM(CASE WHEN vote = 0 THEN 1 ELSE 0 END) AS votes_no,
SUM(CASE WHEN vote = 1 THEN 1 ELSE 0 END) AS votes_yes
FROM comments_votes GROUP BY id_comment
) AS c ON (a.id = c.id_comment)
WHERE a.status = 'approved' AND a.id_post = 1 ORDER BY a.time ASC
Thanks for your answers :)