show the sum of a specific variety to another table sql - sql

Table Inventory
|Variety|Weight|Quantity|
|-------|--------|------|
|Native Chicken|1.6kg|10|
|Native Chicken|1.3|20|
|Chicken Broiler|2.1|30|
|Chicken Broiler|2.3|10|
|Duck|2.1|30|
|Duck|2.3|15|
|Turkey|2.1|30|
|Turkey|2.3|15|
How to get this desired Output for table Reports using the SELECT WHERE, Join and Sum statement in SQL. I've been trying to solve this for a few days now but I haven't seen any codes to answer for this one. Can you help your girl out? Thanks!
Table Reports
|Variety|Qty|
|-------|---|
|Native Chicken|30|
|Chicken Broiler|40|
|Duck|45|
|Turkey|45|
this is the code that I've tried but it's not working.
INSERT INTO reports(qty)
SELECT SUM(qty) FROM (SELECT Inventory.Quantity = "Native Chicken" FROM InventoryTable);

You just need to sum the quantity and group by the column that represents each quantity.
The group by "groups" all like values together where you then sum the quantites for each into a single value
insert into Reports (Variety, Qty)
select Variety, sum(Quantity)
from Inventory
group by Variety

Do a GROUP BY:
insert into Reports (Variety, Qty)
select Variety, sum(Quantity) as Qty
from Inventory
group by Variety
However, I'd consider a view instead of a Reports table, because a view will always have up-to-date data. A separate table requires being emptied and inserted into over and over again.

INSERT INTO reports
SELECT Variety, SUM(Quantity) FROM Inventory GROUP BY Variety;
See https://www.w3schools.com/sql/sql_insert_into_select.asp

Related

sql can't check column1>column2 not working

I have some problem in sql syntax.
i have products table and below two column
total_quantity (Its total quantity for products)
remain_alert_quantity
i want to get records that are below remain_alert_quantity
select * from products where total_quantity<remain_alert_quantity
but its not work .
can i check condition with column value in sql.
thanks and regards.
Given data is not sufficient so I am assuming few thing like,
you want to compare the product quantity with the remain_alert_quantity of same product.
There is some unique column in table (like product_id)
in this case try the below query.
select * from products a where a.total_quantity<
(select b.remain_alert_quantity from products b where b.product_id=a.product_id);

SQL query to return a list of items including the records details, count and sum prices

I am a complete newbie to sql,
I have an access database table that contains a list of car part items including fields - partnumber, price, description, barcode etc- this table is used to temporarily store the details of a sale. It is a legacy system that does not include a qty ie for multiple items they are duplicate records in this temporary table.
I am trying to produce an "invoice" output in which I need to have The quantity of items (ie counts of duplicate items) list their description and part number and hopefully total each group of duplicate entries prices.
I have tried statements along the lines of
select partnumber, count(partnumber) as qty,
description, price, sum(price) as subtotal
from tblregister
group by partnumber
I know my syntax is way out just after some pointers as to how I achieve what I am after
Try adding the other non-aggregated fields in your select clause to the group by clause, like so:
select partnumber, count(partnumber) as qty, description, price, sum(price) as subtotal FROM tblregister group by partnumber, description, price

Using two tables to get one report in SQL Server

I have two tables, product and download as follows.
product (product_id (pk), product_name)
download (download_date(pk), download_version(pk), product_id(pk,fk))
I need a report to show how many downloaded, form which version of what product took place in each month.
SELECT
[product_name],
[version],
MONTH(download_date) AS [Month],
COUNT(MONTH(download_date)) AS [Count]
FROM
product
INNER JOIN
download ON product.product_id = download.product_id
GROUP BY
MONTH(download_date)
and I get this error
Column 'product.product_name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Use alias names for the tables for better readability.
Mention the aliasname.columnname in the SELECT to avoid fetching the wrong values.
You missed the other columns except the aggregate values in the GROUP BY.
So the query below will return the result.
SELECT P.[product_name],
P.[version],
MONTH(D.download_date) AS [Month],
COUNT(MONTH(D.download_date)) AS [Count]
FROM product P
INNER JOIN download D ON D.product_id = P.product_id
GROUP BY P.[product_name], P.[version], MONTH(D.download_date)
You have some issue with tables and primary key.
create the table like this.
product(product_id(PK),name,verion)
download(date,product_id)
and run this query
SELECT product.name,product.version,COUNT(download.product_id)
FROM product INNER JOIN download ON product.product_id=download.download_id
Group BY(download._productid);
i think this is what you want if not post replay i will answer when i get back to stack.

Whats the difference between these two SQL queries?

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?.

SQL grouping results in a select

My SQL table "offers" contains offers users make for products (product_ID, customer_ID, offer).
In my admin page, I want to list the products for which at least one offer exists and show the total offers existing for it.
For example,
PRODUCT #324 Total offers: 42
PRODUCT #99 Total offers: 1
etc.
My guess would be to combine a
SELECT DISTINCT product_ID FROM offers...
And in a second query, to SELECT COUNT(*) FROM offers WHERE product_ID=...
Is it the most efficient way to achieve this, or is there a way to make it inside a single query?
You can do this in one query which will get the count by grouping by the product_id:
SELECT product_ID, COUNT(*)
FROM offers
GROUP BY product_ID
As bluefeet already answered, you achieve it in single query by using group by.
(group by demo)
Another thing to mention is the order by,
select
product_id as id,
count(*) as totals
from
t
group by product_id
order by totals;
If you want to sort with the totals of hits, or if you want to sort by product_id etc.
sqlfiddle