Counting the number of mention depending on sentiment SQL - mysql

Apologies if this is a duplicate of anything, I wasn't finding answers which particularly did what I wanted.
I'm trying to write a SQL query which will return the count of rows which contain a positive, negative or neutral sentiment on one of the candidates in the dataset.
Here is a screenshot for reference
Sentiment is one column but the values in it define the tweet to be positive, negative, or neutral. my goal is to have the query return something like this
if anyone could give me an example on how to do this, I'd appreciate!

try using specific COUNT() functions in your query like this.
SELECT name as `Candidate Name`,
COUNT(CASE WHEN sentiment='Negative' THEN 1 END) AS `Negative`,
COUNT(CASE WHEN sentiment='Positive' THEN 1 END) AS `Positive`,
COUNT(CASE WHEN sentiment='Neutral' THEN 1 END) AS `Neutral`,
COUNT(*) AS `Total`
FROM [table]
GROUP BY candidate

I like using IF()'s or CASE WHEN's to solve this type of thing. Pivots are sometimes time consuming to think through.
SELECT
Name as CandidateName,
SUM(IF(Sentiment = 'N', 1, 0)) as Negative,
SUM(IF(Sentiment = 'Y', 1, 0)) as Positive,
SUM(IF(Sentiment = 'N', 1, 0)) as Neutral
COUNT(*) as Total
FROM [TABLE]
GROUP BY
Name
To use t-SQL, or to just use CASE WHEN's, that same code could look like:
SELECT
Name as CandidateName,
SUM(CASE WHEN Sentiment = 'N' THEN 1 ELSE 0 END) as Negative,
SUM(CASE WHEN Sentiment = 'Y' THEN 1 ELSE 0 END) as Positive,
SUM(CASE WHEN Sentiment = 'N' THEN 1 ELSE 0 END) as Neutral
COUNT(*) as Total
FROM [TABLE]
GROUP BY
Name

Related

How do I get the sum of each line depending on the team name?

I have a table called data:
And I would like to get the sum of each row as column name, like this:
What is the correct SQL statement I need? I've been trying with CASE or IF but I just don't get the right answer.
You need aggregation :
select
sum(case when team = 'team business' then total_points else 0 end) as total_business,
sum(case when team = 'team tech' then total_points else 0 end) as total_tech
from
data d;

Reduce the number of queries

I have seperate queries but i need to reduce the no so put all in one
select count(applicant_id) as registered from student_application where filter_status=0 AND
select count(applicant_id) as filer_select from student_application where filter_status=1 AND
select count(applicant_id) as filter_reject from student_application where filter_status=2
but this shows some errors
Use CASE expression.
Query
select
count(case when filter_status = 0 then applicant_id else null end) as registered,
count(case when filter_status = 1 then applicant_id else null end) as filer_select,
count(case when filter_status = 2 then applicant_id else null end) as filer_reject
from student_application;
SQL Fiddle
You could also use group_by, with the where clause if you're looking for a subset rather than all possible values of filter_status:
SELECT filter_status, COUNT(*)
FROM student_application
WHERE filter_status in (0, 1, 2)
GROUP BY filter_status;

How to sum varchar column in SQL?

I am a beginner in SQL. I need to sum up the gender column.I mean how many males and how many females are there in a Table.This is what i tried.
SELECT
SUM(CASE WHEN Gender='Female' THEN Gender ELSE 'Null' END)Gender,
SUM(CASE WHEN Gender='Male' THEN Gender ELSE 'Null' END)Gender
FROM EmployeeDetails;
I am getting this error:
Msg 8117, Level 16, State 1, Line 2
Operand data type nvarchar is invalid for sum operator.
So i tried the following query:
Select length(Gender) - length(replace(Gender, ' ', '')) + 1 NumbofWords
from EmployeeDetails
Its showing as length is not a recognized builtin function.
Any help will be appreciated.
Thanks in advance.
SUM + CASE will work, but you need to sum numbers like:
SELECT
SUM(CASE WHEN Gender='Female' THEN 1 ELSE 0 END) AS [FemaleNumber],
SUM(CASE WHEN Gender='Male' THEN 1 ELSE 0 END) AS [MaleNumber]
FROM EmployeeDetails;
SQL Server 2012+
SELECT
SUM(IIF(Gender='Female', 1, 0)) AS [FemaleNumber],
SUM(IIF(Gender='Male', 1, 0)) AS [MaleNumber]
FROM EmployeeDetails;
This is a little bit of an XY solution, given that it produces rows and not columns. But this is more "database-y", and essentially what you have is a PIVOTed version of the following anyway.
You can't add words, but you can count them. So just GROUP BY your Gender column and COUNT:
select Gender, count(*)
from EmployeeDetails
group by Gender

get count of two table fields in one query

I am trying to get the count of females and males in the gender field of a table.
Is there a way to get the count of each in one query?
Something like:
select * from table count(where gender = 'm') as total_males, count(where gender = 'f') as total_females;
or will it require two queries?
select count(*) from table where gender = 'm';
select count(*) from table where gender = 'f';
This is basically a PIVOT. MySQL does not have a pivot so you can use an aggregate function with a CASE statement to perform this:
select
sum(case when gender = 'm' then 1 else 0 end) Total_Male,
sum(case when gender = 'f' then 1 else 0 end) Total_Female
from yourtable
See SQL Fiddle with Demo
Or using COUNT:
select
count(case when gender = 'm' then 1 else null end) Total_Male,
count(case when gender = 'f' then 1 else null end) Total_Female
from yourtable;
See SQL Fiddle with Demo
Something like this will work:
SELECT SUM(IF(t.gender='m',1,0)) AS total_males
, SUM(IF(t.gender='f',1,0)) AS total_females
FROM mytable t
The "trick" here is that we are using a conditional test to return either a 0 or a 1 for each row, and then adding up the 0's and 1's. To make this a little more clear, I am using the SUM aggregate function rather than COUNT, although COUNT could be used just as easily, though we'd need to return a NULL in place of the zero.
SELECT COUNT(IF(t.gender='m',1,NULL)) AS total_males
, COUNT(IF(t.gender='f',1,NULL)) AS total_females
FROM mytable t
Consider that the two expressions in the SELECT list of this query:
SELECT COUNT(1)
, SUM(1)
FROM mytable t
Will return the same value.
If you want to avoid the MySQL IF function, this can also be done using the ANSI SQL CASE expression:
SELECT SUM( CASE WHEN t.gender = 'm' THEN 1 ELSE 0 END )) AS total_males
, SUM( CASE WHEN t.gender = 'f' THEN 1 ELSE 0 END )) AS total_females
FROM mytable t
select sum(case when gender='m' then 1 else null end) as total_males, sum(case when gender='f' then 1 else null end) as total_females from ...
Should work just fine!
If your only issue is to avoid two queries, you can always write two queries as subselects of one query.
Select (select 1 from dual) as one, (select 2 from dual) as two from dual
This would work for your scenario, too.

Order by * where *

Sorry that my english isn't the best.
Is it possible to do a "where" in "order by".
Example :
ORDER BY
SUM(points),
SUM(points) WHERE type = 1,
SUM(goal),
SUM(goal) WHERE type = 1
So as first we sort by points.
If anyone has the same number of SUM(points), then we sort by SUM(points) where type = 1.
If anyone has the same number of SUM(points) and the same number of SUM(points) where type = 1, then we sort after SUM(goal).
If anyone has the same in all 3, then it have to sort by SUM(goal) where type = 1.
SUM(CASE WHEN type=1 THEN points ELSE 0 END)
etc.
you should of course add DESC on all order conditions, if you want the "best" first ;)
And the sqlFiddle
ORDER BY
SUM(points),
SUM(CASE WHEN type = 1 THEN points ELSE 0 END),
SUM(goal),
SUM(CASE WHEN type = 1 THEN goal ELSE 0 END)