I just neEd to get the total count of products by category and also the count of product which have price >= 5 by category
This is what I need
You can use my example ON SQL FIDDLE
Thank you very much!
select id_category,
count(1) as qty_products,
count(case when product_price>5 then 1 end) as [qty_products>5]
from orders
group by id_category
It should give you count of products per category. I'm not sure what do you want to display as "id_order" though ... If it's just row number, then you can do something like
select ROW_NUMBER() OVER(ORDER BY id_category) as rownum,id_category,
count(1) as qty_products,
count(case when product_price>5 then 1 end) as [qty_products>5]
from orders
group by id_category
All:
SELECT
id_category,
count(*) AS COUNT
FROM orders
GROUP BY id_category
Output:
ID_CATEGORY COUNT
1 12
2 10
5 6
Only price >=5:
SELECT
id_category,
COUNT(*) AS COUNT
FROM orders
WHERE product_price >=5
GROUP BY id_category
Output:
ID_CATEGORY COUNT
1 10
2 7
5 3
SELECT ID_CATEGORY, COUNT(ID_PRODUCT) AS NoOfProducts,
SUM(CASE WHEN PRODUCT_PRICE > 5 THEN 1 ELSE 0 END) AS ProductsAbove5
FROM Orders GROUP BY ID_CATEGORY
Fiddle here
However, you can not get Id_Order field as you have to group by category.
select
id_category,
count(id_product) as CountAll,
count(case when product_price > 5 then 1 else 0 end) as CountGreaterThan5
from orders
group by id_category
Related
In the table below, I want to know how many customers ordered lunch without a coffee. The result would be 1, for sale ID 300, because two lunches were ordered but only one coffee.
It’s been 8 years since I last used SQL! How do I say “group the records by sale ID and for each group, drop groups where there is no lunch or COUNT(coffee) < COUNT(lunch)"?
SALE ID
Product
100
coffee
100
lunch
200
coffee
300
lunch
300
lunch
300
coffee
here is one way:
select count(*) from (
select saleID
from tablename
group by saleID
having sum(case when product ='coffee' then 1 else 0 end) = 0
and sum(case when product ='lunch' then 1 else 0 end) = 1
) t
You can do it with aggregation and the conditions in the HAVING clause.
This query:
SELECT sale_id
FROM tablename
GROUP BY sale_id
HAVING SUM(product = 'lunch') > SUM(product = 'coffee');
returns all the sale_ids that you want.
This query:
SELECT DISTINCT COUNT(*) OVER () counter
FROM tablename
GROUP BY sale_id
HAVING SUM(product = 'lunch') > SUM(product = 'coffee');
returns the number of sale_ids that you want.
See the demo.
select count(*) from (
--in this subquery calculate counts and ignore items that haven't any lunch
select
saleID, sum(case when product ='coffee' then 1 else 0 end) as coffee,
sum(case when product ='lunch' then 1 else 0 end) lunch
from tablename
group by saleID
having sum(case when product ='lunch' then 1 else 0 end) >= 1 --Here we are ignoring all items haven't any lunch
) t
where lunch > coffee -- we check second condition be ok
After a query I end up with a table like this
tbl_1:
category type count
cpg b 1
auto c 1
cpg c 1
auto v 1
I would like to calculate the total_count by category, and the percentage of count that is not (type<>'v') also by category in SQL.
Any ideas how I could do that ?
The resulting table should look like this:
category total_count percentage
cpg 2 1
auto 2 0.5
Is this what you want?
select category, sum(count),
sum(case when type <> 'v' then count else 0.0 end) / sum(count)
from t
group by category;
From the original table, you could just do:
select category, count(*) as cnt,
avg(case when type <> 'v' then 1.0 else 0 end) as ratio
from tbl1
group by category;
I have a table which stores purchase info from sellers and table contains rating to every purchase out of 5 stars. I want to have output Group By sellers and Each sellers good(Above 3) and bad(Below 4) ratings count
PurchaseId SellerId PurchaseRating
1 ABC 2
2 ABC 5
3 DEF 1
4 XYZ 2
5 DEF 4
7 ABC 3
OUTPUT
SellerId TotalOrders AvgRating Above3*(4&5) Below4*(1 to 3)
ABC 3 3.3 1 2
DEF 2 2.5 1 1
XYZ 1 2 0 1
For first 3 columns I am getting result using this
Select SellerId, Count(P.Id) TotalOrders, Avg(PurchaseRating) AvgRating,
CASE WHEN P.PurchaseRating >= 4 THEN 1 ELSE 0 END AS Above3*(4&5)
from
[dbo].[AmazonAbeOrdersPurchaseInfo] P
Where PurchaseRating is not null
Group by P.SellerId
order by TotalOrders desc
Unable to identify how to include case in group by clause.
You are trying to do conditional aggreation. The important thing is that you want the conditional expression inside the aggregate function:
select
sellerId,
count(*) totalOrders,
avg(purchaseRating) avgRating,
sum(case when purchaseRating > 3 then 1 else 0 end) above3,
sum(case when purchaseRating < 4 then 1 else 0 end) below4
from [dbo].[AmazonAbeOrdersPurchaseInfo]
where purchaseRating is not null
group by sellerId
order by totalOrders desc
I'm little stuck with this algorithm that I have to do in sql.
I have in a table product codes, product name , supplier and the product update date.
I need to prioritize the products by provider and date as the case may be.For Example, if a product has 1,2 and / or 3 as a supplier, the product with the highest update rate must be selected.
But if the product has a supplier 4, 5 and / or 6, the product with the lowest number of the supplier must be selected.
Make this query that is in a cursor if the query is greater than or equal to 1 the data the first data in the row is inserted into another table. It is what I have for the moment
SELECT #QUERY = count (*) from (
SELECT TOP 1
code_product,update_date,supplier,name_product
FROM Product_updates
WHERE ( supplier = 1 OR supplier =2 OR supplier = 3 ) and code_product
= #code_product and code_product is not null
order by update_date DESC
) a
Practical example, here is the table
code_product name_product supplier update_date
1313 A 1 11-03-2019
1313 A 3 12-10-2019
1313 A 2 11-10-2019
1313 A 6 13-10-2019
1515 B 5 13-10-2019
1515 B 4 13-02-2018
1515 B 6 15-04-2019
and expected result
code_product name_product supplier update_date
1313 A 3 12-10-2019
1515 B 4 13-02-2018
Please help, Regards.
You would use row_number() for prioritization:
select pu.*
from (select pu.*,
row_number() over (partition by code_product
order by (case when supplier in (1, 2, 3) then update_date desc,
update_date asc
) as seqnum
from product_updates pu
) pu
where seqnum = 1;
You can do somewhat manipulating the supplier values via case
Select code_product, name_product,
Case when 1 =max(Case
when
supplier in
(1,2,3)
then 1
else 2 end) then max(date)
Else min(date) end
from table group by code_product,
name_product, (Case when 1
=max(Case
when
supplier in
(1,2,3)
then 1
else 2 end) then max(date)
Else min(date) end)
I have a table structure
Table1
ID Hours Qty ProductID
1 2 1 100
1 3 5 200
2 6 6 100
2 2 2 200
If productid is (1,2,3) then i need sum ( Qty * Hours),If productid in (200,300,400,500) then i need sum(qty).
I have written a code like this
select ID,case when productid in (1,2,3) then
SUM( qty * hrs)
when productid in (100,200,300) then SUM( qty ) end result1
from Prod group by id ,productid
but i don't want to group by productid,i would like to pass it in "IN clause".How to achieve it.
Move the SUM() outside of the CASE WHEN statement.
SELECT
ID,
SUM(case when productid in (1,2,3) then qty * hrs
when productid in (100,200,300) then qty
end) result1
FROM
Prod
GROUP BY
ID
Assuming you want all columns plus the result of your query, you can do this:
select p.*, aux.result
from Prod p
inner join (select ID,case when productid in (1,2,3) then SUM( qty * hrs)
when productid in (100,200,300) then SUM( qty )
end as result
from Prod group by id ,productid) aux on aux.id = p.id