mysql compare columns in same table - mysql

I have a table in a mysql db as
id fruit number_eaten day
----------------------------------
1 apple 2 1
2 banana 1 1
3 orange 3 2
4 apple 1 2
5 banana 2 3
6 peach 1 3
I'm trying to figure out how to select such that I can compare how many was eaten per day and place into a spreadsheet so i get
fruit number_eaten_day_1 number_eaten_day_2 number_eaten_day_3
------------------------------------------------------------------------
apple 2 1 0
banana 1 0 2
orange 0 3 0
peach 0 0 1

it's easier to have separate row per fruit and day with summed value number_eaten:
select fruit, day, sum(number_eaten)
from fruits_eaten_by_day
group by fruit, day
but it should also be possible to have exact result you need by doing this:
select
fruit,
sum(if(day=1, number_eaten, 0)) as number_eaten_day_1,
sum(if(day=2, number_eaten, 0)) as number_eaten_day_2,
sum(if(day=3, number_eaten, 0)) as number_eaten_day_3
from fruits_eaten_by_day
group by fruit

Related

how to display result with order by based parent and childs in one table

I got problem with how to display query result based by parent and child on one table
data for example:
ID
CATEGORY
PARENT
ROOT
1
FOOD & DRINK
0
NULL
2
CLOTHES
0
NULL
3
FRUIT
1
1
4
DESERT
1
1
5
PANTS
2
2
6
T-SHIRT
2
2
7
APPLE
3
1
8
BANANA
3
1
9
ICE CREAM
4
1
10
SHORTS
5
2
11
LONG T-SHIRT
6
2
and I want to sort and display to this:
ID
CATEGORY
PARENT
ROOT
2
CLOTHES
0
NULL
5
PANTS
2
2
10
SHORTS
5
2
6
T-SHIRT
2
2
11
LONG T-SHIRT
6
2
1
FOOD & DRINK
0
NULL
4
DESERT
1
1
9
ICE CREAM
4
1
3
FRUIT
1
1
7
APPLE
3
1
8
BANANA
3
1
is there any query script to display like above ?
here my code and have no luck
select
id, category, parent, root
from
tbl_menu
order by
coalesce(root,parent,id),
category
and thanks for help :)
I understand that you want to order the tree nodes depth-first. One option uses a recursive query to generate the path to each node, that you can then use for ordering:
with recursive cte as (
select t.*, cast(id as char) as path from mytable t where root is null
union all
select t.*, concat(c.path, '/', t.id)
from cte c
inner join mytable t on t.parent = c.id
)
select *
from cte
order by path

Mysql Database: Joins sums and group by

I have two tables OrderUpdate and DailyProduction
ORDER_UPDATE
itemid packages ordercode
1 10 ORANGE
2 20 ORANGE
3 40 ORANGE
1 25 APPLE
2 50 APPLE
3 100 APPLE
DAILY_PRODUCTION
date itemid packages ordercode
28-Sep 1 5 ORANGE
20-Sep 1 2 ORANGE
1-Sep 3 4 ORANGE
1-Sep 1 5 APPLE
15-Sep 2 5 APPLE
30-Sep 3 1 APPLE
And I want following as a result
itemid packages ordercode ready remaining
1 10 ORANGE 7 3
2 20 ORANGE 0 20
3 40 ORANGE 4 36
1 25 APPLE 5 20
2 50 APPLE 5 45
3 100 APPLE 1 99
If I follow you correctly, you can left join table order_update with an aggregate query that sums the packages for each (itemid, ordercode) in table daily_production:
select ou.*, coalesce(dp.ready, 0), ou.packages - coalesce(dp.ready, 0) reamining
from order_update ou
left join (
select itemid, ordercode, sum(packages) ready
from daily_production
group by itemid, ordercode
) dp on dp.itemid = ou.itemid and dp.ordercode = ou.ordercode

How to get total of parents categories?

I have two tables, the first is the following
"Categories"
id name parent_id
1 Food NULL
2 Vegetable 1
3 Fruit 1
4 Tomatoes 2
5 onion 2
6 bananas 3
7 grapes 3
the second table is the following
"amounts"
id amount category_id
1 100 4
2 50 6
3 25 5
4 100 7
5 50 4
6 25 7
Currently categories table are three levels and I want to return all
the Categories along with total columns:
Means that sum of the category of level 2 is the total of level 3 and
the total of level 2 should was the sum of level 1. I would really
appreciate some help Beforehand thank you very much.

How to check for one value in multiple rows

Whats the best way to check if different groups of rows in a table with the same GroupID such as different teams have a SINGLE captain? Captain Identifier for example could be '10', so its crucial that it goes through multiple records with the same groupID and checks to see if theres ONLY ONE record with the positionID as '10'. I need it to do it for all teams, i.e all groupID's
-------------------------------------------------
ID | Group ID | Name | Position|
-------------------------------------------------
1 1 John 3
2 1 jim 3
3 1 Hahn 4
4 1 Mary 4
5 1 Moe 4
6 1 Charlie 10
7 2 taylor 4
8 2 Geoff 4
9 2 adam 4
10 2 cam 10
11 3 sharon 2
12 3 tony 4
13 3 steve 3
14 3 eve 4
15 3 gwen 10
--------------------------------
So what I need it to do is check that every groupID only had ONE 10 as the position.
Thanks in advance guys. Check out the image link at the bottom.
im using mysql btw
Sorry if this is badly described
If I understood you - I think you need something like :
select groupId, sum(case when positionID='10' then 1 else 0 end) as captains
from tbl_name
group by groupID
having captains = 1
am I close???
If I understood you correctly, you want an indication that will tell you whether the groupID had more then one captain.
So what you need is this:
select groupID,case when cnt = 1 then 1 else 0 end as IND_ONE_CAPTAIN from (
select groupID, sum(case when positionid = '10' then 1 else 0 end) as cnt
from players
group by groupID)
Now IND_ONE_CAPTAIN columns consist 1 or 0, 1 is for the group id only had 1 captain, and 0 is when they had more.

SQLite COUNT and selecting from multiple tables

Consider the database (made up for example):
Table 'owner'
id name
1 john
2 andrew
3 peter
Table 'duck'
id name ownerID
1 duck1 1
2 duck2 1
3 duck3 1
4 duck4 2
5 duck5 2
6 duck6 3
7 duck7 3
8 duck8 1
9 duck9 3
10 duck10 3
11 duck11 1
12 duck12 2
Table 'food'
id name type duckID
1 beef meat 4
2 grass veg 8
3 lemon fruit 5
4 apple fruit 3
5 pizza snack 7
I wish to write some SQL that for each OWNER, COUNT the number of ducks which eat some kind of food. For example for owner John, there would be 2 ducks (ducks 3 and 8).
So far I am writing this code:
select owner.name, count(duck.ownerID)
from duck, food, owner
where duck.id == food.duckID
and duck.ownerID == owner.id;
I am getting the result:
Peter | 5
Any suggestions are appreciated.
This is done with an group by clause:
select owner.name, food.name, count(duck.id)
from duck, food, owner
where duck.id == food.duckID
and duck.ownerID == owner.id
group by owner.name, food.name;
This query gives you one row for each combination of owner name and food name with the number of the owner's ducks eating this food.