limited number of rows ( N rows )only while executing the DB2 Select Query in Perl Script - perlscript

How can I get limited number of rows ( N rows )only while executing the DB2 Select Query in Perl Script As we Do in DB2 i.e. Fetch N rows only ? How can We arrange the fetched rows as Ascending or Descending ?

Related

How to get count of rows and rows in one query?

Let's say I have a black box query that I don't really understand how it works, something along the lines of:
SELECT ... FROM ... JOIN ... = A (denoted as A)
Let's say A returns 500 rows.
I want to get the count of the number of rows (500 in this case), and then only return a limit of 50.
How can I wrote a query built around A that would return the number '500' and 50 rows of data?
You can use window functions (available in MySQL 8.0 only) and a row-limiting clause:
select a.*, count(*) over() total_rows
from ( < your query >) a
order by ??
limit 50
Note that I added an order by clause to the query. Although this is not technically required, it is a best practice: without an order by clause where the column (or set of columns) uniquely identifies each row, it is undefined which 50 rows the database will return, and the results may not be consistent over consecutive executions of the same query.
This is what SELECT SQL_CALC_FOUND_ROWS is intended to do.
SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name WHERE id > 100 LIMIT 10;
SELECT FOUND_ROWS();
The first query returns the limited set of rows.
The second query calls FOUND_ROWS() which returns an integer number of how many rows matched the most recent query, the number of rows which would have been returned if that query had not used LIMIT.
See https://dev.mysql.com/doc/refman/8.0/en/information-functions.html#function_found-rows
However, keep in mind that using SQL_CALC_FOUND_ROWS incurs a significant performance cost. Benchmarks show that it's usually faster to just run two queries:
SELECT COUNT(*) FROM tbl_name WHERE id > 100; -- the count of matching rows
SELECT * FROM tbl_name WHERE id > 100 LIMIT 10; -- the limited result
See https://www.percona.com/blog/2007/08/28/to-sql_calc_found_rows-or-not-to-sql_calc_found_rows/
There are a few ways you can do this (assuming that I am understanding your question correctly). You can open run two queries (and point a cursor to each) and then open and return both cursors, or you can run a stored procedure in which the count query is ran first, the result is stored into a variable, then it is used in another query.
Let me know if you would like an example of either of these

COUNT returns total rows ignoring LIMIT in MySQL, why?

Today while I was writing a complex query, accidently I found that even I have set LIMIT in my query MySQL server returns the total number of rows for COUNT.
Example:
SELECT COUNT(*) FROM `log` LIMIT 10;
Output:
5219
But if I run the query without COUNT it returns only 10 rows. My question is,
Why does MySQL ignore LIMIT when the COUNT is present?
LIMIT is for returning a subset of the total results, in your case the result is only one line so no effect
The purpose of LIMIT clause to select a limited number of records whereas count is aggregate function, which will return result of aggregation. It won't make any sense to use LIMIT and COUNT together as LIMIT may return any random n records.

Count Distinct vs Group By Discrepancy

I have a large table of 160 million rows. I am trying to get an accurate count of the unique phone numbers. I am getting the following results:
select count(distinct `number`) from customers;
Results = about 143 million rows
select `number` from customers group by `number`;
Results = about 4.3 million rows
When I have run similar count(distinct) and group by queries on other smaller tables in the past, I have received the same number of results between the two.
Any thoughts as to what is causing the discrepancy here?
Update:
I used awk to grab the number column out of the csv. I then ran these two commands in Linux:
sort temp | uniq > temp_unique
wc -l temp_unique
The results (about 4.3 million) were the same as the second select statement above.
I removed the index and rebuilt it and now the first count statement above returns the 4.3 million rows, so it does appear I had a corrupt index. I am curious why a corrupt index would show more results in the count versus less. Any thoughts on that? (I am using innodb btw.)

mysql query for table row range

This might be a very basic question but I am struggling with queying the specific rows in a table based only on the row range.
Let's say I have a table ABC where I have populated 1000 rows. Now I want a sql query so that I can fetch first 100 rows ( that is the range 1 to 100) and then the next 100 ( 101 to 200) and so on till I am done with all rows. And this should be done without querying/filtering on the table's id or any column id.
I am not able to figure it out as I am trained only on querying specific columns in WHERE clause so would appreciate if someone can plz help
You have to use the LIMIT clause in the SELECT query. MySQL allows you to set two parameters for the clause, the offset (first parameter) and the number of rows to fetch (second parameter).
SELECT * FROM `ABC` LIMIT 0, 100
SELECT * FROM `ABC` LIMIT 100, 100
SELECT * FROM `ABC` LIMIT 200, 100
-- etc...
However, you cannot guarantee the order of these rows unless you sort by one or more specific column(s) using the ORDER BY clause.
Read more about the SELECT statement here: http://dev.mysql.com/doc/refman/5.6/en/select.html
you can use limit in mysql.
limit accept 2 parameters.
this will return 1-10 records.
select * from abcd limit 10
this will return 10-20 records.
select * from abcd limit 10,10
this will return 20-30 records.
select * from abcd limit 20,10

MySQL Average of column where there are greater than 5 rows

Is there any way to use MySQL to only return an average if there are more than X rows?
I am currently using the following query:
SELECT round(AVG(a_points),1) as a from points where user_id=X
Can this be done in MySQL or do I have to do a row count first then execute this statement?
The table contains
user_id a_points b_points
So a user could have lots of b_points but only 4 a_points and I wouldn't want to average at that point.
Will it work for you ?
SELECT round(AVG(points),1) as a from points where user_id=X HAVING COUNT(*) >5