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!

Stack Overflow Asked on November 15, 2021

2 Answerstry 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
```

Answered by Nik Srinivas on November 15, 2021

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
```

Answered by ImTryingMyBest on November 15, 2021

1 Asked on December 11, 2020 by tora-garo

2 Asked on December 10, 2020 by sanane-sanane

3 Asked on December 10, 2020 by robin-orheden

0 Asked on December 10, 2020 by bgeun

0 Asked on December 10, 2020 by fgg1991

0 Asked on December 10, 2020 by giantg2

0 Asked on December 10, 2020 by fingers10

asp net core 3 1 asp net core webapi entity framework core entity framework core 3 1 sqlconnection

1 Asked on December 10, 2020 by azza-abidi

4 Asked on December 9, 2020 by jacky-chan

1 Asked on December 9, 2020 by thor

2 Asked on December 9, 2020

0 Asked on December 9, 2020 by eduardo

1 Asked on December 9, 2020 by dripis

3 Asked on December 9, 2020 by nikhila-reddy-c

0 Asked on December 9, 2020 by melissa-arfaoui

1 Asked on December 9, 2020 by nataku62

Get help from others!

Recent Answers

- Lutz Lehmann on Differential equation, modulus signs in solution?
- neca on In a Reflexive banach space, given a closed convex set $C$ and some point $y$, there is a point in $C$, of minimal distance to $y$
- Evangelopoulos F. on In a Reflexive banach space, given a closed convex set $C$ and some point $y$, there is a point in $C$, of minimal distance to $y$
- JJacquelin on Differential equation, modulus signs in solution?
- DanielWainfleet on Consider the sequence where $a_1>0$, $ka_n>a_{n+1}$ and $0<k<1$. Can we say it converges?

Recent Questions

- Differential equation, modulus signs in solution?
- Consider the sequence where $a_1>0$, $ka_n>a_{n+1}$ and $0<k<1$. Can we say it converges?
- In a Reflexive banach space, given a closed convex set $C$ and some point $y$, there is a point in $C$, of minimal distance to $y$
- Ball / Urn question with a twist
- How to prove $phi'(t)1_{Omega_t}(w)$ is measurable?

© 2021 InsideDarkWeb.com. All rights reserved.