How to restrict displaytag to make database call on sorting? - sql

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();

Related

Increase the number of rows in 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.

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 .

Multicriteria Insert/Update

I'm trying to create a query that will insert new records to a table or update already existing records, but I'm getting stuck on the filtering and grouping for the criteria I want.
I have two tables: tbl_PartInfo, and dbo_CUST_BOOK_LINE.
I'm want to select from dbo_CUST_BOOK_LINE based upon the combination of CUST_ORDER_ID, CUST_ORDER_LINE_NO, and REVISION_ID. Each customer order can have multiple lines, and each line can have multiple revision. I'm trying to select the unique combinations of each order and it's connected lines, but take the connected information for the row with the highest value in the revision column.
I want to insert/update from dbo_CUST_BOOK_LINE the following columns:
CUST_ORDER_ID
PART_ID
USER_ORDER_QTY
UNIT_PRICE
I want to insert/update them into tbl_PartInfo as the following columns respectively:
JobID
DrawingNumber
Quantity
UnitPrice
So if I have the following rows in dbo_CUST_BOOK_LINE (PART_ID omitted for example)
CUST_ORDER_ID CUST_ORDER_LINE_NO REVISION_ID USER_ORDER_QTY UNIT_PRICE
SCabc 1 1 0 100
SCabc 1 2 4 150
SCabc 1 3 4 125
SCabc 2 3 2 200
SCxyz 1 1 0 0
SCxyz 1 2 3 50
It would return
CUST_ORDER_ID CUST_ORDER_LINE_NO (REVISION_ID) USER_ORDER_QTY UNIT_PRICE
SCabc 1 3 4 125
SCabc 2 3 2 200
SCxyz 1 2 3 50
but with PART_ID included and without REVISION_ID
So far, my code is just for the inset portion as I was trying to get the correct records selected, but I keep getting duplicates of CUST_ORDER_ID and CUST_ORDER_LINE_NO.
INSERT INTO tbl_PartInfo ( JobID, DrawingNumber, Quantity, UnitPrice, ProductFamily, ProductCategory )
SELECT dbo_CUST_BOOK_LINE.CUST_ORDER_ID, dbo_CUST_BOOK_LINE.PART_ID, dbo_CUST_BOOK_LINE.USER_ORDER_QTY, dbo_CUST_BOOK_LINE.UNIT_PRICE, dbo_CUST_BOOK_LINE.CUST_ORDER_LINE_NO, Max(dbo_CUST_BOOK_LINE.REVISION_ID) AS MaxOfREVISION_ID
FROM dbo_CUST_BOOK_LINE, tbl_PartInfo
GROUP BY dbo_CUST_BOOK_LINE.CUST_ORDER_ID, dbo_CUST_BOOK_LINE.PART_ID, dbo_CUST_BOOK_LINE.USER_ORDER_QTY, dbo_CUST_BOOK_LINE.UNIT_PRICE, dbo_CUST_BOOK_LINE.CUST_ORDER_LINE_NO;
This has been far more complicated that anything I've done so far, so any help would be greatly appreciated. Sorry about the long column names, I didn't get to choose them.
I did some research and think I found a way to make it work, but I'm still testing it. Right now I'm using three queries, but it should be easily simplified into two when complete.
The first is an append query that takes the two columns I want to get distinct combo's from and selects them and using "group by," while also selecting max of the revision column. It appends them to another table that I'm using called tbl_TempDrop. This table is only being used right now to reduce the number of results before the next part.
The second is an update query that updates tbl_TempDrop to include all the other columns I wanted by setting the criteria equal to the three selected columns from the first query. This took an EXTREMELY long time to complete when I had 700,000 records to work with, hence the use of the tbl_TempDrop.
The third query is a basic append query that appends the rows of tbl_TempDrop to the end destination, tbl_PartInfo.
All that's left is to run all three in a row.
I didn't want to include the full details of any tables or queries yet until I ensure that it works as desired, and because some of the names are vague since I will be using this method for multiple query searches.
This website helped me a little to make sure I had the basic idea down. http://www.techonthenet.com/access/queries/max_query2_2007.php
Let me know if you see any flaws with the ideology!

postgreSQL get index of a row that is outside the limit you searched for

I am new to SQL and I am not sure how to properly search my question so I will ask here.
Please see this link to see the SQL tables and queries I am working with
In this example there are 6 rows and I am limiting my search to start at the first index and give me at most 2. However, I would like to know the index of the row that has id 1.
When I use the query I describe in sqlfiddle, It shows me rows with id 5 and 23. But it doesn't include the row with id 1. However, I need to know the index of the row with id 1..
click here to see the full list
The link above prints out all of the rows and we can see that index 3 has the row containing id 1.
However I need to know that index without asking for the entire Array.
Why is this important? Well, lets say that we have 1 million rows. And if I ask for a million rows, that would mean allocating an array of one million. I could parse the array until I find the id I am looking for. However, allocating a million is way too costly.
Lets say for example that the row I am looking for resides in index 26, But I make my query so that it starts at index 0 and limits to 10. The array that I get from this query would not contain index 26. However I still need to know that it IS at index 26.
So this magic query would give me two things:
the top ten rows of the sorted rows
the index of a specified id (e.g. id of 1) regardless of its placement in the list.
Is this a possible query?
Clarification:
I use the word index to mean the row number.
If a we query a list of names from the db, we could get something like this:
bob
frank
dawn
then bob would be at index 0, frank would be at index 1 and dawn at index 2.
If I ORDER BY name ASC then the list of names would become
bob
dawn
frank
bob would be index 0 dawn would be index 1 and frank would be index 2.
I hope this makes things more clear.
If you want the row number, use the row_number() function:
SELECT *
FROM (SELECT ud.id, ud.team_name, ui.name, ui.date_created,
row_number() over (order by ui.name, ui.id) as rownumber
FROM user_data ud JOIN
user_infos ui
ON ui.id = ud.id
WHERE ui.date_created BETWEEN NOW() - INTERVAL '1 year' AND NOW()
) t
WHERE rownumber <= 10 or id = 1;
If you want them in order, just add order by rownumber as the last statement.