SQL: How to display items from one table according to categories from another table, and counting the total items in each category - sql

I have two tables, a category table, and a items table. The category table have the following fields:
project_No
cat_ID
cat_Description
The items table has the following fields:
project_No
cat_ID
item_Id
item_description
item_Qty
item_cost
Now I need to write a query that displays all the items for each category, but I also want to count the amount of items in each category
Now the output must first list the category before the items in that catgegory and then list all the items in that category, and giving a total for the amount of items in that category, then another category and the items, for example
Beverages
Coffee $1.50 4
Tea $2.50 4
Total Items 2
Tin Food
Peas $0.50 10
Meatballs $1 20
total items 2
I need to write this SQL and place it inside my TADOQuery component in Delphi
Please can anyone assist me

Select category_table.Cat_Description, item_table.Item_Desctiption, SUM(Item_table.Item_Cost) as Total_Cost, SUM(item_Table.Item_Qty) as Total_Qty
From Items_table join Category_Table
on items_Table.Cat_Id = Category_Table.Cat_Id and items_Table.Project_no = Category_table.Project_no
This would get you the result:
Beverages Coffee $1.50 4
Beverages Tea $2.50 4
Tin Food Peas $0.50 10
Tin Food Meatballs $1.00 20
I summed your cost, but if you just want cost listed, remove the sum from the option.

Related

How to group by condition and average only if column value is not null in bigquery sql

Hi I have a table that shows the category of product and another table with daily price of the product. I would like to get the average price of the category where average not count null values. How do I achieve this? Example of table product
product
category
apple
fruit
pear
fruit
grape
fruit
celery
vegetables
cabbage
vegetables
chicken
meat
turkey
meat
beef
meat
another table with daily price and productid as columns and the price in the rows
date
apple
pear
grape
celery
cabbage
chicken
turkey
beef
2022-01-01
2
4
1
2
3
4
3
2022-01-02
2
2
2
4
3
2022-01-03
2
2
2
3
into
date
fruit
vegetables
meat
2022-01-01
3
1.5
3.3
2022-01-02
2
2
3.5
2022-01-02
2
2
3
Where average is only to columns where it is not null, it would be better not to do it manually.
Consider below query using UNPIVOT AND PIVOT:
SELECT * FROM (
SELECT date, category, price
FROM prices UNPIVOT (price FOR productid IN (apple, pear, grape, celery, cabbage, chicken, turkey, beef)) p
JOIN category c ON c.product = p.productid
) PIVOT (AVG(price) FOR category IN ('fruit', 'vegetables', 'meat'))
ORDER BY date;
Consider also below approach
create temp function keys(input string) returns array<string> language js as """
return Object.keys(JSON.parse(input));
""";
create temp function values(input string) returns array<string> language js as """
return Object.values(JSON.parse(input));
""";
select *
from (
select date, category, round(avg(safe_cast(price as float64)), 2) avg_price
from prices t, unnest([struct(to_json_string(t) as json)]),
unnest(keys(json)) product with offset
join unnest(values(json)) price with offset using(offset)
left join products using(product)
where product != 'date'
group by date, category
)
pivot (any_value(avg_price) for category IN ('fruit', 'vegetables', 'meat'))
if applied to sample data in your question - output is
Potential benefit of using above is to eliminate need in enlisting all column names from products table, which are 8 in your example but in reality most likely much more! Obviously, another way to address this is to build dynamic query and run it using execute immediate which you can find quite a number of examples here on SO.
But, assuming that number of categories is significantly lower (just few as in your example) to compare with number of products - I would use this approach as execute immediate has its own drawbacks ...

How to merge two rows and sum the columns

product
quantity
price
milk
3
10
bread
7
3
bread
5
2
And my output table should be
product
total_price
milk
30
bread
31
I can't seem to get my code to work. Here is my code
SELECT product, (SELECT (quantity*unit_price)
FROM shopping_history AS sh ) AS total_price
FROM shopping_history
GROUP BY product
You are looking for the aggregate function SUM (which doesn't require a sub-query) e.g.
SELECT product, SUM(quantity*unit_price) AS Total_Price
FROM shopping_history
GROUP BY product

Count all of a column where value is 2 and sum this value with price

I'm doing with Northwind database where I use the Products table. I need to count all of the rows where Category_Id is 2 and sum the amount with the prices.
Here's the example of a table shortly:
Category_ID | Unit Price
1 | 2,90
2 | 3,70
3 | 4,90
2 | 1,90
5 | 0,90
2 | 2,90
There are 3 rows where category_Id is 2. How to sum this 3 with that rows Unit price?
3,70 + 1,90 + 2,90 = 8,50
So the answer I need is 8,50 but I have no idea how to get that amount with a SQL query.
Does someone know?
you can get the aggregated values for all Ids using
Select Categeory_Id, sum([Unit Price]) Total, count(*) Qty
from Products
group by Category_Id
or just a specific total such as
select sum([Unit Price]) total
from products
where category_Id=2

How to add values of the same item in a column?

I have the following columns in a table:
Product Name
Quantity
Description
I want to add the quantities of the product if the name of the product is same. For example if I have product with the same name twice, I want the total quantities of the products to be added and get a result.
This is the table and I want to add the quantities of the same item:
Name Quantity Description
Pen 3
Pencil 2
Pen 6
Eraser 7
Eraser 6
For exmaple:
I have pen twice and so want to add (3+6) and the display the total as 9, and ...
I have eraser twice (7+6), so the total should be 13.
The solution is GROUP BY clause:
SELECT Name, SUM(Quantity)
FROM Table
GROUP BY Name
GROUP BY is used in an SQL query to group rows together for aggregation purposes.
For example:
If your input table had rows:
ItemName Qty
Pen 4
Pencil 7
Pen 6
SQL Code:
SELECT ItemName, SUM(Qty)
FROM Table
GROUP BY ItemName
Using GROUP BY ItemName in your query would give you the output:
ItemName Qty
Pen 10
Pencil 7
SELECT SUM(column_name) FROM table_name
This is the best example!

Getting the max(price) for each item ordered, given multiple different prices for any ordered item

If I have an items_ordered table that looks like this:
items_ordered
customerid order_date item quantity price
10330 30-Jun-1999 Pogo stick 1 28.00
10101 30-Jun-1999 Raft 1 58.00
10298 01-Jul-1999 Skateboard 1 33.00
10101 01-Jul-1999 Life Vest 4 125.00
10299 06-Jul-1999 Parachute 1 1250.00
10339 27-Jul-1999 Umbrella 1 4.50
10449 13-Aug-1999 Unicycle 1 180.79
And I want to get the max price for each distinct item in the table, given that an item could appear multiple times in this table with different prices, how would I do that, assuming that this doesn't work:
select item, max(price) from items_ordered;
Add a Group By and you're golden.
select item, max(price) from items_ordered group by item;