count total volume by category - php

I have 3 tables
Company_categories
companies
daily_rates
I want to count total volume industry wise of all the company exist within the category of companies
for example category A contains 3 companies and category B contains 5 companies so I want to sum total volume of all 3 companies in category A and so on for all categories I tried to do so but I am confused how to do with 3rd table as of I am easily been able to count the companies contains in category but not sure how to count the volume of all companies exist in a category
my table structure
company_categories
id name
+------------+----------+
| 1 | A |
|------------|----------|
| 2 | B |
|------------|----------|
companies
id name category
+------------+----------+-----------+
| 1 | co 1 | 1 |
|------------|----------|-----------|
| 2 | co 2 | 2 |
|------------|----------|-----------|
| 3 | co 3 | 1 |
|------------|----------|-----------|
daily_stock_rates
id traded_volume company_id
+------------+------------------+---------------+
| 1 | 40 | 1 |
|------------|------------------|---------------|
| 2 | 80 | 2 |
|------------|------------------|---------------|
| 3 | 30 | 3 |
|------------|------------------|---------------|
here is my code
$sql = mysqli_query($connect, "SELECT c.id category_id, c.name category_name, com.id, com.category count( dsr.total_traded_volume ) total_volume
FROM company_categories c
INNER JOIN companies com ON c.id = com.category
LEFT JOIN daily_stock_rates dsr ON com.id = dsr.company_id
GROUP BY com.category
ORDER BY total_volume DESC LIMIT 10");
while($data = mysqli_fetch_assoc($sql)) {
echo $data['category_name'] . ": .".$data['total_volume'];
echo "<br />";
}
Can Anyone help me out

joint twice to get all the rates related to the category
SELECT cat.name, SUM(rat.traded_volume) volume
FROM company_categories cat
JOIN companies comp ON comp.category = cat.id
JOIN daily_stock_rates rat ON rat.company_id = comp.id
GROUP BY cat.name
ORDER BY volume DESC
LIMIT 10
Most important diff to your query:
you need SUM(), not COUNT()
select only what you asked: volume by category. You cannot select
names of companies alongside (what company you would want to see
there anyway)

I don't think it's the join you got wrong, it's the select list and the group by clause.
First of all, if you want the total of the volume, then use sum(), not count(). Also, do not include so many fields in the select list if you want the total by category:
SELECT c.id category_id, c.name category_name, sum( dsr.total_traded_volume ) total_volume
FROM company_categories c
LEFT JOIN companies com ON c.id = com.category
LEFT JOIN daily_stock_rates dsr ON com.id = dsr.company_id
GROUP BY c.id
ORDER BY total_volume DESC LIMIT 10

Related

MySQL Join three tables and display 0 if null

I have three tables:
person_table
id| name | gender
1 | Joe | male
2 | Jane |female
3 | Janet | female
4| Jay | male
etc...
product_table
id| name
1 | magazine
2 | book
3 |paper
4 | novel
etc...
**person_product
person_id| product_id | quantity
1 | 1 | 1
1 | 3 | 3
2 | 3 | 1
4 | 4 | 2
etc...
I have tried to make a query that will return a table like this:
person_id| person_name | product_name| quantity
but i can't make it so that if lets say John has no books, it should display
(johns id) John|book|0
instead of just skipping this line.
Where did i go wrong?
here is what i managed to come up with:
SELECT p.*, f.name, l.quantity
FROM person_product AS l
INNER JOIN people_table AS p ON l.person_id=p.id
INNER JOIN product_table AS f ON l.product_id=f.id
ORDER BY id`
It seems that you're generating a report of all people, against all products with the relevant quantity; on a large data set this could take a while as you're not specifically joining product to person for anything other than quantity:
SELECT
p.id,
p.name,
p.gender,
f.name,
IFNULL(l.quantity,0) AS quantity
FROM person_table AS p
JOIN product_table AS f
LEFT JOIN person_product AS l
ON l.person_id = p.id
AND l.product_id = f.id
ORDER BY p.id, f.name
Which results in:
Is that more-or-less what you're after?
you need to start with people_table than using left join you need to bring other table data.
as you need 0 value if null than you can use function IFNULL
SELECT p.*, f.name, IFNULL(l.quantity,0)
FROM people_table AS p
LEFT JOIN person_product AS l ON l.person_id=p.id
LEFT JOIN product_table AS f ON l.product_id=f.id
ORDER BY p.id
if has no book shouldn't appear in the table , try this (easy to understand) :
SELECT NAME
,'0'
,'0'
FROM person_table
WHERE id NOT IN (
SELECT person_id
FROM person_product
)
UNION
SELECT person_id
,product_id
,quantity
FROM person_product;

How to get a list of similar items

I have 4 tables:
items
+----+------+---------+-----+
| id | name | city_id | ... |
+----+------+---------+-----+
attributes
+----+------+-----+
| id | name | ... |
+----+------+-----+
item_attribute
+----+---------+--------------+
| id | item_id | attribute_id |
+----+---------+--------------+
city
+----+------+-----+
| id | name | ... |
+----+------+-----+
Items and attributes have relations many-to-many.
Item is located only in one city one-to-many
Question:
I'm using php (Laravel). How can I get Items list (with LIMIT) for one Item with similar attributes in one city? Attribute list is never equals for 2 items.
Is it possible to do with MySQL query?
Example:
| ItemName | Attributes | City |
+----------+-----------------------+------+
| Alpha | one, two, three, four | NY |
| Beta | five, six, seven | NY |
| Gamma | one, three, seven | NY |
| Delta | one, six, eight | CA |
| Epsilon | two, three, four | NY |
| Zeta | ten, nine | NY |
I want to choose similar items for Alpha, they will be: Gamma, Epsilon because they have similar attributes.
Delta won't be chosen, because it's located in another city.
If you have both the item_id and the city_id to pass in:
SELECT i.name,
GROUP_CONCAT(a.name) attributes,
c.name
FROM items i
JOIN city c
ON c.id = i.city_id
JOIN item_attribute ia
ON ia.item_id = i.id
AND EXISTS (
SELECT 1
FROM item_attribute ia1
JOIN item_attribute ia2
ON ia2.attribute_id = ia1.attribute_id
AND ia2.item_id = ia.item_id
WHERE ia1.item_id = :item_id /* Pass in item id variable */
)
JOIN attributes a
ON a.id = ia.attribute_id
WHERE i.city_id = :city_id /* Pass in city id variable */
GROUP BY i.name, c.name
If you just want to pass the example item id: (A little bit sloppy, but should work)
SELECT i.name,
GROUP_CONCAT(a.name) attributes,
c.name
FROM items base
JOIN items i
ON i.city_id = base.city_id
JOIN city c
ON c.id = i.city_id
JOIN item_attribute ia
ON ia.item_id = i.id
AND EXISTS (
SELECT 1
FROM item_attribute ia1
JOIN item_attribute ia2
ON ia2.attribute_id = ia1.attribute_id
AND ia2.item_id = ia.item_id
WHERE ia1.item_id = base.id
)
JOIN attributes a
ON a.id = ia.attribute_id
WHERE base.id = :item_id /* Pass in item id variable */
GROUP BY i.name, c.name
** UPDATE **
Ordering:
...
JOIN (
SELECT ia2.item_id, COUNT(*) count
FROM item_attribute ia1
JOIN item_attribute ia2
ON ia2.attribute_id = ia1.attribute_id
AND ia2.item_id = ia1.item_id
/* AND ia2.id != ia1.id /* If you don't want the original item */
WHERE ia1.item_id = base.id
GROUP BY ia2.item_id
) similar
ON similar.id = ia.item_id
...
ORDER BY similar.count DESC
You can perform INNER JOINS in all
SELECT I.name,I_A.name,city.name FROM attributes as A
INNER JOIN item_attribute as I_A ON I_A.attribute_id = A.id
INNER JOIN city ON I_A.id = city.id
INNER JOIN items as I ON I.id = I_A.item_id
WHERE <Your condition>
To get comma separated values you can refer here
Let me know if I am not getting your point.

How do I select all categories per product and returning this in one row per product?

I'm having a bit of a brain freeze now, because this should be very easy (I think).
I have three tables:
products list
categories list
category_map (links product and category)
I want to list products with its categories (names). One product may have many categories.
// This query
SELECT a.id, a.name, b.category
FROM sl_link_product_category c
LEFT JOIN sl_product a ON c.fk_productID = a.id
LEFT JOIN sl_category b ON c.fk_categoryID = b.id
WHERE c.fk_categoryID = b.id
ORDER BY a.id
// Gives me this result
| 1 | product 1 | cat 1 |
| 1 | product 1 | cat 2 |
| 1 | product 1 | cat 3 |
| 2 | product 2 | cat 2 |
| 2 | product 2 | cat 4 |
Now, in the final result set, I want an array with products where each product has an array of categories.
My question is; Do I have to work with the result set in PHP to achieve my goal? Or is it possible in MySQL to put all categories (comma separated) in one variable / column?
The GROUP_CONCAT function is what you're looking for:
SELECT a.id, a.name, GROUP_CONCAT(b.category)
FROM sl_link_product_category c
LEFT JOIN sl_product a ON c.fk_productID = a.id
LEFT JOIN sl_category b ON c.fk_categoryID = b.id
GROUP BY a.id, a.name
ORDER BY a.id

Counting the most amount of items assigned to a user using greatest

I am trying to query 5 separate tables in my mysql database, and display the actor with the most amount of items assigned to them. The table structures are as follows;
item
itemid | item | description | brand | date | time | path |
actor
actorid | name | actorthumb | bio |
brand
brandid | brandname | description | image |
movie
movieid | title | genre | year | moviethumb | synopsis|
request
requestid | userid | itemid | brandid | movieid | actorid | content | requestdate |
I presume I need to join the request, actor and items table together, using COUNT function count how many items are assigned to an actor, then use GREATEST to display the actor with the highest amount of items assigned to them?
The query to join all the tables is
$query = "SELECT greatest i.*, a.*, b.*, m.*, r.* FROM item AS i, actor AS a, brand AS b, movie AS m, request AS r
WHERE r.itemid = i.itemid
AND r.actorid = a.actorid
AND r.brandid = b.brandid
AND r.movieid = m.movieid";
Please confirm the best way to do the above?
SELECT a.actorid, a.name
FROM request r
INNER JOIN actor a
ON r.actorid = a.actorid
GROUP BY a.actorid, a.name
ORDER BY COUNT(DISTINCT r.itemid) DESC
LIMIT 5
The query to join all the tables is
$query = "SELECT i.*, a.*, b.*, m.*, r.* FROM item AS i, actor AS a, brand AS b, movie AS m, request AS r
WHERE r.itemid = i.itemid
AND r.actorid = a.actorid
AND r.brandid = b.brandid
AND r.movieid = m.movieid
ORDER BY count(i.*) DESC";

Multiple count values on one column

I want to query my database and get counts of the occurences of each id in a single column.
The two tables related to this query are:
categories: this table has a list of all categories with titles
id | title
----------
1 | project
2 | tech
3 | other
category_news: this table is used to assign news items to categories, each news item can be in multiple categories
id | category_id | news_id
--------------------------
1 | 1 | 2
2 | 1 | 5
3 | 1 | 3
4 | 2 | 4
5 | 3 | 2
6 | 3 | 1
I would like to get a result of (doesnt have to be an array, I can sort out the results of a query, just using an array below for an example of the returned results):
array(
'project' => 3
'tech' => 1
'other' => 2
);
Where the key is the category title and the value is the total number of news items in each category. I know that I could do it with a query for each category:
select count(cn.category_id), c.title from category_news cn join categories c on c.id = cn.category_id where category_id = 1
But my question is, can I do it with one query to get all the counts? As if I have 100 categories thats 100 queries my way and I would love to do it in one.
Any help would be much appreciated
Regards
Luke
Try this:
SELECT c.title, count(cn.category_id)
FROM categories c
LEFT JOIN categories_news cn
ON c.id = cn.category_id
GROUP BY c.title
This will work(tested):
SELECT COUNT(cn.category_id), c.title
FROM category_news cn
JOIN categories c ON c.id = cn.category_id
GROUP BY cn.category_id
Yes, you can. You have to use a query like the following (tested)
SELECT count( * ) , c.title
FROM category_news cn
JOIN categories c ON c.id = cn.category_id
WHERE 1
GROUP BY cn.category_id

Categories