How can i apply group by after applying limit in mysql? - mysql

I am trying to run the below query but it is wrong as per MySQL syntax.
I want to select the first 100 rows then want to apply group by to it.
select customerid from customer
limit 100
group by customerid
How can I achieve it?

How about this? You need to add an aggregation column for it to work.
SELECT customerid
FROM (SELECT *
FROM customer
LIMIT 100) sub1
GROUP BY sub1.customerid;

Related

I am trying to do a Order by after a Limit Query

So this is my First Question here, so please forgive me if something is wrong.
I am trying to do Order by a limit query.
So there is a Customers table of 90 records.
I want Limit 3 records query to be done first and then sort this query records by a column called ContactName.
I think it would be a nested. The nested works fine in SQL server, but don't know how to do in MySQL.
Limit Query:
Select * from customers limit 3;
Order by after limit Query ( My guess, but this doesn't works):
Select * from (Select * from customers limit 3) Order by ContactName;
Please Help. Thanks
you just need this
Select * from customers Order by ContactName limit 3;
When you use a subquery in a FROM or JOIN clause, you're required to give it an alias.
Select *
from (Select * from customers limit 3) AS x
Order by ContactName;
Note that using LIMIT 3 without ORDER BY in the subquery doesn't really make much sense -- you don't know how it's going to choose the 3 customers to return. It would make more sense if you were choosing the 3 newest customers and then ordering them by name:
Select *
from (
Select *
from customers
order by registration_date DESC
limit 3) AS x
Order by ContactName;

How to Group By after Order By without any aggregate function

I'm trying to get the unique records after sorting the data in specific order.
The data table looks like this
SELECT * FROM category_products;
Now, requirement is to Order By the records on category_id and then select unique rows of product_id. I wrote the below query to sort the records
SELECT * FROM category_products ORDER BY category_id=2 desc;
When I try to Group By this sorted results with product_id, then the results aren't in right order. Even tried Group By clause in both child query and same query.
SELECT * FROM
(SELECT
*
FROM
category_products
ORDER BY category_id = 2 DESC) AS cat_products
GROUP BY cat_products.product_id;
Current Output:
Expected Output:
The result isn't the right one for which I'm looking for. Kindly help me guys, this doesn't seems to that tricky thing but I'm kind of stuck with it.
requirement is to Order By the records on category_id and then select
unique rows of product_id. I wrote the below query to sort the records
This should be what you are looking for:
SELECT DISTINCT product_id
,(SELECT category_id
FROM category_products
WHERE product_id = CP.product_id
ORDER BY category_id
LIMIT 1) AS cat_id
FROM category_products CP;
you need a SUBQUERY with an ORDER BY and LIMIT 1
It's a little unclear on how you aim to achieve this but from my understanding you can maybe create a custom sorting column to fulfill your requirement. See example below:
SELECT *,
CASE WHEN category_id=2 THEN 1
WHEN category_id=4 THEN 2
ELSE 99 END AS rownum
FROM category_products
ORDER BY rownum, product_id;
I'm using CASE to define a custom rownum and use it in the ORDER BY clause.
Demo fiddle here: https://www.db-fiddle.com/f/kAcGYtng2RWCf2Yhn5N1C9/0

How to get the total records details along with the query which is having a limit

How to get the total records details along with the query which is having a limit
My MySQL query is given below
SELECT name FROM employees LIMIT 10
which will give me 10 rows, but the total records will be around 6000. How can we get that also along with the above query
Use a subquery:
SELECT name, (SELECT COUNT(*) FROM employees) AS cnt
FROM employees
LIMIT 10
Use a Sub-query
SELECT nameFROM,
(select count(1) from employees) as total_count
From employees
LIMIT 10
or CROSS JOIN
SELECT nameFROM,total_count
From employees
CROSS JOIN (select count(1) total_count from employees) A
LIMIT 10
Adding ORDER BY to your query will make the result consistent
You can use SQL_CALC_FOUND_ROWS option in your query which will tell MySQL to count total number of rows disregarding LIMIT clause. You still need to execute a second query in order to retrieve row count, but it’s a simple query and not as complex as your query which retrieved the data. Usage is pretty simple. In you main query you need to add SQL_CALC_FOUND_ROWS option just after SELECT and in second query you need to use FOUND_ROWS() function to get total number of rows
SELECT SQL_CALC_FOUND_ROWS name FROM employees WHERE LIMIT 10
SELECT FOUND_ROWS();
(SELECT NAME, 1 AS 'Obs' FROM employees LIMIT 10)
union select 'RecsinTable', count(*) from employees

Is efficient the following SQL query?

Exist a better way to do what the following SQL query does? I have the feeling that table1 will be searched twice and may be that can be avoided with some trick and increase the efficient of the query, but I just can't figure out how ;( Here is the query (in MySQL):
SELECT a, SUM(count)
FROM table1
GROUP BY a
HAVING SUM(count) = (SELECT SUM(count) as total FROM table1 GROUP BY a ORDER BY total DESC LIMIT 1)
The goal is return the number(s) with the major accumulate, with its accumulate.
being table1 a two field table like:
a,count
1,10
1,30
1,0
2,1
2,100
2,4
3,10
4,50
4,55
The result with that data sample is:
2,105
4,105
Thanks in advance.
SELECT a, total FROM
(SELECT a AS a, SUM(COUNT) AS total
FROM table1
GROUP BY a) AS xyz
HAVING total = MAX(total)
Hope this will work for you
This sub-query is executed only once, and you don't have to bother with creating any pre-query as other answers may suggest (although doing so this is still correct, just not needed). Database engine will realise, that the sub-query is not using any variable dependent on the other part of the query. You can use EXPLAIN to see how the query is executed.
More on the topic in this answer:
https://stackoverflow.com/a/658954/1821029
I think you could probably do it by moving your HAVING sub-select query into its on prequery. Since it will always include a single row, you won't require any "JOIN", and it does not have to keep recomputing the COUNT(*) every time the HAVING is applied. Do it once, then the rest
SELECT
a,
SUM(count)
FROM
table1,
( SELECT SUM(count) as total
FROM table1
GROUP BY a
ORDER BY total DESC
LIMIT 1 ) PreQuery
GROUP BY
a
HAVING
SUM(count) = PreQuery.Total
This query return one row with two columns:
1- a list of comma separated values of "a" column, which have the biggest "Total"
2- and the biggest Total value
select group_concat(a), Total
from
(select a, sum(count) as Total
from table1
group by a) OnTableQuery
group by Total
order by Total desc
limit 1
Note that it queries table1 just one time. The query was already tested.

MySQL - using GROUP BY and DESC

In my SQL query I am selecting data with GROUP BY and ORDER BY clauses. The table has the same numbers across multiple rows with different times in each row. So I think I want to apply a GROUP BY clause.
However in the results return the oldest time with the number, but I need the most recent time.
SELECT * FROM TABLE GROUP BY (numbers) ORDER BY time DESC
The query appears as if it should first apply GROUP BY and then ORDER BY... but the results do not appear to work this way.
Is there any way to fix this?
SELECT *
FROM table t
WHERE time = (
SELECT max(time)
FROM table
WHERE t.numbers = numbers
)
work-around is to re-write the query as:
SELECT * FROM (SELECT * FROM table ORDER BY time DESC) AS t GROUP BY numbers;
SELECT * FROM table
WHERE time IN (
SELECT MAX(time)
FROM table
GROUP BY numbers
)
According to the manual you can add desc to the group by list:
Example:
group by item1, item2 desc, item3
with or without rollup.
I've tried this and it works in Ubuntu version 5.5.58. The reference page is:
https://dev.mysql.com/doc/refman/5.7/en/group-by-modifiers.html
SELECT * FROM TABLE GROUP BY numbers DESC;
This will give you last record from group.
Thanks