I have a table equipment with columns name, description, status
This is how my table looks like when I display all rows:
id name description status
1 shovel shiny shovel 1
2 shovel shiny shovel 1
3 shovel shiny shovel 0
4 hammer big hammer 1
5 hammer big hammer 0
This is the SQL statement I have right now:
SELECT equipment.name, equipment.description, COUNT(*) AS stock
FROM equipment
GROUP BY equipment.name
and it shows the following:
name description stock
shovel shiny shovel 3
hammer big hammer 2
What I want is to display another column showing the number of equipment that has a status of 1
name description stock available
shovel shiny shovel 3 2
hammer big hammer 2 1
You are almost there: all you need is to sum up the status fields, like this:
SELECT
equipment.name,
equipment.description,
COUNT(*) AS stock,
SUM(status) as available
FROM equipment
GROUP BY
equipment.name,
equipment.description
The trick here (that will work in SQL Server and MySQL) is to use conditional SUM() instead of COUNT()
SELECT ..., SUM(WHEN CASE status = 1 THEN 1 ELSE 0 END) as available
If status can be only 1 or 0 then you can just total it:
SELECT ..., SUM(status) as available
Related
I'm trying to join a few tables in MySQL. Our setup is a little unique so I try to explain as good as I can.
I have a table 'INVENTORY' that represents the current items on stock.
These items are stored in a table 'COMPONENT'
Components are being used in installations.
Every user can have multiple installations and the same component can be used in multiple installation as well.
To uniquely map a component to an installation, it can be assigned to a PRODUCT. a product as has a 1-1 relationship with an installation. A component is not directly related to an installation
To finally assign a product to a specific installation a mapping table COMPOMENT_PRODUCT is used.
Example:
A component is like a part, lets say a screw. This screw is used in a computer. The very same screw can be used on multiple computers. But each computer can only be used on one specific installation.
TABLE COMPOMENT_PRODUCT
COMPOMENT_ID PRODUCT_ID
1 1
1 2
2 1
2 2
So we have the components C1 and C2 relevant for two installations.
TABLE INVENTORY
COMPOMENT_ID INSTALLATION_ID ON_STOCK
1 1 5
1 2 2
What I want to achieve
Now, I want to retrieve the inventory state for all components. But, not every component has an inventory record. In these cases, the ON_STOCK value from the inventory shall be NULL
That means, for this example I'd expect the following results
COMPOMENT_ID PRODUCT_ID ON_STOCK
1 1 5
1 2 2
2 1 NULL
2 2 NULL
But executing this query:
SELECT DISTINCT
COMPONENT_PRODUCT.COMPONENT_ID,
COMPONENT_PRODUCT.PRODUCT_ID,
INVENTORY.ON_STOCK
FROM INVENTORY
RIGHT JOIN COMPONENT_PRODUCT ON COMPONENT_PRODUCT.COMPONENT_ID =
INVENTORY.COMPONENT_ID
returns the following resultset:
COMPONENT_ID PRODUCT_ID ON_STOCK
1 1 5
1 2 5
1 1 2
1 2 2
2 1 (null)
2 2 (null)
Now, my next thought was, "of course, this is how joins behave, okay I need to group the results". But the way SQL works, the aggregation is not entirely predictable. SO when I
GROUP BY COMPONENT_PRODUCT.COMPONENT_ID,COMPONENT_PRODUCT.PRODUCT_ID
I get this result:
COMPONENT_ID PRODUCT_ID ON_STOCK
1 1 5
1 2 5
2 1 (null)
2 2 (null)
I have prepared a Fiddle here: http://sqlfiddle.com/#!9/71ca87
What am I forgetting here? Thanks in advance for any pointers.
Try this query -
SELECT DISTINCT
COMPONENT_PRODUCT.COMPONENT_ID,
COMPONENT_PRODUCT.PRODUCT_ID,
INVENTORY.ON_STOCK
FROM INVENTORY
RIGHT JOIN COMPONENT_PRODUCT ON COMPONENT_PRODUCT.COMPONENT_ID =
INVENTORY.COMPONENT_ID
AND COMPONENT_PRODUCT.PRODUCT_ID = INVENTORY.INSTALLATION_ID
I have two Mysql tables which are:
1 - Main table (_benefits) which consists of benefitCode, benefitName and
2 - Second table which consists of clientID, benefitCode1, benefitCode2, etc.
what I need is that when I do a SELECT * FROM _benefits
I get a COUNT of all clients receiving the corresponding benefitCode
a small example would be:
Main Table
Benefit Code Benefit Name
6MBO 6 Monthly Bonus
AP Age Pension
Beneficiaries Table
clientID 6MBO AP
123M 1 0
456M 1 1
Required Output Select statement should give
Benefit Code Benefit Name Total
6MBO 6 Monthly Bonus 2
AP Age Pension 1
Thank you for your assistance
I have a table, poll_response, with three columns: user_id, poll_id, option_id.
Give an arbitrary number of poll/response pairs, how can I determine the number of distinct user_ids match?
So, suppose the table's data looks like this:
user_id | poll_id | option_id
1 1 0
1 2 1
1 3 0
1 4 0
2 1 1
2 2 1
2 3 1
2 4 0
And suppose I want to know how many users have responded "1" to poll 2 and "0" to poll 3.
In this case, only user 1 matches, so the answer is: there is only one distinct user.
But suppose I want to know how many users have responded "1" to poll 2 and "0" to poll 4.
In this case, both user 1 and user 2 match, so the answer is: there are 2 distinct users.
I'm having trouble constructing the MySQL query to make this happen, especially given that there are an arbitrary number of poll/response pairs. Do I just try to chain a bunch of joins together?
To know how many users have responded "1" to poll 2 and "0" to poll 3.
select count(user_id) from(
select user_id from tblA
where (poll_id=2 and option_id=1) or (poll_id=3 and option_id=0)
group by user_id
having count(user_id)=2
)m
SQL FIDDLE HERE.
Suppose I have a table that holds some type of record, say cooking instructions like "Fold the melted chocolate into the egg whites". The table contains a unique ID field and the string.
I want to build another table for recipes (each with a unique ID and a name), each of which would be a series of sequential instructions (some instructions would be used for several/many recipes).
What is the best way to structure my recipe table to map a recipe's unique ID to a sequential series of instructions (which IDs are not sequential)?
Try a normalized design like this:
recipe
id name
1 Recipe1
2 Recipe2
recipe_instruction
recipe_id instruction_id sortorder
1 5 1
1 3 2
1 4 3
2 6 1
2 7 2
2 3 3
To get the list of instructions for a specific recipe you can use this query:
SELECT i.the_string
FROM recipe_instruction AS ri
JOIN instruction AS i
ON ri.instruction_id = i.id
WHERE ri.recipe_id = 1
ORDER BY ri.sortorder
I have this data on a table :
id name field
0 marco attack
1 andrea defense
2 luca medium
3 ernesto defense
4 vittorio medium
5 manuele attack
i need to order as field. BUT, the priority list order (for my example) should be defense-medium-attack.
so it must return :
andrea, ernesto, luca, vittorio, marco, manuele.
How can do it? bye
You should store the fields in a separate table and give them a sort order. Then you can join to that table.
As well as allowing you to sort efficiently, it also makes the table structure more relational - which is good.
id field sort
1 defense 1
2 medium 2
3 attack 3
id name field
0 marco 3
1 andrea 1
2 luca 2
3 ernesto 1
4 vittorio 2
5 manuele 3
select p.name,
ps.field
from players p
join playersort ps
on p.field = ps.id
order by ps.sort
SELECT
X.id,
X.name,
X.field
FROM (
SELECT id,
name,
field,
CASE field WHEN 'defense' THEN 1
WHEN 'medium' THEN 2
WHEN 'attack' THEN 3
END AS SortValue
FROM MyTable) AS X
ORDER BY X.SortValue