I have a table, inventory. The items in inventory have an item ID, but there might be multiple entries with different colors and sizes for the same item id. I'm trying to figure out how to return the total number in inventory for each item. So, I would return the total number of item 1, and not the total number of yellow item 1's then the total number of small item 1's, etc.
Thanks!
The basic query would be an aggregation query, something like:
select itemid, sum(quantity)
from inventory
group by itemid;
If you want the total for a single item, then you would add something like where itemid = 1.
If I'm understanding correctly, it sounds like you need to use distinct with count:
select count(distinct itemid)
from inventory
Depending on your desired results, you may be looking to group your results. In which case, you'd use group by with the desired field. Maybe something like this:
select itemid, count(*)
from inventory
group by itemid
This will return a row with the count of records per item id.
Related
I want to see the average cost of an item. First I am using a SUM statement and GROUP BY the manufacturing order and Item to see how much each item costs per manufacturing order (using WHERE statements to take out specific steps in the process). Then I want to average those to see how much the item costs on average based on that set, can I do this easily in one statement instead on creating a temp table?
You have to take result in temp table if you first want to sum the cost of an item per manufacture order and perform average on total cost per item achieved from sum. I hope I understood your problem statement clearly.
SELECT item, AVG(cost) FROM
(SELECT item, manufacture_order, SUM(COST) cost
FROM manufacture_order_tab
GROUP BY item, manufacture_order) tab1
GROUP BY item;
try this
SELECT AVG(Cost), SUM(COST)
FROM your_table
GROUP BY your_column
I would like to check whether a record with the same ID (Product_ID) has more than one record with a different date (I would like to check if the product was received in different loots or in just one day), so if it just returns one row it means that it was delivered all the same day, and if more than one result is return its the other way round.
Table PRODUCT
ID (PK) | Product_ID | Type | Deliver_Date | Amount
I've tried with a group by and distinct with no result.
EDIT: Query I had so far...
SELECT DISTINCT ,
count(*)
FROM PUBLIC.product
WHERE product_id = ?
AND deliver_date = ?
HAVING count() = 1
Your use of the DISTINCT keyword is incorrect.
You can either use DISTINCT with a list of fields following it, telling which fields you want to have distinct, or use an aggregate function like COUNT, SUM, MIN etc. together with grouping fields.
E.g.
SELECT DISTINCT Product_Id, Deliver_date
FROM ...
WHERE ...
means "give me all the distinct combinations of product id and deliver date". This is not actually what you need, as DISTINCT Product_id will simply tell you which product IDs there are, but not how many of them and in which dates, and DISTINCT Product_id, Deliver_date will give you all possible combinations of product ID and deliver date, but you'll need to count them manually.
The GROUP BY construct is more informative
SELECT count(*), Product_Id
FROM ...
WHERE ...
GROUP BY Product_Id
Groups the rows by the product id. It tells you how many rows are there for each product ID.
But what if you have several rows with the same product ID and the same date? You'll get a number greater than 1 in that query, but it won't help you because you wanted to distinguish between products delivered on two different days and products delivered all on the same day.
To do this, you need to use COUNT(DISTINCT(Deliver_date)):
SELECT COUNT(DISTINCT(Deliver_date)), Product_ID
FROM ...
WHERE ...
GROUP BY Product_ID
This means:
* Separate the rows into groups by the Product_ID.
* Inside each such group (all the rows that have that Product_ID), find all the distinct Deliver_Date values. Count how many such Delivery_Date values there are inside the group.
So if the product was delivered 10 times within the same day, you'll just have one distinct delivery date for that product ID. The COUNT will return 1. If it was delivered 5 times on day x, and 5 times on day y, then you'll have two distinct delivery dates (x and y), and the COUNT will return 2.
Now, if you want to eliminate all the ones that were all delivered on the same day (the count of distinct dates is 1), you add a HAVING clause to your query:
SELECT COUNT(DISTINCT(Deliver_date)), Product_Id
FROM PRODUCT
GROUP BY Product_Id
HAVING COUNT(DISTINCT(Deliver_date)) > 1
This will give you a list of all the products that were delivered on at least two separate dates.
Of course, if you just want to check if a particular product was delivered on more than one date, it's simpler:
SELECT COUNT(DISTINCT(Deliver_date))
FROM PRODUCT
WHERE Product_ID = ?
This will give you the number of distinct days on which this product was delivered. If it delivered on just one day, the result will be 1. If it was delivered on more than one day, the result will be a number greater than one.
To sum up:
There is no such thing as a DISTINCT,. DISTINCT is always followed by the name of a field or fields that are supposed to be distinct.
But there is a COUNT(DISTINCT(field_name)) which counts how many distinct values the field has in a group or a result set.
Question: Select the item and per unit price for each item in the items_ordered table. Hint: Divide the price by the quantity.
1.
select item, sum(price)/sum(quantity)
from items_ordered
group by item;
2.
select item, price/quantity
from items_ordered
group by item;
Have a look at the resultis for flashlights. First one shows average price correctly but 2nd one only takes 28/4 and shows 7, ignoring the 4.5 few rows down. Someone please explain why this is the case.
The used table data from an external website.
SUM() is a group function - so that essentially says go get me all the price and quantities by item, and add them all up to return them in one row.
MySQL is quite forgiving when grouping things and will try to retrieve a rowset (which is why your second example returns something - albeit wrong).
Generally, if you are GROUPing columns (items in your exmaple), you need to return one row per column (item).
Try running the SQL below to see what that looks like.
SELECT item
, SUM(price) AS sum_price
, SUM(quantity) AS sum_quantity
, COUNT(*) AS item_count
, SUM(price) / SUM(quantity) AS avg_price_per_quant
FROM items_ordered
GROUP BY item
ORDER BY item ASC
The first query returns the average price for that item, the second query returns the price for the first item it encounters. This only works in MySQL, the second query would error in SQL Server as no aggegrate function is used. See this post for more details Why does MySQL allow "group by" queries WITHOUT aggregate functions?.
Hey all so I have created a view using some tables in my DB.
My view looks like..
Listing all Order information.
Next, I need to make another view based on the one shown above. For this one I need to display only the Product Names, Total number of times product has been ordered, and the Total price for that product.
I am having trouble combining the ProductNames whilst also combining the Quantity and ItemTotal.
I have tried using Distinct(ProductName)
CREATE VIEW ProductSummary AS
SELECT DISTINCT(ProductName), Quantity AS OrderCount, ItemTotal
FROM OrderItemProducts
however that just results in..
Which is not correct because it displays duplicate ProductNames
(because they have different OrderCounts).
I would like to Combine the Duplicate rows and total the OrderCount and ItemTotals. What is the best and/or most correct way to do this?
Use group by and sum:
CREATE VIEW ProductSummary AS
SELECT ProductName, sum(Quantity) AS OrderCount, sum(ItemTotal) as ItemTotal
FROM OrderItemProducts
Group by ProductName
i have a DB2 table (orderitems) that has columns named ORDERITEMS_ID and ORDERS_ID I'm looking to get a count of all of the orderitems_id that are associated with each orders_id. I can get the count but i would like the order_id associated with that count.
i've tried
SELECT COUNT(orderitems_id) as total
FROM orderitems
GROUP BY orders_id
ORDER BY total DESC
i believe this is giving me the total count of each of the items in a order_id. but i'm not sure how to add the order_id with the result set
if i try the following
SELECT orders_id, COUNT(orderitems_id) as total
FROM orderitems
GROUP BY orders_id
ORDER BY total DESC
this is a bad query
i've looked into joining but that seems to be dealing with two tables...not sure how to append this information.
Try this:
select distinct orders_id,
count(orderitems_id) as total
from orderitems
group by orders_id
order by total desc
Summarizing precisely what you want to do often helps. In this case, you want a count of orderitems_id for each distinct orders_id, e.g. for each different value of orders_id and not for each line. When you want a result depending on the different values of a column, think distinct.