How to count base on the others table sum
I have this tables
CARDS
| ID | Name |
---------------------
| 1 | Nice |
| 2 | Wow |
| 3 | Lol |
| 4 | Ohyeah |
| 5 | Olala |
VOTES
ID | *card_id* | *user_id* | vote |
-------------------------------------------------
1 | 1 | 1 | -1 |
2 | 2 | 2 | -1 |
3 | 2 | 4 | 1 |
4 | 2 | 5 | 1 |
5 | 3 | 2 | 1 |
6 | 3 | 1 | 1 |
RESULT QUERY WHAT I WANT
| card_id | total_votes |
-------------------------------
| 1 | -1 |
| 2 | 1 |
| 3 | 2 |
| 4 | 0 |
| 5 | 0 |
and this is what I've tried
SELECT c.*, COALESCE(SUM(v.vote),0) as vote_count
FROM cards c
LEFT JOIN votes v ON c.id = v.card_id
I don't where I mistake, please need help
You dont need subquery, just the group by
SQL Fiddle Demo
SELECT c.`ID`,
COALESCE(Sum(v.vote), 0) AS total_votes
FROM cards c
LEFT JOIN votes v
ON c.id = v.`card_id`
GROUP BY c.`ID`;
OUTPUT
| ID | total_votes |
|----|-------------|
| 1 | -1 |
| 2 | 1 |
| 3 | 2 |
| 4 | 0 |
| 5 | 0 |
You should select all votes and group them by card_id. There u can simply use SUM function.
SELECT c.id, COALESCE(SUM(v.vote), 0)
FROM VOTES
LEFT JOIN c ON v.card_id = c.ID
GROUP BY v.card_id
Following query will do the trick
select c.id, sum(v.vote)
from cards c, votes v
where c.id = v.card_id
group by c.id
Related
I have four tables mls_user, mls_category, mls_entry, and bonus_point.
Following are the tables:
mls_user
*--------------------------*
| id | store_id | name |
*--------------------------*
| 1 | 101 | Santosh |
| 2 | 101 | Aman |
| 3 | 102 | Ankit |
| 4 | 101 | Suman |
| 5 | 101 | Ramesh |
*--------------------------*
mls_category
*----------------------------------------*
| cat_no | store_id | cat_type | cat_val |
*----------------------------------------*
| 1 | 101 | running | 1 |
| 2 | 101 | cycling | 4 |
| 3 | 102 | running | 1 |
| 4 | 102 | swiming | 2 |
*----------------------------------------*
mls_entry
*----------------------------------------------------------*
| id | user_id | store_id | category | distance | status |
*----------------------------------------------------------*
| 1 | 1 | 101 | 1 | 5 | Approved |
| 2 | 1 | 101 | 1 | 10 | Approved |
| 3 | 1 | 101 | 2 | 50 | Approved |
| 4 | 1 | 101 | 1 | 10 | Approved |
| 5 | 2 | 101 | 1 | 5 | Approved |
*----------------------------------------------------------*
bonus_point
*---------------------------------------------------------*
| bns_id | user_id | store_id | bonus_points | bonus_type |
*---------------------------------------------------------*
| 1 | 1 | 101 | 100 | fixed |
| 2 | 2 | 101 | 25 | % |
*---------------------------------------------------------*
Now from these four tables, I want below output:
*--------------------------------------------------------------*
| name | points | bonus_points | bonus_type | Total_Points |
*--------------------------------------------------------------*
| Santosh | 25 | 100 | fixed | 125 |
| Aman | 5 | 25 | % | 30 |
| Suman | 0 | 0 | - | 0 |
| Ramesh | 0 | 0 | - | 0 |
| Ankit | 0 | 0 | - | 5 |
*--------------------------------------------------------------*
I am using the following query but this query only shows the value for Santosh and aman
SELECT u.id, u.name, COALESCE(t1.points, 0) AS points, b.bonus_points
FROM mls_user u
LEFT JOIN
(SELECT e.user_id, SUM(e.distance / c.cat_value) AS points
FROM mls_entry e
INNER JOIN mls_category c ON e.store_id = c.store_id AND e.category = c.cat_no
WHERE e.status='approved' AND e.store_id = '101'
GROUP BY e.user_id ) t1 ON u.id = t1.user_id
INNER JOIN bonus_points b ON b.user_id = u.id
WHERE u.store_id = '101'
ORDER by points DESC
So where I am doing wrong in my query, Please help me in this.
In your bonus_points table you have only two entry related to user 1 and 2 (Santosh and aman) if you use INNER JOIN then you obtain only these user
in this case you need left JOIN for bonus_points Too
SELECT u.id, u.name, COALESCE(t1.points, 0) AS points, ifnull(b.bonus_points,0) bonus_point
FROM mls_user u
LEFT JOIN (
SELECT e.user_id, SUM(e.distance / c.cat_value) AS points
FROM mls_entry e
INNER JOIN mls_category c ON e.store_id = c.store_id
AND e.category = c.cat_no
WHERE e.status='approved'
AND e.store_id = '101'
GROUP BY e.user_id
) t1 ON u.id = t1.user_id
LEFT JOIN bonus_points b ON b.user_id = u.id
WHERE u.store_id = '101'
ORDER by points DESC
I have two table one store data child and parent hierarchy and other paths and descendant
+----------+------------+-----------+
| userid | parent | price |
+----------+------------+------------
| 1 | null | 20 |
| 2 | 1 | 20 |
| 3 | 1 | 20 |
| 4 | 2 | 20 |
| 5 | 2 | 20 |
| 6 | 3 | 20 |
| 7 | 4 | 20 |
+----------+------------+-----------+
I need to get all userid with parent 1 then get descendant in other table and group by userid sum prices
+-------------+---------------+-------------+
| ancestor_id | descendant_id | path_length |
+-------------+---------------+-------------+
| 1 | 1 | 0 |
| 1 | 2 | 1 |
| 1 | 3 | 1 |
| 1 | 4 | 2 |
| 1 | 5 | 2 |
| 1 | 6 | 2 |
| 1 | 7 | 3 |
| 2 | 2 | 0 |
| 2 | 4 | 1 |
| 2 | 5 | 1 |
| 2 | 7 | 2 |
| 3 | 3 | 0 |
| 3 | 6 | 1 |
| 4 | 4 | 0 |
| 4 | 7 | 1 |
| 5 | 5 | 0 |
| 6 | 6 | 0 |
| 7 | 7 | 0 |
+-------------+---------------+-------------+
I have query it sum all childs together
select
sum(b.price)
from webineh_prefix_nodes_paths_tmp a
join webineh_prefix_nodes_tmp b on (b.userid = a.descendant_id)
where a.ancestor_id = 1
this work fine but total sum parent 1
I need to show bellow result for child direct (2,3)
+----------+------------+-
| userid | total |
+----------+------------+
| 2 | 80 |
| 3 | 40 |
+----------+------------+
also in create sqlfiddle my question http://sqlfiddle.com/#!9/9415ed/2
Try this;)
select ancestor_id as userid, sum(b.price) as total
from webineh_prefix_nodes_paths_tmp a
join webineh_prefix_nodes_tmp b
on b.userid = a.descendant_id
where a.ancestor_id in (select userid from webineh_prefix_nodes_tmp where parent = 1)
group by ancestor_id
SQLFiddle Demo
Edited
select ancestor_id as userid, sum(b.price) as total
from webineh_prefix_nodes_paths_tmp a
join webineh_prefix_nodes_tmp b
on b.userid = a.descendant_id
inner join webineh_prefix_nodes_tmp c
on a.ancestor_id = c.userid
and c.parent = 1
group by ancestor_id
SQLFiddle Demo
try this
select sum(b.price) from webineh_prefix_nodes_paths_tmp a join webineh_prefix_nodes_tmp b on (b.userid = a.descendant_id) where a.ancestor_id in ( 1,2,3) GROUP by ancestor_id
I've tried the following queries but unfortunately they don't work :(.
Worth mentioning that each customer has more than one CustomerUsers
select (a.TotalJobs / b.DaysActive) from
(select count(jr.id) as TotalJobs
from jobrequests jr, customers c, customerusers cu
where jr.customeruserid=cu.id
and cu.customerid=c.id
group by c.name) as a,
(select datediff(curdate(), from_unixtime(c.CreationTime)) as DaysActive
from customers c
group by c.name) as b
Please see below the tables
Jobs:
+----+--------------+
| ID | JobRequestID |
+----+--------------+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 1 |
| 5 | 1 |
| 6 | 2 |
| 7 | 2 |
| 8 | 3 |
| 9 | 3 |
| 10 | 3 |
| 11 | 4 |
| 12 | 4 |
| 13 | 5 |
| 14 | 5 |
| 15 | 6 |
| 16 | 7 |
| 17 | 8 |
| 18 | 8 |
| 19 | 9 |
| 20 | 10 |
+----+--------------+
JobRequests:
+----+---------------+
| ID | CustomeUserID |
+----+---------------+
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
| 4 | 2 |
| 5 | 2 |
| 6 | 3 |
| 7 | 4 |
| 8 | 4 |
| 9 | 4 |
| 10 | 5 |
| 11 | 5 |
| 12 | 5 |
| 13 | 6 |
| 14 | 6 |
| 15 | 7 |
+----+---------------+
CustomerUsers:
+----+------------+
| ID | CustomerID |
+----+------------+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 2 |
| 6 | 2 |
| 7 | 2 |
| 8 | 3 |
| 9 | 3 |
| 10 | 4 |
+----+------------+
Customers:
+----+------+--------------+
| ID | Name | CreationTime |
+----+------+--------------+
| 1 | a | 1415814194 |
| 2 | b | 1415814194 |
| 3 | c | 1415986994 |
| 4 | d | 1415986994 |
+----+------+--------------+
For the moment it returns 16 results (4X4), dividing each result from 1st sub-query to each result from the 2nd one (each of these sub-queries return 4 results). Can anyone please help me to get this to divide only 1 result from sub-query 1 to it's corespondent from sub-query 2?
Thank you in advance.
I suspect that you can do what you want this a query like this:
select c.name, count(*) / (datediff(curdate(), from_unixtime(c.CreationTime))
from customerusers cu join
jobrequests jr
on jr.customeruserid = cu.id join
customers c
on cu.customerid = c.id
group by c.name;
I don't see why you need two subqueries for this.
I'm guessing you need to join your results together -- as currently written, you're producing a cartesian product.
Try something like this adding c.id to each subquery (it's better to group by it presumably rather than the name):
select (a.TotalJobs / b.DaysActive)
from (
select c.id,
count(jr.id) as TotalJobs
from jobrequests jr
join customers c on jr.customeruserid=cu.id
join customerusers cu on cu.customerid=c.id
group by c.id) a join (
select c.id,
datediff(curdate(), from_unixtime(c.CreationTime)) as DaysActive
from customers c
group by c.id) b on a.id = b.id
Please note, I've updated your syntax to use the more standard join syntax.
I've this query.
SELECT
d.gid, d.channel_id
FROM channels, (
SELECT
gigs.id as gid, gigs.channel_id
FROM gigs
ORDER BY gigs.id DESC
LIMIT 1
) as d
WHERE d.channel_id = channels.id
LIMIT 10
I was hoping to get one gid for each channels.id, but instead it only returns one column. Why is that?
Gigs table
+---------+------------+
| id | channel_id |
+---------+------------+
| 150661 | 6 |
| 745797 | 6 |
| 483996 | 6 |
| 3407209 | 6 |
| 1175022 | 5 |
| 3238459 | 6 |
| 711413 | 5 |
| 524758 | 5 |
| 154764 | 5 |
| 1594779 | 5 |
| 1659091 | 2 |
| 1086768 | 6 |
| 1161320 | 8 |
| 467394 | 5 |
| 2877040 | 8 |
| 2705821 | 6 |
| 1881747 | 5 |
| 901902 | 1 |
| 143322 | 6 |
| 671658 | 1 |
+---------+------------+
Channels table
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 10 |
+----+
Try this one,
SELECT a.id, b.max_ID
FROM channels a
INNER JOIN
(
SELECT channel_ID, MAX(id) max_ID
FROM gigs
GROUP BY channel_ID
) b ON a.id = b.channel_ID
SQLFiddle Demo
basically, I used INNER JOIN to show only id from channels table that has atleast on record on gigs table. If you want to show list even if there is no match on gigs table then you should use LEFT JOIN instead.
SELECT id, (SELECT id FROM Gigs WHERE channel_id = c.id ORDER BY id DESC LIMIT 1) As GigID
FROM channels c
I'm new in SQL queries. I have a problem with the query.
I have table books:
+----+-------+--------+-----------+
| id | title | author | publisher |
+----+-------+--------+-----------+
| 1 | Book1 | 1 | 1 |
| 2 | Book2 | 1 | 2 |
| 3 | Book3 | 2 | 1 |
| 4 | Book4 | 2 | 2 |
| 5 | Book5 | 2 | 3 |
+----+-------+--------+-----------+
And I'm have another table, which contains copies of books with given book_id.
+----+---------+
| id | book_id |
+----+---------+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 2 |
| 6 | 3 |
| 7 | 4 |
| 8 | 4 |
| 9 | 5 |
+----+---------+
All I need is to merge these two tables into one to have table sorted by amount of book copies.
I found solution to get table sorted by amount of book copies:
select book_copies.book_id, count(*) total_count
from book_copies
group by book_id
having count(*) > 0
order by count(*) desc;
+---------+-------------+
| book_id | total_count |
+---------+-------------+
| 1 | 3 |
| 2 | 2 |
| 4 | 2 |
| 3 | 1 |
| 5 | 1 |
+---------+-------------+
Now I don't know how to merge them.
I've tried like this:
select books.title from books
left join
(select book_copies.book_id, count(*) total_count
from book_copies
group by book_id
having count(*) > 0
order by count(*) desc)
as total_table on books.id = total_table.book_id;
But all I get was this:
+-------+
| title |
+-------+
| Book1 |
| Book2 |
| Book3 |
| Book4 |
| Book5 |
+-------+
Could you help me please?
EDIT: by merging I meant smth like this:
+-------+--------+-----------+-----+
| title | author | publisher | tot |
+-------+--------+-----------+-----+
| Book1 | 1 | 1 | 3 |
| Book2 | 1 | 2 | 2 |
| Book4 | 2 | 2 | 2 |
| Book3 | 2 | 1 | 1 |
+-------+--------+-----------+-----+
Many thanks to #Marco for the answer!
Dmitriy
I think you could try:
SELECT b.title, b.author, b.publisher, COUNT(bc.book_id) AS tot
FROM books b LEFT JOIN book_copies bc
ON b.id = bc.book_id
GROUP BY b.id
EDITED:
If you want sort, you can try
SELECT * FROM
(SELECT b.title, b.author, b.publisher, COUNT(bc.book_id) AS tot
FROM books b LEFT JOIN book_copies bc
ON b.id = bc.book_id
GROUP BY b.id) g
ORDER BY g.tot DESC