i have a table like this
id genre genre2
1 25 0
2 12 25
3 25 12
4 18 17
5 19 14
and i want to count the different genres id and the count of them in both columns but merged into one
and want a result like this
genre count
25 3
12 2
18 1
19 1
17 1
14 1
0 1
i have tried unions, and counts, and everything i think of, but i cant achieve to get the result i want, the closest was making two querys and UNION them, but it doesnt work like i want
any ideas? thanks for the help
You must use UNION ALL instead UNION.
SELECT
genre, COUNT(*) cnt
FROM
(SELECT genre FROM tbl
UNION ALL
SELECT genre2 FROM tbl) a
GROUP BY genre
ORDER BY cnt DESC
You can try it on SQL FIDDLE
Related
I have the following data,
id emp_id csa_taken
1 100 2
2 100 2
3 100 0
4 100 2
5 101 2
6 101 2
7 101 0
8 101 0
I expect a result with count where csa_taken=2 for individual employee.
expected result:
emp_id count_csa_taken
100 3
101 2
I have tried the following query with a failed attempt.
Select count(employee_id) From $employeeCSA where csa_taken=2
Please suggest as I am new to sql.
If I understand you correctly you like to count all employees with a cas_taken of two. As there are multiple entries for the csa_taken for one employee you need to group them.
E.g.:
SELECT COUNT(*) FROM $employeeCSA WHERE csa_taken = 2 GROUP_BY employee_id
Please note that COUNT(*) counts the rows (not the fields).
You also need group by. Try like:
Select count(employee_id),emp_id From $employeeCSA where csa_taken=2
group by emp_id
If i understand correctly, then you can try this:
SELECT emp_id,COUNT(emp_id) from dbo.Sample WHERE csa_token = 2 GROUP BY emp_id
I have my data base like this
id project_id client_id price
1 1 1 200
2 2 1 123
3 2 1 100
4 1 1 87
5 1 1 143
6 1 1 100
7 3 3 123
8 3 3 99
9 4 3 86
10 4 3 43
11 4 3 145
12 4 3 155
Now here I want that it will sum the price columns with the same client_id.
For that I just made my query like this
Select `project_id`, SUM(`price`) FROM `table-name` GROUP BY `client_id`
This one is doing sum the price but I am getting only two project_id in the result. I want the result should be all the distinct project for the client id and the price will be summed for the group clients.
So can someone tell me how to do this? Any help and suggestions will be really appreciable. Thanks
You should not have "bare" column in a group by query that are not in the group by statement.
If you want the list of projects, you can get them in a list like this:
SELECT client_id, GROUP_CONCAT(project_id), SUM(price)
FROM table-name
GROUP BY client_id;
you only have two client that why you are getting only two record , you can group by two column,
Select `project_id`, SUM(`price`) FROM `table-name` GROUP BY `client_id`, `project_id`
I want to SUM two columns from two different database and output the highest value.
trying to figure it out since last 1day but no luck. can anyone please help?
Table 1
mid points
1 20
2 10
1 10
1 30
3 10
Table 2
mid points
1 20
2 10
1 10
2 20
1 10
3 10
so the total should be
mid points
1 100
2 40
3 20
output that i want highest total mid is 1 = 100
Try this untested query:
select mid , sum(points) from (
select mid,points from table1
union all
select mid,points from table2
) as table3
group by mid
order by sum(points) DESC
limit 1
i have two sql queries which give resultset with same attributes... i want to combine these two result sets...
my first query gives
order_id frequency
-------------------------
1 5
3 7
10 2
12 3
and second query gives
order_id frequency
-------------------------
1 3
10 2
12 8
what i finally want in result is
order_id frequency
-------------------------
1 5
3 7
10 2
12 3
1 3
10 2
12 8
here union will not work as if there is are two same tuples such as pair
10 2
it should appear twice.
please suggest some mysql query;
have you tried with UNION ALL?
Use
UNION ALL
to avoid removing dups.
You need Union All
Union must have an equal number of expressions in their target lists
Select order_id, frequency from Table_A
Union All
Select order_id, frequency from Table_B
I have the following query, in the top select statement (sum(l.app_ln_amnt)/count(l.app_ln_amnt)) works well but in the union I want to find the total of (sum(l.app_ln_amnt)/count(l.app_ln_amnt)) query from the top select statement However my solution seems to be off I need some help please
select
(sum(l.app_ln_amnt)/count(l.app_ln_amnt)),
from receipt_history l
UNION
select
SUM(sum(l.app_ln_amnt)/count(l.app_ln_amnt)),
from receipt_history l
THIS is what the initial table looks like
id app_ln_amnt
1 2
1 2
1 2
1 2
2 5
3 7
4 9
id app_ln_amnt
1 2
2 5
3 7
4 9
total 23
Now my table looks like the second one but the total is 29 and im trying to get it to be 23
You could use a subquery rather than a union
SELECT SUM(Value) FROM
(select
(sum(l.app_ln_amnt)/count(l.app_ln_amnt)) AS value
from receipt_history l ) t