I have the following table:
id
name
type
score
1
john
orange
2345
2
john
yellow
5
3
john
black
5454540
4
jack
orange
1123
5
jack
yellow
1000
6
jack
black
86943
7
jane
orange
9876
8
jane
yellow
10000
9
jane
black
102233
comment;
id : inte
name : same name save more times to more rows,
score: it is int data
type : it has string data black,white,yellow,purple,orange
I'm using the following queries to calculate two score totals
SELECT name,sum(score) as `first`
FROM salary
WHERE type = 'orange'
SELECT name,sum(score) as `second`
FROM salary
WHERE type in ('black','yellow')
i want see result that ( all names must be group, single name.)
name
FirstScore
SecondScore
john
2345
5454545
jack
1123
87943
jane
9876
112233
Use a conditional SUM() to aggregate the values based on type:
SELECT name
, SUM(CASE WHEN type IN ('orange') THEN score END ) AS FirstScore
, SUM(CASE WHEN type IN ('yellow','black') THEN score END ) AS SecondScore
FROM salary
GROUP BY Name
Results:
name
FirstScore
SecondScore
john
2345
5454545
jack
1123
87943
jane
9876
112233
db<>fiddle here
Something like this (but this is untested):
SELECT
salary.name,
first.score as "first_score",
second.score as "second_score"
FROM salary
LEFT JOIN (SELECT name,sum(score) as score
FROM salary
WHERE type = 'orange'
) as first ON first.name = salary.name
LEFT JOIN (SELECT name,sum(score) as score
FROM salary
WHERE type in ('black','yellow')
) as second ON second.name = salary.name
Related
studClassJunction
studID
classID
1
A
2
A
1
B
2
B
studOutput
studID
classID
actID
score
1
A
act1
23
1
A
act2
15
2
A
act2
16
1
B
act1
18
2
B
act1
18
userRecd
userID
frstnme
role
1
Carlos
student
2
Roberto
student
3
Lorem
teacher
My goal is to get all the students score and their names in a given activity stored in a specific classroom whether the student has a score or not.
The point is to show the teacher (the one viewing this) the students who has answered the activity and those who did not.
So, for example, get score and name for activity ID act1 for all students in class ID A.
Expected Output:
studID
frstnme
score
1
Carlos
23
2
Roberto
null
Since studID 1 has answered the activity, there's a value in the score column (23). However, studID 2 has only answered act2, and not act1, so they have a score of null, which I think can be changed to 0 through CASE expression.
How do I get this kind of result?
EDIT 1
This is my attempt so far in achieving this. It goes as follows:
SELECT SCJ.studID AS ID, UR.firstnme AS NAME, SO.score AS SCORE
FROM studClassJunction AS SCJ
INNER JOIN studOutput AS SO
ON SCT.classID = SO.classID
INNER JOIN userRecd AS UR
ON SCT.studID = UR.userID
WHERE (SO.actID = "act1" OR SO.actID IS NULL) AND SCJ.classID = "A"
This query will yield this result:
| ID | NAME | SCORE |
|-----------------|------------------|----------------|
| 1 | Carlos | 23 |
| 2 | Roberto | 23 |
For some reasons, instead of a null, my SQL query has also put 23 in studID 2's score column.
You aren't joining on student ID. If you do that, it should work.
SELECT SCJ.studID AS ID, UR.firstnme AS NAME, coalesce(SO.score, 0) AS SCORE
FROM studClassJunction AS SCJ
INNER JOIN studOutput AS SO
ON SCJ.classID = SO.classID and SCJ.studID = SO.studID
INNER JOIN userRecd AS UR
ON SCT.studID = UR.userID
WHERE (SO.actID = "act1" OR SO.actID IS NULL) AND SCJ.classID = "A"
I have data like this
id otherid name
1 123 banana
2 123 banana
3 123 banana
4 456 grape
5 456 grape
6 789 orange
7 111 banana
How can I get output like this: (with MySQL query)
name count
banana 2
grape 1
orange 1
Try this:
SELECT
f.`name`,
COUNT(DISTINCT (f.`otherid`))
FROM
`fruits` f
GROUP BY f.`name`
You can use COUNT with GROUP BY:
SELECT
`name`, COUNT(*)
FROM
write_you_table_name
GROUP BY
`name`,`otherid`
Using distinct in the count function is recommended. But if you prefer not to using it, try this:
select name,count(*) from
(select name from fruit group by name,otherid) t
group by name;
SELECT name , count(*) as count FROM tb_stock GROUP BY other_id
I have a question about Query using "Count" function.
I have a table : Student(id,name,age,sex)
I use sql query as:
Select name, age, count(id) as NumberOf
from Student
where sex = 'Boy'
group by age, name
And My DB:
1 | John | 12 | Boy |
2 | Mary | 13 | Girl |
3 | Alice | 15 | Girl |
I want to find a number of student is the same as age as well as boy.But it returns Null. I want it to return 0. How should? Thanks
I suppose you want this :
select name, age, sum(if(sex='Boy',1,0)) as NumberOf
from student
group by age, name;
name age NumberOf
John 12 1
Mary 13 0
Alice 15 0
P.S. deliberately I put sum due to probability of existence of duplicated record for name and age.
You can do this without if :
select name, age, sum(sex='Boy') as NumberOf
from student
group by age, name;
I have a table that looks like this:
Categories:
cId | Name | Parent
----+-------------------------+-------
1 | Parent One | NULL
2 | Child of 1st Parent | 1
3 | Parent Two | NULL
4 | Child of 1st Parent | 1
5 | Child of 2nd Parent | 2
The table does not represent a heirarchy: Every item is either a child or a parent, but not both.
And one table like this:
Posts:
pId | Name | cID
----+-------------------------+-------
1 | Post 1 | 1
2 | Post 2 | 2
3 | Post 3 | 2
4 | Post 4 | 3
I'd like to run a query on it that returns this:
cId | Count
---+---------
1 | 3
2 | 2
3 | 1
4 | 0
5 | 0
Count is the number of posts connected to the category.
All categories should be returned.
Parent categories should have the count of the category + child categories sum. (this is one of the things I'm having problem with)
Child categories should have the category sum.
How should I do this?
From your expected results, it looks like you don't care about grandchildren and lower, in which case, this should work. To get the correct parent count, I'm checking for Parent IS NULL or Count(children) > 0, in which case, I'm adding 1:
SELECT c.cId, CASE WHEN C.Parent IS NULL OR COUNT(C2.cId) > 0 THEN 1 ELSE 0 END +
COUNT(C2.cId) TotalCount
FROM Categories C
LEFT JOIN Categories C2 on c.cId = c2.Parent
GROUP BY c.cId
Here is some sample fiddle: http://www.sqlfiddle.com/#!2/b899f/1
And the results:
CID TOTALCOUNT
1 3
2 2
3 1
4 0
5 0
---EDIT---
From reading your comments, it looks like you want something like this:
SELECT c.cId,
COUNT(DISTINCT P.pId) + COUNT(DISTINCT P2.pId) TotalCount
FROM Categories C
LEFT JOIN Posts P ON C.CId = P.CId
LEFT JOIN Categories C2 on c.cId = c2.Parent
LEFT JOIN Posts P2 ON C2.CId = P2.CId
GROUP BY c.cId
http://www.sqlfiddle.com/#!2/eb0d2/3
This is general hint. I do not know if analytic functions and partitioning available in MySQL but you can partition your output by categories then count and sum up within categories. Do some research about analytic functions and partition by clause. General example of what I meant - output is partitioned by deptno and ordered. Also, max hiredate determined within partition - replace max with count, sum etc... in your case:
SELECT * FROM
(
SELECT deptno
, empno
, ename
, sal
, RANK() OVER (PARTITION BY deptno ORDER BY sal desc) rnk
, ROW_NUMBER() OVER (PARTITION BY deptno ORDER BY sal desc) rno
, MAX(hiredate) OVER (PARTITION BY deptno ORDER BY deptno) max_hire_date
FROM emp_test
ORDER BY deptno
)
--WHERE rnk = 1
ORDER BY deptno, sal desc
/
DEPTNO EMPNO ENAME SAL RNK RNO MAX_HIRE_DATE
--------------------------------------------------------------------
10 7839 KING 5000 1 1 1/23/1982
10 7782 CLARK 2450 2 2 1/23/1982
10 7934 MILLER 1300 3 3 1/23/1982
20 7788 SCOTT 3000 1 1 1/28/2013
20 7902 FORD 3000 1 2 1/28/2013
20 7566 JONES 2975 3 3 1/28/2013
I'm currently working with this MySQL statement:
SELECT * FROM jobs GROUP BY jobType, address1;
I need to be able to add another condition to this query. GROUP BY only combines duplicates, but I also need to combine all rows that have a jobType of '1' or a jobType of '2'. All other jobTypes will be grouped normally.
For example, my table looks something like this:
jobType | address1
1 | 123 State st
2 | 123 State st
3 | 415 5th Ave
1 | 123 State st
1 | 24 3rd Ave
1 | 123 State st
3 | 555 Mission st
4 | 123 State st
I want to combine rows 1 and 2, even though their jobType is different. Again, any rows with a jobType other than either 1 or 2 would be grouped normally.
SELECT *
FROM jobs
GROUP BY CASE WHEN jobType in ('1','2') THEN '1' ELSE jobType END, address1