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)
Related
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
I have a table structured like this:
user_id saved_id
1 2
1 34
1 36
2 489
2 14
3 731
4 48
5 901
6 234
6 9
6 64
What I would like to do is first count how many saved ids each user has, and then group these results so that I know how often each total_saves occurs.
This is what I currently have:
SELECT user_id, count(*) as total_saves FROM table GROUP BY user_id ORDER BY total_saves DESC
Which gives me
user_id total_saves
1 3
6 3
2 2
3 1
4 1
5 1
What I would like to have is this:
total_saves count
3 2
2 1
1 3
Can't get my head around how to group the total_saves that I already have. I tried GROUP BY total_saves but that doesn't work.
Use two aggregations:
select total_saves, count(*) as cnt
from (select user_id, count(*) as total_saves
from t
group by user_id
) t
group by total_saves;
Use Subquery
select total_saves, count(total_saves) as count
from (select user_id, count(*) as total_saves
from table
group by user_id
) a
group by total_saves order by total_saves;
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 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)
i have a problem concerning a select query in MYSQL
i have two different tables and i want to obtain a certain result
i used COUNT method which gave me only the results (>=1)
But in reality , i want to use all counts with zero included how to do it?
My query is:
SELECT
first.subscriber_id,
second.tag_id,
COUNT(*)
FROM
content_hits first
JOIN content_tag second ON first.content_id=second.content_id
GROUP BY
second.Tag_id,first.Subscriber_id<br>
First table:Content_hits
CONTENT_ID SUBSCRIBER_ID
30 1
10 10
34 4
32 2
40 3
28 3
30 6
31 8
12 3
Second table:Content_tag
CONTENT_ID TAG_ID
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
11 2
12 2
13 2
14 2
Result but incomplete For example:Subsrciber6 for tag_id=1 should have a count(*)=0
subscriber_id tag_id COUNT(*)
1 1 4
2 1 7
3 1 2
4 1 1
5 1 3
7 1 2
8 1 1
9 1 1
10 1 3
1 2 2
2 2 3
3 2 2
Now that you have further elaborated on what you actually want to achieve, it can be seen that the problem is much more complex. You actually want all combinations of subscriber_id and tag_id, and then count the number of actual entries in the joined table product. whew. So here goes the SQL:
SELECT combinations.tag_id,
combinations.subscriber_id,
-- correlated subquery to count the actual hits by tag/subscriber when joining
-- the two tables using content_id
(SELECT count(*)
FROM content_hits AS h
JOIN content_tag AS t ON h.content_id = t.content_id
WHERE h.subscriber_id = combinations.subscriber_id
AND t.tag_id = combinations.tag_id) as cnt
-- Create all combinations of tag/subscribers first, before counting anything
-- This will be necessary to have "zero-counts" for any combination of
-- tag/subscriber
FROM (
SELECT DISTINCT tag_id, subscriber_id
FROM content_tag
CROSS JOIN content_hits
) AS combinations
Not sure, but is this what you want?
SELECT first.subscriber_id, second.tag_id, COUNT(*) AS c
FROM content_hits first JOIN content_tag second ON first.content_id=second.content_id
GROUP BY second.Tag_id,first.Subscriber_id HAVING c = 0