How to use COUNT in my sql query - sql

I'm trying to count the number of occurrence of product_id.
This is my query that's return 10 products but not according the number of occurrence of product_id.
SELECT name, category_id, product_id
FROM product, notification
WHERE type='product_consumed' OR type='product_rate' AND product.id = notification.product_id AND category_id="1"
GROUP BY product_id ORDER BY product.category_id ASC LIMIT 10
I tried to COUNT the occurrenceof product_id like this, but it returns to me bad results,
SELECT name, category_id, product_id, COUNT(*) AS most_viewed
FROM product, notification
WHERE type='product_consumed' OR type='product_rate' AND product.id = notification.product_id AND category_id=".$cat."
GROUP BY product_id ORDER BY product.category_id ASC, most_viewed DESC LIMIT 10
My wish is to have a sql response like this :
Category of the product | Name of product | Number of views
Thanks

Try this, you are missing a set of brackets
SELECT name, category_id, product_id, COUNT(*) AS most_viewed
FROM product, notification
WHERE (type='product_consumed' OR type='product_rate') AND product.id = notification.product_id AND category_id=".$cat."
GROUP BY product_id
ORDER BY product.category_id ASC, most_viewed DESC LIMIT 10

Related

SQL Query - second ID of a list ordered by date and ID

I have a SQL database with a list of Customer IDs CustomerID and invoices, the specific product purchased in each invoice ProductID, the Date and the Income of each invoice . I need to write a query that will retrieve for each product, which was the second customer who made a purchase
How do I do that?
EDIT:
I have come up with the following query:
SELECT *,
LEAD(CustomerID) OVER (ORDER BY ProductID, Date) AS 'Second Customer Who Made A Purchase'
FROM a
ORDER BY ProductID, Date ASC
However, this query presents multiple results for products that have more than two purchases. Can you advise?
SELECT a2.ProductID,
(
SELECT a1.CustomerID
FROM a a1
WHERE a1.ProductID = a2.ProductID
ORDER BY Date asc
LIMIT 1,1
) as SecondCustomer
FROM a a2
GROUP BY a2.ProductID
I need to write a query that will retrieve for each product, which was the second customer who made a purchase
This sounds like a window function:
select a.*
from (select a.*,
row_number() over (partition by productid order by date asc) as seqnum
from a
) a
where seqnum = 2;

Select multiple columns with not all columns mentioned in Groupby - Postgres v12

I have a table which contain review_id,product_id,ratings,reviewer_id,review_comments. The table i have is as below.
My need is quite simple but I have issues figuring it out. Need is to get product_id, rating, reviewer_id and review_comments of the product_id which has the max value of review_id
With below query, I am able to get product_id and review_id properly.
SELECT product_id,max(review_id) as review_id
FROM public.products Group by product_id;
But when I try to add ratings, reviewer_id, and review_comments, it raises an error that those columns have to be part of a groupby and if I add those columns, grouping gets disturbed since I need grouping only on product_id and nothing else.
Is there a way to solve this?
My expected result should contain all row content with review_id 7,5,8 since for product_id 1 review_id 7 is highest and for product_id 2 review_id 5 is highest and for product_id 3 review_id 8 is highest.
Try PostgreSQL's DISTINCT ON:
SELECT DISTINCT ON (product_id)
product_id,
review_id,
rating,
reviewer_id,
review_comments
FROM products
ORDER BY product_id, review_id DESC;
This will return the first row for each product_id in the ORDER BY order.
This can be done with NOT EXISTS:
select p.product_id, p.rating, p.reviewer_id, p.review_comments
from public.products p
where not exists (
select 1 from public.products
where product_id = p.product_id and review_id > p.review_id
)
You can try below way-
select * from tablename a
where review_id =(select max(review_id) from tablename b where a.product_id=b.product_id)
or use row_number()
select * from
(
select *, row_number() over(partition by product_id order by review_id desc) as rn
from tablename
)A where rn=1

Group by number of instances of product sold descending and then order by string length of product name in SQL

I have a table with two columns: product_id and product_name. Each product_id is repeated multiple times and each occurrence equals one sale.
I want to count the number of times the product_ids occur and order them from the most sold to the least and then I want to order it by the length of the product_name which is a string.
Right now this is what I have now and I am not sure it is correct:
SELECT product_id, COUNT(*), LEN(product_name) AS NameLength
FROM table
GROUP BY product_id
ORDER BY COUNT(product_id) DESC, LEN(product_name);
You can not add LEN(product_name) in the selection without adding the same in the GROUP BY part. You can try this following script which will first order the result by Most Order Count of a Product and then by length of product name if there more then one product available with the same count.
SELECT product_id,LEN(product_name), COUNT(*)
FROM table
GROUP BY product_id,LEN(product_name)
ORDER BY COUNT(product_id) DESC, LEN(product_name)
I don't believe that you want in the SELECT list the length of the product_name but the product_name itself, so you must GROUP BY product_id, product_name and ORDER BY COUNT(product_id) DESC, LEN(product_name):
SELECT product_id, COUNT(*), product_name
FROM tablename
GROUP BY product_id, product_name
ORDER BY COUNT(product_id) DESC, LEN(product_name);
See the demo.

SQL Query: SELECT MAX SUM quantity

How do i combine a SUM and MAX in a single query?
Lets say i have a orderrule:
ProductID \ Quantity
I Could say:
Select ProductID,SUM(Quantity) AS Sold
FROM Orderrule
GROUP BY ProductID
ORDER BY SUM(Quantity) Desc
However that would return all sales, and not just the most sold product (with quantity).
Try this
SELECT TOP(1)
*
FROM
(
Select
ProductID,
MAX(Quantity) As MaxQuantity,
SUM(Quantity) AS Sold
FROM Orderrule
GROUP BY ProductID
)AS X
ORDER BY Sold DESC
So there are two ways to do it - first to have a limit on the number of results, something likes:
select * from (your_select) where rownum = 1
the other one is to pick the one with the the highest value, which will require a subselect, something like:
having sum(quantity) =
(select max(sum_quan) from (select sum(Quantity) from orderrule group by Product_id))
SELECT TOP 1 ProductID, Sold FROM
(
SELECT ProductID, SUM(Quantity) AS Sold
FROM Orderrule
GROUP BY ProductID
) totals
ORDER BY Sold DESC

Sorting Records on the Basis of Number of Items in a Group- SQL Server

I have a set of records and I want to sort these records on the basis of the number of items in a group.
I want to arrange the records in such a way that Products with maximum number of items are at the top i.e. the required order is- Product_ID 3 (with 6 items), then Product_ID 1 (with 5 items) and the last one would be Product_ID 2(with 3 items).
The following query returns the count of the items with same Product_ID, however, I want Item_Name, Item_Description and Item_Number to be arranged as well.
Select Product_ID, Count(*) from Product group by Product_ID order by Count(*) DESC
I have tried another query as follows, but I know I am wrong somewhere that it is not giving the desired results and I can't think of a possible solution:
Select Product_ID, Item_Name, Item_Description, Item_Number from Product
group by Product_ID,item_name,item_description,item_number
order by COUNT(product_ID)
Thanks in advance for your help!!
Select Product_ID, Item_Name, Item_Description, Item_Number
from Product
order by COUNT(1) over (partition by Product_ID) desc
I assume you want to group by the ID only but you want to list all other fields, you don't need to group by at all if you just want to order by:
SELECT product_id,
item_name,
item_description,
item_number
FROM product p1
ORDER BY (SELECT Count(product_id)
FROM product p2
WHERE p1.product_id = p2.product_id) DESC
Try using an alias:
Select Product_ID, Count(*) AS num_products from Product group by Product_ID order by num_products DESC;