Paginating result set in oracle database - sql

I have high transactional service with oracle db as backend.Many number of clients will be calling our service to get the data.When we receive the request for data, we need to query the db, get the result set and send them in the paginated way.I dont want the query.But wanted to know what really happens.say if the result set has 20,000 rows, and if we need to send 100 data per page in repsonse,how can i mention that there is remaining set of data, in the response,so that the client needs to hit our service to get the next next pages?.Say the response is in json format.How should the resposne format look like?.I'm new to oracle.Thanks for your help.

To select the data the paginated way, try
select order_id, order_descr
from (select order_id, order_descr, row_number() over(order by order_date desc) r
from orders
where customer_id = 123)
where r between 1 and 101
to display order 1 upto 100 (first page) of customer_id 123.
If you receive at client side more than 100 rows of data then more data exists.
The inner select statement with order by clause is necessary.

Related

SQL Multiple Conditions in max statement not working

I am attempting to filter my table and get the item that sold for the most amount of money. In order to do this I am using "AuctionOpen" to determine whether or not the auction is still open. The auction cannot be open and have the item been sold (later I will use this for the most expensive item available).
I am able to use the AND operator to compare AuctionOpen by using the following:
select s.*
from auctionsite.dbo.Auction s
where s.HighestBid = (select max(s2.HighestBid) from auctionsite.dbo.Auction
s2) and s.AuctionOpen = 0;
When I set this equal to zero I get results, but when I set it equal to 1, it only returns the column titles even though there are values set to 1 in the table.
Results when compared to 0:
Results when compared to 1:
Clearly, the highest bid is on a record where AuctionOpen <> 1.
I recommend using order by and fetch (or the equivalent in your database):
select s.*
from auctionsite.dbo.Auction s
where s.AuctionOpen = 0
order by s.HIghestBid desc
fetch first 1 row only
In SQL Server, use either select top (1) or offset 0 rows fetch first 1 row only.
I think you should try the Count aggregate function
here, try this:
**Select count(Item_name) As
[Item with the highest money]
from table_name
Group by Item_name DSEC;**
You can check my page hereSQL/MySQL tips for some SQL/MySQL lessons

find rejected items from the customer's order

I need to generate a report of the rejected items of an order, I have to do it when the order has finished being processed by de system and the conditions that I have to consider that the order has stopped being processed are:
The status of the order in the process is equal to or greater than 600
All items in the order were rejected and are in 999 status
I want to make an SQL query that considers the two previous conditions to bring me the rejecteds items from the order when it is no longer processed by the system.
scenario example:
so, I am trying them in the following way
select * from order_detail_status
where order_number = 'OR_001'
and process_status= '999'
and process_id = (select max(process_id) from configuracion.order_detail_status where order_number = 'OR_001' and process_status >= 600)
this would work if only scenario 1 existed, but for scenario 2 the request never reaches that status, so I am trying to add a second condition:
or (select distinct (process_status) from configuracion.order_detail_status where order_number = 'OR_002' ) = '999'
in the second condition I want to indicate that all the records of the order were rejected with the state 999, but it does not work for me, any suggestions?
If you want to find orders where ALL items have process_status of 999, then try something like this:
SELECT order_number, MIN(process_status) AS minps, MAX(process_status) AS maxps
FROM order_detail_status
GROUP BY order_number
HAVING minps=maxps AND minps=999
Grouping the lines by order and then doing min() and max() gives you the highest and lowest status for the order. If they match, then there is only one status for all items in the order. If the single status is 999 (or > 600), then you have the answer.
HAVING is like a WHERE condition but operates after the grouping is done.
Results:
OR_002 999 999

i have a set of account numbers to which i have to map 12 months data in respective months column

i have a set of account numbers .i need to map values for 12 months against the account numbers in their respective month.
While using a update query it throws more than 1 value is being thrown .
please suggest.
upate balances
set inrambfeb14=feb14.inramb
from feb14
where balance.accountno=feb14.accountno
with cte as ( select accountNo,max(inramb) inramb from feb14 group by accountNo )
update balances set inrambfeb14 = cte.inramb
from cte join balances bal on bal.accountno = cte.accountno
This will give you max value to update...
for more suitable answers you should give some input data & your expectation clearly
There must be multiple records for one account, and hence the error.
you must have some logic to get distinct inramb data per account to run this successfully.
Like the one vignesh is suggesting i.e. max of data.

SQL Query results pagination with random Order by in SQL Server 2008

I am trying to implement pagination to a page on my website that returns results from a database table.
Currently, it returns all rows in a random order. However, as my database is growing, I want to paginate these results instead of displaying them all on one page. However, I don't want to return all results just to display 20 records for instance. Depending on the page, I want take just the 20 records from the database that are relevant.
I'm following this tutorial: Tutorial
However, the I cannot use the query with the OFFSET clause, because the hosting uses SQL SERVER 2008. (It is introduced in 2012 i believe).
I tried following the answer to this Question, but I want the results in a random order, and I cannot do an ORDER BY on a derived table... so I'm a bit stuck for ideas!
Any help? Thanks!
This is what I currently have:
SELECT Title, Filename, PhotoURL, Orientation, FolderName, SetURL, RowNum
FROM (
SELECT p.Title, p.Filename, p.URL AS PhotoURL, p.Orientation, s.FolderName, s.URL AS SetURL, ROW_NUMBER() OVER (ORDER BY p.PhotoID) AS RowNum
FROM Photos p
LEFT OUTER JOIN SetPhotos sp
ON sp.PhotoID = p.PhotoID
LEFT OUTER JOIN [Sets] s
ON s.SetID = sp.SetID
WHERE p.Hide = 0
ORDER BY NEWID()
) AS PaginatedPhotos
WHERE PaginatedPhotos.RowNum BETWEEN 0 AND 10
Add integer column 'order' to your table
Write a code that fills this column in all rows with unique random numbers
Run this code from time to time to shuffle your rows
Make pagination as usual while sorting by 'order'
Keep in mind that the same rows can appear on different pages if you shuffle rows in the middle of someone paginating.
Just select TOP(pagesize). Since your order is random, requesting page=2 does not result in the page 2 of the original result that displayed page 1. In other words when the order is random and changes each time then page 1 is always correct for any page requested.

SQL Server 2008 query - paging from JSF

I am new to SQL Server 2008, and I need some help in query for pagination logic on my JSF page.
Here is my query:
select *
from ShipmentHistory sh
where sh.CCIPortID in ?
order by sh.TrackingNumber, sh.CreateTimeStamp
for the first time I need to get only 100 records from db and when ever the user clicks on next on my JSF page I need to fetch next 100 records only. Am keep tracking of user clicks on the page i.e next and previous button.
Thanks for your time.
First, you'll need to perform a count of your result set, so you know how many total records you have, and how many pages will be displaying the data.
Next, you'll need to specify the start and end rows that you want to retrieve, and then use a query similar to the following to pull that chunk of your results.
SELECT * FROM (
SELECT
*,
ROW_NUMBER() OVER (ORDER BY sh.TrackingNumber, sh.CreateTimeStamp) AS rn,
FROM ShipmentHistory sh
WHERE sh.CCIPortID in ?
) AS ordered
WHERE rn >= #startRow AND rn < #endRow
Note: Don't use SELECT *, replace this with the actual columns that you need to return.