I want a count of the number of rows for each user in the table. For example, suppose I have a table like this:
user_id | order_id | name
--------+----------+-----
6 | a1 | Joe
6 | b4 | Joe
6 | c2 | Joe
6 | d5 | Joe
6 | a6 | Joe
8 | b9 | Mary
8 | c7 | Mary
8 | a2 | Mary
3 | ba | Jack
3 | c3 | Jack
3 | a9 | Jack
3 | b6 | Jack
5 | c9 | Jill
5 | d2 | Jill
I want a result that tells me that Joe appears 5 times, Mary 3 times, Jack 4 times, and Jill twice. So I want a sql statement (preferably mySql, but any language will do), that gives results like this:
name | user_id | count
-----+---------+------
Joe | 6 | 5
Mary | 8 | 3
Jack | 3 | 4
Jill | 5 | 2
Is there a way to do this in sql? I can't find one, but there's a lot I don't know about sql.
Select
Count(userid) as count
, userid
, name
From table
Group by userid, name
Please try:
SELECT COUNT(order_id) AS count, name FROM table_name GROUP BY name
Related
I have two tables (Manager and Worker). I want to do a left join from Manager to Worker. The name of workers under a manager should come below manager name. The expected output is presented below:-
Manager Table:
ManagerCode Name Age
1 Chris 52
2 Rick 55
3 David 50
Worker Table
ManagerCode WName Age
1 Harry 33
1 Phil 40
2 Johnny 28
2 Jeff 47
Expected table:
ManagerCode Name Age
1 Chris
1 Harry
1 Phil
2 Rick
2 Johnny
2 Jeff
3 David
Left join results both manager and worker names side by side. But I want them in one column.
You meed UNION ALL and not a join:
select t.managercode, t.name, t.age
from (
select managercode, name, age, 1 as ismanager from Manager
union all
select managercode, wname, age, 0 from Worker
) t
order by t.managercode, t.ismanager desc
The column ismanager is used to sort the manager before all the workers under them.
See the demo.
Results:
| managercode | name | age |
| ----------- | ------ | --- |
| 1 | Chris | 52 |
| 1 | Phil | 40 |
| 1 | Harry | 33 |
| 2 | Rick | 55 |
| 2 | Johnny | 28 |
| 2 | Jeff | 47 |
I have 2 tables in MS Access with the following values
Customer
id | name
1 | jon
2 | bob
3 | jack
Order
id | amount | date | customer
5 | 50 | 3/10/2017 | 1
4 | 100 | 3/10/2017 | 1
3 | 45 | 2/28/2017 | 2
2 | 10 | 3/10/2017 | 3
1 | 5 | 3/10/2017 | 2
I want to get an output of
name | orderid | amount
jon | 5 | 50
bob | 3 | 45
jack | 2 | 10
I want to get amount of the latest order id per customer, however I keep getting this
name | orderid | amount
jon | 5 | 50
jon | 4 | 100
bob | 3 | 45
bob | 2 | 10
jack | 1 | 5
I used the query designer and have used the function MAX() to the order id, GROUP BY to all columns (MS Access does not allow to group the rows using a single column), DISTINCT and DISTINCTROW, as well as set the query properties "Unique Records" to Yes but the duplicate records still shows.
[Users]
ID | UserID | City | Phone
----+-----------+-----------+----------
1 | John | Rome | 12345
2 | Tom | Oslo | 12345
3 | Simon | Bogota | 12345
4 | Kurt | Tokyo | 12345
[Orders]
ID | UserID | OrderNr | OrderName
------------------------------------------------
1 | John | 1 | Apple
2 | John | 2 | Carrots
3 | John | 3 | Banana
4 | Tom | 3 | Banana
5 | Tom | 1 | Apple
6 | Tom | 8 | Raisins
7 | Simon | 3 | Banana
8 | Simon | 1 | Apple
9 | Kurt | 7 | Cucumber
Approved List
1 (Apple)
3 (Banana)
4 (Another order)
8 (Raisins)
Now i would like to select all Users who's orders only contains/matches my approved list.
John should be excluded in this case because he ordered Carrots which has OrderNr 2 that is not in my approved list.
If you want users who only have orders in the approved list, I would suggest conditional aggregation and a having clause:
select o.userid
from orders as o
group by o.userid
having sum(iif(o.OrderNr not in (1, 3, 4, 8), 1, 0)) = 0;
The having clause filters out users who have an "unapproved" product.
I have three tables like this:
Student
stuNum | stuName
------------------
2012 | jack
2013 | tom
Quiz
quizNum | quizName
------------------
1 | chapter 1
2 | chapter 2
3 | chapter 3
studentassessment
stuNum | quizNum | assessmentMark
-----------------------------------
2012 | 1 | 10
2012 | 2 | 8
2012 | 3 | 10
2013 | 1 | 5
I want to get result something like this
stuNum | stuName | Quiz Num | assessmentMark
--------------------------------------------
2012 | jack | 1 | 10
2012 | jack | 2 | 8
2012 | jack | 3 | 10
description : all three table are connected.. i want to get the stuNum=2012, quizNum that all stuNum have done.
I tried several combination to fetch result but can't work.
this is example combination that i tried :
$sql = "select a.stuNum,a.quizNum,a.assessmentMark from studentassessment a inner join student b on a.stuNum=b.stuNum inner join quiz c on a.quizNum=c.quizNum where b.stuNum='2012'"
Query
SELECT a.stuNum,
b.stuName,
a.quizNum,
c.quizName,
a.assessmentMark
FROM studentAssessment a
JOIN student b
ON a.stuNum=b.stuNum
JOIN quiz c
ON a.quizNum=c.quizNum
WHERE a.stuNum=2012;
Output
+--------+---------+---------+-----------+----------------+
| STUNUM | STUNAME | QUIZNUM | QUIZNAME | ASSESSMENTMARK |
+--------+---------+---------+-----------+----------------+
| 2012 | Jack | 1 | chapter 1 | 10 |
| 2012 | Jack | 2 | chapter 2 | 8 |
| 2012 | Jack | 3 | chapter 3 | 10 |
+--------+---------+---------+-----------+----------------+
Fiddle demo
Here .. try this out
SELECT a.stuNum,b.stuName, a.quizNum, a.assessmentMark
FROM studentassessment a
INNER JOIN student b ON a.stuNum = b.stuNum
WHERE b.stuNum='2012'
you do not need to put an inner join on quiz table since neither you are retrieving any value from it nor you are using it to deduce some relation in your output
These are the tables:
professor:
+-------+--------+--------+--------+------+
| empid | name | status | salary | age |
+-------+--------+--------+--------+------+
| 1 | Arun | 1 | 2000 | 23 |
| 2 | Benoy | 0 | 3000 | 25 |
| 3 | Chacko | 1 | 1000 | 36 |
| 4 | Divin | 0 | 5000 | 32 |
| 5 | Edwin | 1 | 2500 | 55 |
| 7 | George | 0 | 1500 | 46 |
+-------+--------+--------+--------+------+
works:
+----------+-------+---------+
| courseid | empid | classid |
+----------+-------+---------+
| 1 | 1 | 10 |
| 2 | 2 | 9 |
| 3 | 3 | 8 |
| 4 | 4 | 10 |
| 5 | 5 | 9 |
| 6 | 1 | 9 |
| 2 | 3 | 10 |
| 2 | 1 | 7 |
+----------+-------+---------+
course:
+----------+------------+--------+
| courseid | coursename | points |
+----------+------------+--------+
| 1 | Maths | 100 |
| 2 | Science | 80 |
| 3 | English | 85 |
| 4 | Social | 90 |
| 5 | Malayalam | 99 |
| 6 | Arts | 40 |
+----------+------------+--------+
The question is :
Return list of employees who have taught course Maths or Science but
not both
The query which I wrote is :
select distinct professor.name from professor
inner join works
on professor.empid=works.empid
where works.courseid in
(select courseid from course where coursename ='Maths' or coursename='Science');
The output I received is:
Arun
Benoy
Chacko
Here the employee 'Arun' shouldnt have been displayed as he as taught both Maths and Science.
Please help me out !!
You may use an aggregate COUNT() to check that the total number of DISTINCT courses taught is exactly 1, while still filtering to the two different types of courses. That ensures that only one, never both, is returned.
Because the IN () limits all rows initially returned only to the two desired courses, professors can have a maximum of 2 possible different courses via COUNT(DISTINCT coursename). A HAVING clause then prohibits those with 2 from the final result set.
SELECT
DISTINCT professor.name
FROM
professor
INNER JOIN works ON professor.empid = works.empid
/* Join against course to get the course names */
INNER JOIN course ON works.courseid = course.courseid
WHERE
/* Restrict only to Maths, Science */
course.coursename IN ('Maths', 'Science')
GROUP BY professor.name
/* Only those with exactly one type of course */
HAVING COUNT(DISTINCT course.coursename) = 1
Here is a demonstration: http://sqlfiddle.com/#!2/2e9610/2
You want to use an xor here instead of an or.
select distinct professor.name from professor
inner join works
on professor.empid=works.empid
where works.courseid in
(select courseid from course where coursename ='Maths' xor coursename='Science');