getting count values in mysql - mysql

I have a table with 3 columns and looks like
branchid semesterid courseid
4 25 2
4 36 5
4 23 3
4 12 10
4 34 15
4 2 7
4 23 42
7 23 9
7 10 6
7 34 3
7 20 17
I need the count of semesterid and courseid of that branch
SELECT branchid, count(semesterid), count(courseid) WHERE branchid = 4
When I execute this query, the count(semesterid) shows wrong count and courseid shows correct count of 7

With my crystal ball I predict you need this instead:
SELECT COUNT(DISTINCT semesterid),
COUNT(DISTINCT courseid)
FROM Table1
WHERE branchid = 4;
... as the query shown should work just fine for counting the number of records (but not unique records).

try:
SELECT branchid,
count(distinct semesterid) as semesterid,
count(distinct courseid) as courseid
FROM <table>
WHERE branchid = 4
group by branchid

If you want the number of unique results you have to replace count(id) with count(distinct id)

Related

group by month returns only April for two tables

Currently I am honestly at loss what I am doing wrong. It is a rather simple query I think.
Tables:
operations:
id processedon clientid
1 2018-01-01 9
2 2018-03-16 9
3 2018-04-21 9
4 2018-04-20 9
5 2018-05-09 9
items:
id operation_id quantity unitprice
1 1 10 2
2 1 5 3
3 2 20 4
4 3 10 2
5 4 8 4
6 4 10 4
7 5 2 2
The expected result of the operation/query is:
month total_value
1 35
3 80
4 92
5 4
That is quantity * unitprice based. For some reason, it only returns month=4
SELECT
month(`operations`.`processedon`) AS `month`,
SUM((`items`.`quantity` * `items`.`unitprice`)) AS `total_value`
FROM `items`
INNER JOIN `operations` ON (`items`.`operation_id` = `operations`.`id`)
GROUP BY 'month'
ORDER BY 'month'
According to the info provided the join should be
INNER JOIN operations ON items.operation_id = operations.id
Eg
SELECT
month(`operations`.`processedon`) AS `month`,
SUM((`items`.`quantity` * `items`.`unitprice`)) AS `total_value`
FROM `items`
INNER JOIN `operations` ON `items`.`operation_id` = `operations`.`id`
GROUP BY month(`operations`.`processedon`)
ORDER BY `month`
There is no efficiency gain by using a column alias in the group by clause, I prefer to avoid using them except perhaps in the order by clause.
The following query will give you the required answer
SELECT
month(`operations`.`processedon`) AS `month`,
SUM((`items`.`quantity` * `items`.`unitprice`)) AS `total_value`
FROM items
INNER JOIN operations ON (items.operation_id = operations.id)
GROUP BY month(operations.processedon)
ORDER BY month(operations.processedon)
You need to specify month correctly since it is not an existing column.
You'll get the following result
month total_value
1 35
3 80
4 92
5 4

MySQL multiple count based on two column with multiple GROUP BY in single table

I have a query like below, it is working fine but not optimized, since it takes 1.5 sec to run. How to make this to an optimized result?
select h.keyword_id,
( select count(DISTINCT(user_id)) from history where category_id = 6
and h.keyword_id=keyword_id group by keyword_id ) as cat_6,
( select count(DISTINCT(user_id)) from history where category_id = 7
and h.keyword_id = keyword_id group by keyword_id ) as cat_7
from
history h group by h.keyword_id
History table
his_id keyword_id category_id user_id
1 1 6 12
2 1 6 12
3 1 7 12
4 1 7 12
5 2 6 13
6 2 6 13
7 2 7 13
8 3 6 13
Result:
keyword_id cat_6 cat_7
1 2 2 (unique users)
2 2 1
3 1 0
You can rewrite your query like this:
select h.keyword_id,
count(distinct if(category_id = 6, user_id, null)) as cat_6,
count(distinct if(category_id = 7, user_id, null)) as cat_7
from
history h
group by h.keyword_id
Your desired result based on the sample data is by the way false. In each keyword_id there's always just one distinct user_id.
you can see the query in action in an sqlfiddle here
For more optimization, you'd have to post the result of show create table history and the output of explain <your_query>;

mysql get the columns sum and also get the distinct values at a time

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`

MySQL: Select id if not repeated exactly 4 times

I'm wondering what is the right way to display all id_product if is NOT repeated 4 time.
This is my table:
id_product id_related
55 1
55 2
55 3
55 4
11 1
11 123
11 12
36 12
36 9
36 14
36 654
I need to find products without added 4 related products.
In this case the result i expect is 11.
Something like that
select id_product, count(*)
from <table>
group by id_product
having count(*) < 4
Following query
SELECT id_product
FROM table
GROUP BY id_product
HAVING COUNT(id_product) < 4
Select
p.id_product,
p.id_related
from product p
join
(
select
id_product,
count(id_related) as tot
from product
group by id_product
having tot <> 4
)p1
on p1.id_product = p2.id_product
If you want to filter out duplicate id_related meaning multiple occurrence of same id_related is counted as one you can use distinct as
count(distinct id_related)

Get a count of one column based on another column

Let's say I have the following data. I am trying to the number a count of each buyer_id by acct_id where the buyer_id contains 5. So for acct 51, which has five instances and contains one with 5, I was to find a count of each buyer_id (excluding 5 of course). This count should go through all acct_id's which contain a buyer_id with 5, and provide a count of all the other buyer_id's.
acct_id buyer_id message
51 5 success
51 13 fail
51 4 success
51 6 success
51 9 fail
53 6 fail
53 12 fail
53 4 success
57 6 fail
57 12 fail
57 4 success
57 5 success
So in this example I'd end up with something like below
buyer_id count(*)
4 2
5 2
6 2
9 1
12 1
13 1
Thanks for your help!
This should do the trick:
select buyer_id, count(distinct acct_id)
from Table1 a
where exists (
select * from Table1
where acct_id = a.acct_id
and buyer_id =5)
group by buyer_id
See the sqlfiddle here: http://sqlfiddle.com/#!2/0d3d2/3
I think this is what you want.
Select buyer_id, count(*)
From table
Where acct_id in (Select acct_id From table Where buyer_id = 5) --insert buyer_id param here
Group By buyer_id