have the following table and data
partno price qty
A0001 10 2
A0001 8 6
A0001 15 10
How can I issue a query to get the following result
partno. price. qty.
A0001 15 250
unique partno, highest price in the list and sum(qty )* highest price.
A simple aggregation shows the result you want:
select
partno,
max(price) as max_price,
max(price) * sum(qty) as total
from t
group by partno
/*Just replace "Table_Name" with your first table's name in the database */
select
distinct
partno,
max(price) price,
max(price) * sum(qty) qty
from
Table_Name
group by
partno
Related
There are two Tables - orders and item_line
orders
order_id
created_at
total_amount
123
2022-11-11 13:40:50
450.00
124
2022-10-30 00:40:50
1500.00
item_line
order_id
product_id
product_name
quantity
unit_price
123
a1b
milo
4
100.00
123
c2d
coke
5
10.00
124
c2d
coke
150
10.00
The question is:
Find the products contributing to the 50% of the total sales.
My take on this is -
SELECT i.product_name,SUM(o.total_amount)AS 'Net Sales'
FROM item_line i
JOIN orders o on o.order_id = i.order_id
GROUP BY i.product_name
HAVING SUM(o.total_amount) = (SUM(o.total_amount)*0.5);
But this is not correct. SUM windows functions need to be used, but how?
Try the following, explanation is within the query comments:
-- find the the total sales for each product
WITH product_sales AS
(
SELECT product_id, product_name,
SUM(quantity * unit_price) AS product_tot_sales
FROM item_line
GROUP BY product_id, product_name
),
-- find the running sales percentage for each product starting from porduct with highest sales value
running_percentage AS
(
SELECT product_id, product_name, product_tot_sales,
SUM(product_tot_sales) OVER (ORDER BY product_tot_sales DESC) /
SUM(product_tot_sales) OVER () AS running_sales_percentage,
SUM(product_tot_sales) OVER () AS tot_sales
FROM product_sales
)
-- select products that have a running sales percentage less than the min(running_sales_percentage) where running_sales_percentage >= 0.5
-- this will select all of products that contributes of 0.5 of the total sales
SELECT product_id, product_name, product_tot_sales,
tot_sales,
running_sales_percentage
FROM running_percentage
WHERE running_sales_percentage <=
(
SELECT MIN(running_sales_percentage)
FROM running_percentage
WHERE running_sales_percentage >= 0.5
)
You don't need a join with orders table, all data you need is existed in the item_line table.
See demo.
Runnable query example at https://www.db-fiddle.com/f/ssrpQyyajYdZkkkAJBaYUp/0
I have a postgres table of sales; each row has a sale_id, product_id, salesperson, and price.
I want to write a query that returns, for each (salesperson, product_id) tuple with at least one sale:
The total of price for all of the sales made by that salesperson for that product (call this product_sales).
The total of price over all of that salesperson's sales (call this total_sales).
My current query is as follows, but I feel silly writing sum(sum(price)). Is there a more standard/idiomatic approach?
select
salesperson,
product_id,
sum(price) as product_sales,
sum(sum(price)) over (partition by salesperson) as total_sales
from sales
group by 1, 2
order by 1, 2
Writing sum(price) instead of sum(sum(price)) yields the following error:
column "sales.price" must appear in the GROUP BY clause or be used in an aggregate function
UPDATES
See this response for a nice approach using a WITH clause. I feel like I ought to be able to do this without a subquery or WITH.
Just stumbled on this response to a different question which proposes both sum(sum(...)) and a subquery approach. Perhaps these are the best options?
You can use a Common Table Expression to simplify the query and do it in two steps.
For example:
with
s as (
select
salesperson,
product_id,
sum(price) as product_sales
from sales
group by salesperson, product_id
)
select
salesperson,
product_id,
product_sales,
sum(product_sales) over (partition by salesperson) as total_sales
from s
order by salesperson, product_id
Result:
salesperson product_id product_sales total_sales
------------ ----------- -------------- -----------
Alice 1 2000 5400
Alice 2 2200 5400
Alice 3 1200 5400
Bobby 1 2000 4300
Bobby 2 1100 4300
Bobby 3 1200 4300
Chuck 1 2000 4300
Chuck 2 1100 4300
Chuck 3 1200 4300
See running example at DB Fiddle.
You can try the below -
select * from
(
select
salesperson,
product_id,
sum(price) over(partition by salesperson,product_id) as product_sales,
sum(price) over(partition by salesperson) as total_sales,
row_number() over(partition by salesperson,product_id order by sale_id) as rn
from sales s
)A where rn=1
I'm looking for some assistance: I am looking to get this into a report but not sure how to achieve this.
Here is the data stored in the table:
Product | Quantity | Status | Line
Product1 1 Active 1000
Product2 2 Active 2000
Product1 2 Active 3000
Product1 1 InDev 4000
Product2 2 Active 5000
I am grouping by Product and Status and summing up Quantity.
But looking to also retrieve the lowest line number for row in the group.
My expected result would be like below:
Product | Quantity | Status | Line
Product1 3 Active 1000
Product2 4 Active 2000
Product1 1 InDev 5000
Any help would be greatly appreciated
This can be done if you group by Product, Status and aggregate:
select Product, sum(Quantity) Quantity, Status, min(Line) Line
from tablename
group by Product, Status
You can use window function :
select *
from (select t.*, row_number() over (partition by product, status order by line) as seq,
sum(qty) over (partition by product, status) as sum_qty
from table t
) t
where seq = 1;
If table has only available columns (in question) then you can do aggregation :
select product, sum(qty), status, min(line) as line
from table t
group by product, status
order by line;
I want to select items and sum their sales and price like:
Produkt Sales Price
Car1 1 100
Car1 2 120
Car1 3 110
Car2 1 200
Car2 2 210
Result should be
Produkt Sales Price
Car1 6 330
Car2 3 410
I know the sum function, but I cant get out how I select the simular Cars to group them to sum the numbers.
You just need to GROUP your results on column produkt and use sum() aggregate function to calculate sales and prices for each group.
This is ANSI SQL, so it works not only in Firebird.
SELECT produkt, SUM(sales) as sales, SUM(price) as price
FROM yourtable
GROUP BY produkt
ORDER BY produkt
SELECT Produkt, SUM(Sales) AS Total_sales, SUM(Price) as Total_price
FROM table_name --Your table name here
GROUP BY Produkt
ORDER BY Produkt
Try this SQL
I have two tables:
product_in (store all product QTY)
Product Code(PK) Description QTY
RS121102 SUITS 100
RS129985 SUITS 100
DF-C09 SHIRTS 50
AE-H05 SHIRTS 50
product_out (store all products sold QTY)
Product Code Description QTY
RS121102 SUITS 50
AE-H05 SHIRTS 10
I want result like below
Product Code Description Total Qty Sold QTY
RS121102 SUITS 100 50
RS129985 SUITS 100 0
DF-C09 SHIRTS 50 0
AE-H05 SHIRTS 50 10
How can I do this?
SELECT pi.ProductCode, pi.Description, pi.QTY AS TotalQty,
ISNULL(po.QTY, 0) AS SoldQty
FROM product_in as pi
LEFT JOIN product_out as po
ON po.ProductCode = pi.ProductCode
That's asuming there aren't multiple records for each product in product_out.
Try this:
SELECT ProductCode, Description, SUM(Total_QTY) AS Total_Qty, SUM(Sold_Qty) AS Sold_Qty
FROM
(
SELECT ProductCode, Description, QTY As Total_Qty, 0 As Sold_Qty
from product_in
)
UNION ALL
(
SELECT ProductCode, Description, -QTY As Total_Qty, QTY As Sold_Qty
from product_out
)
GROUP BY ProductCode, Description