I need help. I can't seem to find the logic behind this code.
I am working on a voting system, and I need to output the results of the votes.
I want to count all of the rows that has a unique name in it and output how many.
My table goes like this.
voterid | pres | vpres | sec | trea | PIO
---------------------------------------------
1 | John | Mitch | James | Jack | Eman
2 | John | Pao | Bryan | Jack | Faye
3 | Kelvin | Pao | James | Jeck | Faye
Output should be
Pres | Votes
--------------
John | 2
Kelvin | 1
Here's my code.
SELECT DISTINCT
pres,
(SELECT COUNT(pres) FROM (SELECT DISTINCT pres FROM tblVote AS Votes)) AS Votes
FROM tblVote
Thanks in advance!
I think you are just looking for a simple GROUP BY query:
SELECT pres, COUNT(*) AS Votes
FROM tblVote
GROUP BY pres
Related
The title is a bit messy, but here's an example
suppose we have table:
| name | room |
=================
| John | 4 |
| John | 6 |
| John | 9 |
| Smith | 4 |
| Smith | 6 |
| Brian | 4 |
| Brian | 6 |
| Brian | 9 |
I want to select John and Brian because they both have exactly rooms 4, 6 and 9, but not Smith, since he doesn't have the room 9. (If we had another person who ONLY has room 4 and 6, then it'd select that other person as well as Smith).
I know I need to do some kind of correlated query, but I'm not sure how to actually get it to do something like
for a check for b
If you want groups of names that share the exact same rooms, I would recommend group_concat():
select rooms, group_concat(name) as names
from (select name, group_concat(room order by room) as rooms
from t
group by name
) n
group by rooms;
If you want only combinations with more than one name, then add having count(*) > 1 to the outer select.
This query
select r1.name, count(stars) as reviewCount
from (reviewer r1 join reviewer r2 using(rID)) join rating using(rID)
group by r1.name;
Yields the following output
+------------------+--------------+
| name | reviewCount |
+------------------+--------------+
| Ashley White | 1 |
| Brittany Harris | 3 |
| Chris Jackson | 3 |
| Daniel Lewis | 1 |
| Elizabeth Thomas | 2 |
| James Cameron | 1 |
| Mike Anderson | 1 |
| Sarah Martinez | 2 |
+------------------+--------------+
i want to list users who have reviewCount > 3, something like below
+------------------+--------------+
| name | reviewCount |
+------------------+--------------+
| Brittany Harris | 3 |
| Chris Jackson | 3 |
+------------------+--------------+
i have tried using where and having clause but they result in Empty set (0.01 sec)
select r1.name, count(stars) as reviewCount
from (reviewer r1 join reviewer r2 using(rID)) join rating using(rID)
group by r1.name
having reviewCount > 3;
What is it that i am missing?
edit1 : refer this for sample data to test
edit 2: Also can anyone suggest how can i write this query without using count and having
The result is actually correct but your query needs a little modification. You need to use >= rather than >
HAVING reviewCount >= 3;
I'd rather use the aggregated column than it's alias since it will work on most RDBMS.
HAVING count(stars) >= 3
Apologies if the question title is not clear. Don't really know how to phrase the following in one line.
I hope I mange to explain what I want to achieve. This is wrecking me head.
Currently I have a table which keeps a record for the training status of employees for different projects. Structure is like this:
--------------------------------------------------------------
|id|timestamp |employee |project |status |
--------------------------------------------------------------
|1 |2015-10-01 00:00:00 | john doe | project_1 | In Training |
|2 |2015-10-04 00:00:00 | jane doe | project_1 | In Training |
|3 |2015-11-01 00:00:00 | john doe | project_1 | Live |
|4 |2015-10-08 00:00:00 | john doe | project_2 | In Training |
|5 |2015-11-01 00:00:00 | jane doe | project_2 | In Training |
|6 |2015-11-09 00:00:00 | jane doe | project_3 | Live |
Now management requested a table / view which gives an overview about the latest status per employee. So I'm looking for a query which returns this:
-----------------------------------------------------
| employee | project_1 | project_2 | project_3 |
-----------------------------------------------------
| John Doe | Live | In Training | |
| Jane Doe | In Training | In Training | Live |
So the query should look for the latest entry per employee and project and return the status of that project.
Is there any way this is doable?
Well, you're conflating 2 problems into 1. The first problem is how to get the latest status per employee per project. The second is how to show the data in columns. The first problem is more interesting, so that's the one I'll answer.
SELECT DISTINCT x.employee
, y.project
, z.timestamp
, z.status
FROM my_table x
JOIN my_table y
LEFT
JOIN
( SELECT a.*
FROM my_table a
JOIN
( SELECT employee
, project
, MAX(timestamp) max_timestamp
FROM my_table
GROUP
BY employee
, project
) b
ON b.employee = a.employee
AND b.project = a.project
AND b.max_timestamp = a.timestamp
) z
ON z.employee = x.employee
AND z.project = y.project;
I have an requirement to return all educators associated with a session as a concatenated list, but to do it within a join (don't ask - I know there are multiple ways of doing it but I'm working with a library that won't accept those methods ;-))
I have a sessions table with session_id and session_name.
| session_id | session_name |
+------------+--------------+
| 1 | Swimming |
| 2 | Chess |
I have a session_educators table which is basically a junction table session_id and contact_id.
| session_id | contact_id |
+------------+------------+
| 1 | 1 |
| 1 | 2 |
| 2 | 3 |
| 2 | 4 |
I have a contacts table with contact_id and full_name
| contact_id | full_name |
+------------+--------------+
| 1 | Fred Bloggs |
| 2 | Mary Bloggs |
| 3 | Mark Smith |
| 4 | Shelly Smith |
So far this seems to be the closest I've come:
SELECT
sessions.session_id,
sessions.name,
educators.names
FROM
`sessions`
LEFT JOIN
(
SELECT
GROUP_CONCAT(contacts.full_name SEPARATOR ', ') as names
FROM
`contacts`
WHERE
contact_id
IN
(
SELECT
contact_id
FROM
session_educator
WHERE
session_educator.session_id = sessions.session_id
)
) `educators`
USING
(`session_id`)
But I'm really fumbling in the dark trying to figure it out, can anyone help?
What I'm wanting, as you can probably tell from the query, is a result like this:
| session_id | session_name | educators |
+------------+--------------+--------------------------+
| 1 | Swimming | Fred Bloggs, Mary Bloggs |
| 2 | Chess | Mark Smith, Shelly Smith |
Any help greatly appreciated - even if it's just to say it can't be done!
You shouldn't need subqueries for this. It is a few joins and an aggregation:
select s.session_id, s.session_name,
group_concat(e.name separator ', ') as educators
from sessions s left join
session_educators se
on se.session_id = s.session_id left join
educators e
on e.educator_id = se.educator_id
group by s.session_id;
I do believe you're making it all a little more complex than needed (unless I've missed something in your requirements? I know how much of a pain libraries can be...)
This worked for me:
SELECT
session_id,
session_name,
(
SELECT
GROUP_CONCAT(full_name SEPARATOR ', ') as names
FROM
contacts c,
session_educators se
WHERE
c.contact_id = se.contact_id
AND
se.session_id = s.session_id
)
FROM
sessions s
;
I have a table like this:
name | day | score
------------------
John | 1 | 4
John | 2 | 5
John | 3 | 6
Marc | 1 | 7
Marc | 2 | 4
Marc | 3 | 5
Paul | 1 | 8
Paul | 2 | 2
Paul | 3 | 3
I want to get the sum of the score for each person, but only for certain days, sorted by this sum. let's say I want to get the score-sum of the 1. and 2. day, this is what I expect:
name | sum(score)
-----------------
Marc | 11
Paul | 10
John | 9
this is what failed:
SELECT name, sum(score) FROM mytable WHERE day<=2
I think I have to surround the sum(score)-part with some IF-statement, but I have no idea how.
Just add group by
SELECT name, sum(score) FROM mytable WHERE day<=2 group by name
Use sum function and group by clause for grouping the result.
query
select name,sum(score) as score
from myTable
where day in (1,2)
group by name
order by sum(score) desc;
fiddle demo