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

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

Related

Calculating percentages per product using aggregate functions and group by

I have a table with 5 rows representing two different products. The columns include the product, sales, discount. I'm trying to calculate the percentage of sales per product that included a discount. The table looks like this:
product
sales
discount
1
10
0
1
10
5
2
20
10
2
20
0
2
20
10
My results should look like the below (which I know because I've calculated this in Excel):
product
perc_discount
1
50.00
2
66.67
For each of the two products we are calculating the count of sales with discount divided by the total count of sales, so for product 1 it would be (1/2)*100 = 50.
My SQL code looks like the below:
SELECT
product,
(SELECT COUNT(*) FROM sales WHERE discount >0)/COUNT(*)*100 AS perc_discount
FROM sales
GROUP BY product
However, the result I'm getting is:
product
perc_discount
1
150.0
2
100.0
It seems to be calculating the total count of discounted sales in the table and diving it by the count of each product and I can't seem to figure out how to change it. Any ideas on how I can improve this?
Thanks.
How about conditional sum?
SQL> select product,
2 round(sum(case when discount > 0 then 1 else 0 end) / count(*) * 100, 2) perc_discount
3 from sales
4 group by product;
PRODUCT PERC_DISCOUNT
---------- -------------
1 50
2 66,67
SQL>
So: add 1 for every discount row per product. Divide that sum with total number of rows per product (that's count). Round the result to 2 decimals (so that it looks prettier).
You can use conditional aggregation. For example:
select
product,
100.0 * count(case when discount <> 0 then 'x' end) / count(*) as perc_discount
from sales
group by product

Select the best selling product ID

What if I have table like this and I want to select the best selling product_id.
id
transaction_id
product_id
qty_sold
1
21
2
5
2
22
3
2
3
23
4
2
3
24
2
1
3
25
2
4
I want the best selling product_id with the highest qty_sold
Using SQLS, you can group by the productID, add up the number of sold, and order by the total descending. If we also take the minimum transaction ID per product, if two products come out to have the same total qty, we can take the minimum tran ID to split the tie
SELECT TOP 1 product_id, SUM(qty_sold) as sellcount, MIN(transaction_id) as firsttran
FROM t
GROUP BY product_id
ORDER BY SUM(qty_sold) DESC, MIN(transaction_id)
Once you're happy the sums are right etc, you can remove the , SUM(qty_sold) as sellcount, MIN(transaction_id) from the SELECT if you want/if you only need the prod ID

How to find lowest value from one columns that has been Grouped by SQL Server

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;

SQL Server: how to divide the result of sum of total for every customer id

I have 4 tables like this (you can ignore table B because this problem did not use that table)
I want to show the sum of 'total' for each 'sales_id' from table 'sales_detail'
What I want (the result) is like this:
sales_id | total
S01 | 3
S02 | 2
S03 | 4
S04 | 1
S05 | 2
S05 | 3
I have tried with this query:
select
sum(total)
from
sales_detail
where
sales_id = any (select sales_id
from sales
where customer_id = any (select customer_id
from customer)
)
but the query returns a value if 15 because they are the sum of those rows of data.
I have tried to use "distinct" before sum
and the result is [ 1, 2, 3 ] because those are distinct of those rows of data (not sum of each sales_id)
It's all about subquery
You are just so far off track that a simple comment won't help. Your query only concerns one table, sales_detail. It has nothing to do with the other two.
And, it is just an aggregation query:
select sd.sales_id, sum(sd.total)
from sales_detail sd
group by sd.sales_id;
This is actually pretty close to what the question itself is asking.

postgresql - how to calculate the percentage of number of entries in one table w.r.t to no of entries in another table

I have two tables(A & B) in my database(c). both tables contains two coulmns as shown below
column name Datatype
eventtime timestamp without time zone
serialnumber numeric
Table A contains total number of products (good and downgraded -- but in these table they are not defined as affected/downgraded) produced in each day. And table B contains total number of only downgraded products.
I want to make an quality process control chart using the percentage of downgraded products w.r.t total number of products produced (using serialnumber to join for example).
could some one tell me how can i get the percentage value for each day (also for each hour)
Use date_trunc() to group rows by desired period, e.g.:
select
a.r_date::date date,
downgraded,
total,
round(downgraded::numeric/total* 100, 2) percentage
from (
select date_trunc('day', eventtime) r_date, count(*) downgraded
from b
group by 1
) b
join (
select date_trunc('day', eventtime) r_date, count(*) total
from a
group by 1
) a
using (r_date)
order by 1;
date | downgraded | total | percentage
------------+------------+-------+------------
2015-05-05 | 3 | 4 | 75.00
2015-05-06 | 1 | 4 | 25.00
(2 rows)