Grouping by and counting but retrieve only one column - sql

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

Related

How to get latest record in GROUP BY in SQL

I write this query:
SELECT *
FROM `peer_settings`
WHERE `sorting_hub_id` LIKE '[\"172\"]'
GROUP BY product_id
ORDER BY `peer_settings`.`product_id` ASC
when I run this query I got Id 488 product
id 100
but I want id 46595 product id 100
You can try using the having clause
SELECT *
FROM `peer_settings`
WHERE `sorting_hub_id` LIKE '[\"172\"]'
GROUP BY product_id having max(id)
ORDER BY `peer_settings`.`product_id` ASC
in MySQL, you can order by more than one column:
SELECT *
FROM `peer_settings`
WHERE `sorting_hub_id` LIKE '[\"172\"]'
GROUP BY product_id
ORDER BY `peer_settings`.`product_id` ASC,`peer_settings`.`id` DESC
LIMIT 1

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

Create multiply function 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

Need to change LIMIT into something else

Is there a way to change "LIMIT 1" and get the same output? I have to get client's name, surname and a quantity of books that has the most books
SELECT stud.skaitytojas.name, stud.skaitytojas.surname,
COUNT (stud.skaitytojas.nr) AS quantity
FROM stud.egzempliorius , stud.skaitytojas
WHERE stud.egzempliorius.client = stud.skaitytojas.nr
GROUP BY stud.skaitytojas.nr
ORDER BY quantity DESC
LIMIT 1
Postgres supports the ANSI standard FETCH FIRST 1 ROW ONLY, so you can do:
SELECT s.name, s.surname, COUNT(s.nr) AS quantity
FROM stud.egzempliorius e JOIN
stud.skaitytojas s
ON e.client = s.nr
GROUP BY s.name, s.surname
ORDER BY quantity DESC
FETCH FIRST 1 ROW ONLY;
Also notice the use of table aliases and proper JOIN syntax. I also prefer to list the columns in the SELECT in the GROUP BY, although that is optional if s.nr is unique.
You can select the row with the highest quantity using row_number()
SELECT * FROM (
SELECT * , row_number() over (order by quantity desc) rn FROM (
SELECT stud.skaitytojas.name, stud.skaitytojas.surname, COUNT (stud.skaitytojas.nr) AS quantity
FROM stud.egzempliorius , stud.skaitytojas
WHERE stud.egzempliorius.client = stud.skaitytojas.nr
GROUP BY stud.skaitytojas.name, stud.skaitytojas.surname
) t
) t where rn = 1
If you want to include ties for the highest quantity, then use rank() instead.

SELECT MAX of COUNT

I have a table "well". It contains a column app_rate_unit (type: nvarchar).
My goal is to count every distinct value in the table and let the DBMS (MS Server 2005) give me the most occurring one.
This is my code:
SELECT MAX(app_rate_unit) AS MAX_APP
FROM (SELECT app_rate_unit, COUNT(*) AS co
FROM dbo.well AS w
GROUP BY app_rate_unit
) AS derivedtbl_1
The poblem with it is however, that my DBMS actually delivers the lowest count to me.
SideQuestion: How do I filter for a foreign key (in the table) and NOT NULL (in app_rate_unit) when counting?
select top 1 app_rate_unit, count(*) from dbo.well
group by app_rate_unit
order by count(*) desc
Try this
SELECT
COUNT(app_rate_unit)AS MAX_APP ,
app_rate_unit
FROM
dbo.well
WHERE
app_rate_unit IS NOT NULL
GROUP BY
app_rate_unit
ORDER BY
MAX_APP DESC
The above script will give you the count and the item. You can change the count if you are not sure only one item will have the maximum number of occurrence.
select top 1 count(*) as co from dbo.well as w group by app_rate_unit
order by count(*) desc
In PostgreSQL we can write query which using max of count as
select max(count) from (
select count(id) from Table _name group by created_by,status_id having status_id = 6 ) as Alias
eg
select max(count) from (
select count(id) from orders group by created_by,status_id having status_id = 6 ) as foo