I have a table like this:
ITEM PICTURE CATEGORY STOCK
1 Mickey Shirt 3
2 Mickey Jacket 2
3 Donald Jacket 0
4 Donald Shirt 6
5 Donald Hoodie 1
6 Mickey Hoodie 3
7 Goofy Shirt 1
8 Goofy Hoodie 2
9 Goofy Jacket 3
The ITEM column is auto increment
I need an output like this:
PICTURE HOODIE JACKET SHIRT
Donald 1 0 6
Goofy 2 3 1
Mickey 3 2 3
Any idea how I can produce that output? Thank you
You can use CASE expression.
Query
select PICTURE,
sum(case CATEGORY when 'Hoodie' then STOCK else 0 end) as `HOODIE`,
sum(case CATEGORY when 'Jacket' then STOCK else 0 end) as `JACKET`,
sum(case CATEGORY when 'Shirt' then STOCK else 0 end) as `SHIRT`
from your_table_name
group by PICTURE;
Related
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
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.
just think I have a table like shown below
**id** **product**
2 chocolate
1 chocolate
2 pepsi
3 fanta
2 pepsi
4 chocolate
5 chips
3 pizza
1 coke
2 chips
6 burger
7 sprite
0 pepsi
and want to arrange the above table in the manner shown below using only mysql
**id** **product**
0 pepsi
1 chocolate, coke
2 chocolate,fanta,chips
3 fanta,pizza
4 chocolate
5 chips
6 burger
7 sprite
how do I do it?
select id, group_concat(distinct product) as products
from your_table
group by id
order by id
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.
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