Increase the number of rows in sql - sql

I am learning SQL. I have a Select-From command and the output result is limited to 25 rows. I would like to increase the number of rows to say 50 or 75 rows displayed. What syntax do I include in my Select - From code to make this possible?
I tried typing 50 in front of the Select * Command.

If you mean limiting query result, depending on your database you can use some keywords.
For example, for SQL SERVER you can get only first 50 rows via the command below:
select top 50 * from yourTable;
But if you are limiting your output display via an editor/IDE, you need to check manual of it.

Related

Stored procedure with limited number of rows

I am trying to build a stored procedure where I am able to extract data in bit size piece.
For example, if I have 1000 rows of data, I need to get it in batches of maybe first 10 or 100 and in the next run the next 10 or 100.
SELECT TOP 100 will only keep giving the first 100 rows but will ignore anything else.
Thanks,
Vicky

Using SQL Server query desired table output for the given table

I have a following table in SQL Server 2008 and it needs to be shown in a particular format. I was thinking of what approach should I follow to get desired output. Whether cursor needs to be used or not or multiple while loops to traverse each rows. This table has multiple accounts and dt_id is unique.
Kindly find the table below:
Desired output:
The problem statements is for eg in first table for first non zero dt_debit amount is 100 and I need to check whether this value cancels out in dt_credit column on FIFO method.
So in this case in first loop 100 gets compared to 500. So here it's less than
500 therefore it will go in the 2nd table.
Then it will go to 2nd value that is 400 and it will compare previous remaining balance that is 500 - 100 = 400 so again since its not greater again entry will go in desired output table.
Now for value 50 it will compare with 300 and 250 balance will be left. Next value is 300 therefore for 300 values will go twice as it will come in 250 + it will also take 50 from 80 dt_credit.
Can this logic be done in SQL Server. I am using SQL Server 2008. Any help would be appreciated.

Know the hidden row in LIMIT sql lite query

I am trying to analyse a sqllite database and I use these data for a bar chart. I will count and do the avg of age group by each value in each column, in this case Class with the limit of only first 100 distinct values.
An example of this table:
Age Class
25 Worker
30 Student
48 Spy
I use LIMIT 100 to limit the result. To add more information for user, I want to let user know the number of values didn't get in account and the hidden rows, is there anyway to achieve this?
Simple solution: I am not very familiar with sql so I think to do two queries, with and without LIMIT, count the number of rows and substrat each other to find the answer. But because I have 42 columns so I would be very happy if I can have another solution.
If you want all but the first 100 rows, you can combine LIMIT with OFFSET.
select * from test01 LIMIT 1000000 OFFSET 100;

In Sql Server 2014 ORDER BY clause with OFFSET FETCH NEXT returns weird results

I am currently using Sql Server 2014 Professional and the current version is (12.0.4100). I have a View and I am trying to SELECT 10 rows with specific offset.My View is like below:
BeginTime | EndTime | Duration | Name
09:00:00.0000000|16:00:00.0000000| 1 | some_name1
09:00:00.0000000|16:00:00.0000000| 2 | some_name2
09:00:00.0000000|16:00:00.0000000| 3 | some_name3
09:00:00.0000000|16:00:00.0000000| 4 | some_name4
09:00:00.0000000|16:00:00.0000000| 5 | some_name5
09:00:00.0000000|16:00:00.0000000| 6 | some_name6
09:00:00.0000000|16:00:00.0000000| 7 | some_name7
there are 100 rows like these and all have the exact same value in BeginTime and EndTime. Duration is incremented from 1 to 100 in related table. If query is only:
SELECT * FROM View_Name
ResultSet is correct. I can understand it by checking the duration column.
If I want to fetch only 10 rows starting from 0, ResultSet is correct and it is correct for starting from up to 18. When I want to fetch 10 rows starting from 19 or more than 19, Duration in ResultSet returns irrelevant results like Duration reversed. But it never returns the rows which has duration more than 11.
The query that I used to fetch specific rows is as follows:
SELECT * FROM View_Name ORDER BY BeginTime ASC OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY
There is also something strange in this situation; if I specify USE master, this problem disappears, but, if I specify USE [mydb_name], the problem appears again. By the way, I am using SQL SERVER 2014 Professional v(12.0.2269) in my local pc, this problem disappears for the above situation.
PS: I can not use USE master because, I am creating and listing the view dynamically, in Stored Procedures. Any help, answer or comment will be accepted. Thank You!
The documentation explains:
To achieve stable results between query requests using OFFSET and
FETCH, the following conditions must be met:
. . .
The ORDER BY clause contains a column or combination of columns that are guaranteed to be unique.
What happens in your case is that BeginTime is not unique. Databases in general -- and SQL Server in particular -- do not implement stable sorts. A stable sort is one where the rows are in the same order when the keys are the same. This is rather obvious, because tables and result sets represent unordered sets. They have no inherent ordering.
So, you need a unique key to make the sort stable. Given your data, this would seem to be either duration, name, or both:
SELECT *
ROM View_Name
ORDER BY BeginTime ASC, Duration, Name
OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY;
your order by should be unique,otherwise you will get indeterministic results(in your case ,begin time is not unique and your are not guarnteed to get same results every time).try changing your query to below to make it unique..
SELECT * FROM View_Name ORDER BY duration OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY
Further to add ,your first query (select * from view) result set is not guaranteed to be accurate every time unless you have an outer order by .

How to restrict displaytag to make database call on sorting?

I have a list of 1000 records, and I shown there 1000 records on page load using display tag.
I enabled sorting on some columns, and when I click on the table header to sort, display tag is making a database call and loading all 1000 records again.
How can we restrict to make database calls on sorting for display tag?
Because, we have loaded all the 1000 records, so could we make use of those list without loading the records again?
You are missing a fundamental: Pagination.
You should not load 1000 records at once; load 10 records at once instead (assuming 10 records is what you show in a page), and load the other 10 when changing page / sorting.
Filter them directly in the query (if you are using queries), for example, selecting page 3 (results from 21 to 30) in a query to a minimal "person table", ordered by name, would result it the following SQL:
Oracle
SELECT id, name, age
FROM ( SELECT id, name, age
FROM table_person
ORDER BY name )
WHERE ROWNUM BETWEEN 21 and 30
PostgreSQL
SELECT id, name, age
FROM table_person
ORDER BY name
LIMIT 10
OFFSET 20
and so on.
Be sure to read the documentation related to your database, and simply use query parameters to specify order by, starting row and ending row values.
you can use setFirstResults(), setMaxResults(),instead unnecessarily load all 1000 records.
divde per page max result shown 10 records only, and setFirstResult is restricted records first 5 from your records list.
Criteria cr= database_session.createCriteria(Records.class);
cr.addOrder(Order.desc("Recordsdate"));
cr.setFirstResult(5);
cr.setMaxResults(10);
all_records_list = (List<Records>) cr.list();