Create multiply function SQL - sql

I don't know if you can call it a multiply function, or function in function
I want to create output of productname number 5,6,7,8 from the small to the big one.
this output is from the big to the small
And i want to create the reverse output , create function that output the productname 5,6,7,8 asc
and later create another function that output 5,6,7,8 order by price desc
How to do it ? thanks !

you just add column name desc order and limit to get number of record
select * from products order by unitprice desc limit 5,4

RowNumber() will fix your issue
Row Number
WITH OrderedProducts AS
(
SELECT product_id, unit_price
ROW_NUMBER() OVER (ORDER BY unit_price DESC) AS RowNumber
)
SELECT product_id, unit_price
FROM OrderedProducts
WHERE RowNumber BETWEEN 4 AND 8;

If you want to skip the first, second, third and fourth items, then you can use the NOT IN clause. Somewhat like this :
Select Top 8 product_id, price, other_fields etc from Table1 Where product_id not in (select Top 4 product_id from Table 1 where filter_goes_here Order By product_id asc) Order By Price desc

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 - getting record with maximum value

I am trying to retrieve the product with the highest price:
SELECT ProductName, Price
FROM [Products]
ORDER BY Price DESC
LIMIT 1
I wanted to know if there is another way of doing this in a more efficient way, with MAX for example.
Use MAX and GROUP BY
SELECT ProductName, MAX(Price) [Price]
FROM [Products]
GROUP BY ProductName
ORDER BY MAX(Price) DESC
LIMIT 1;
I've always done it with the following
SELECT top 1 Name
FROM tableName
ORDER BY Price DESC
You can use TOP 1 but you always have to consider the possibility of having a tie, so:
SELECT TOP 1 WITH TIES ProductName, Price
FROM [Products]
ORDER BY Price DESC
select top 1 * from [Products] order by Price desc

Grouping by and counting but retrieve only one column

The following SQL code returns me two columns: 'product_id' and 'counter'.
I want it to return only 'product_id', is it possible?
select product_id, count(*) as counter
from accounts_products
group by product_id
order by counter DESC
limit 1
Just remove the counter column, and ORDER BY the count directly:
SELECT product_id
FROM accounts_products
GROUP BY product_id
ORDER BY COUNT(*) DESC
LIMIT 1