Pagination Logic - Oracle - sql

As I'm new to oracle, not sure how to handle the pagination logic.
I need a query that returns the results based on the start vale and the limit value.
See the below resultset
id name
1 abc
2 def
3 ghi
4 jkl
5 qqq
6 www
7 eee
8 ttt
9 yyy
10 uuu
11 iii
12 ppp
13 aaa
14 sss
15 ddd
16 fff
17 ggg
18 hhh
19 jjj
20 kkk
For suppose if I give the values start value = 0 and limit = 5
then it should return the below records
id name
1 abc
2 def
3 ghi
4 jkl
5 qqq
if start value = 5 and limit = 15 , then I should return the below result
id name
5 qqq
6 www
7 eee
8 rrr
9 ttt
10 yyy
11 uuu
12 iii
13 ooo
14 ppp
15 ddd
16 fff
17 ggg
18 hhh
19 jjj
Please help me with the query
Please let me know if anything.
Thanks

Probably best to use bind variables for this. If you are not familiar with Oracle bind variables, spend 15 minutes reading about them, they are super helpful.
select id, name
from your_table
where id between :start_value and :start_value + :limit_value - 1;

Related

AWK Remove lines that have the same value in fields 1,...,n but differ in the value of the n+1 field except if they are unique

I have file1.tsv which looks like this:
1 ABC 10 XYZ Null Null
1 ABC 10 XYZ 1000 FFGG
1 ABC 10 XYZ 1001 FFHH
2 DEF 11 UVW Null Null
3 GHI 30 RST Null Null
3 GHI 30 RST 1002 JJKK
3 GHI 30 RST 1003 JJLL
I would like awk to print to file2.tsv the output:
1 ABC 10 XYZ 1000 FFGG
1 ABC 10 XYZ 1001 FFHH
2 DEF 11 UVW Null Null
3 GHI 30 RST 1002 JJKK
3 GHI 30 RST 1003 JJLL
That is, removing (not printing) line 1 and line 5 because they are not unique consdering ONLY the values of fields $1-$4 and because $5="Null"and $6="Null"
Thanks in advance.
Don't print lines where ABC or GHI matches Null.
awk '/ABC|GHI/~!/Null/' file
1 ABC 10 XYZ 1000 FFGG
1 ABC 10 XYZ 1001 FFHH
2 DEF 11 UVW Null Null
3 GHI 30 RST 1002 JJKK
3 GHI 30 RST 1003 JJLL

Find all parent and child with a relationship table

I have inherited two tables:
Table 1: Loc
LocID Field1 Field2
---------------------------
1 AAA BBB
2 CCC DDD
3 EEE FFF
4 GGG HHH
5 III JJJ
6 KKK LLL
7 MMM NNN
8 OOO PPP
9 QQQ RRR
10 SSS TTT
11 UUU VVV
Table 2: LocRel
LocID_A LocID_B
-----------------------
1 2
1 3
2 4
2 5
3 6
3 7
5 11
8 9
8 10
So, the LocRel table basically is used to specified the relationships between the various records in the Loc table.
I am struggling with writing a sql statement that will list all the relationships like so:
LocID Level LocIDH_Ancestry Field1 Field2
---------------------------------------------------
1 1 NULL AAA BBB
2 2 1 CCC DDD
3 2 1 EEE FFF
4 3 1,2 GGG HHH
5 3 1,2 III JJJ
6 3 1,3 KKK LLL
7 3 1,3 MMM NNN
8 1 NULL OOO PPP
9 2 8 QQQ RRR
10 2 8 SSS TTT
11 4 1,2,5 UUU VVV
I am not good at all in queries involving relationships, and would really appreciate some help on how the above can be achieved.
Thank you to all!
This answer may be SQL Server specific. I'm not an expert in SQL, so I don't know how much of this is standard and/or adopted in other dbms.
You can implement the kind of queries you mentioned, using recursive queries. Here is a intro to the subject that provides a good starting point.
For your specific query, something like this should do the work.
WITH MyCTE AS
(
SELECT
LocID, 1 as Level,
NULL as LocIDH_Ancestry,
Field1, Field2
FROM
Table1
WHERE
LocID NOT IN (SELECT LocID_B FROM Table2)
UNION ALL
SELECT
t1.LocID,
c.Level + 1 as Level,
t2.LocID_A as LocIDH_Ancestry,
t1.Field1, t1.Field2
FROM
Table1 t1
INNER JOIN
Table2 t2 ON t1.LocID = t2.LocID_A
INNER JOIN
MyCTE c ON t2.LocID_B = c.LocID
)
SELECT *
FROM MyCTE
Hope that helps.

SUM column values based on two rows in the same tables in SQL

I have one table like below in my SQL server.
Trans_id br_code bill_no amount
1 22 111 10
2 22 111 20
3 22 111 30
4 22 111 40
5 22 111 10
6 23 112 20
7 23 112 20
8 23 112 20
9 23 112 30
and I want desired output like below table
s.no br_code bill_no amount
1 22 111 110
2 23 112 90
try this:
select br_code, bill_no, sum(amount)
from TABLE
group by br_code, bill_no

DB2:how to get top

I have a table having data like
pin id name
3 33 jjj
2 22 bbb
1 111 aaaa
1 112 aa
1 113 aaa
4 44 kkk
I want to print rows of the table where if count(*) group by pin =1 (i.e single entry in table ) print the row
if count(*) group by pin >2 then print first two rows
so my out put should be
pin id name
3 33 jjj
2 22 bbb
1 111 aaaa
1 112 aa
4 44 kkk
Use row_number() OVER(partion by pin order by id) as rownum function . Where rownum <3
. As #Clockwork-Muse said, you need to define an order becase you need to say what do you want to see if there are more than 2 rows for a particular pin.
This will generate you desired output.

SQLite equivalent query of the given MSSQL query.

I've been trying to find a way to use the row_number over partition by, but couldn't find how to do so in SQLite.
I'm trying to convert the following the working query from MSSQL syntax to SQLite:
select TKG, hour, CAP, switch, Calls
from
(
select *,
row_number() over(partition by TKG order by cap desc) rn
from yourtable
) t1
where rn = 1
Here's an example with SQLFiddle: http://sqlfiddle.com/#!3/7bf9a/3
Basically, I have the following table:
TKG hour CAP SWITCH CALLS
AAA 7 45 HH 56
AAA 8 35 HH 76
AAA 9 25 HH 43
BBB 7 32 LL 5
BBB 8 43 LL 65
BBB 9 434 LL 65
CCC 7 54 JJ 43
CCC 8 564 JJ 43
CCC 9 54 JJ 65
ddd 7 10 MM 4
ddd 8 10 MM 3
ddd 9 10 MM 5
I need to order by the TKG with the max CAP so the output will look like this:
TKG hour CAP SWITCH CALLS
AAA 7 45 HH 56
BBB 9 434 LL 65
CCC 8 564 JJ 43
ddd 7 10 MM 4
Without knowing unique key it's really hard to do in readable way (no cross apply, no windowed functions). If you can afford to add id into your table, you can do:
select *
from yourtable
where
id in (
select min(y.id)
from yourtable as y
inner join (
select [TKG], max([CAP]) as [CAP]
from yourtable
group by [TKG]
) as y2 on y2.[TKG] = y.[TKG] and y2.[CAP] = y.[CAP]
group by y.[TKG]
)
sql fiddle demo
Considering myself lucky that I don't work in SQLite often :)