Posts Tagged ‘PL/SQL’
[PLSQL] Select a random row from a database table
Friday, 5 February, 2010
1 comment
There is some way to select a random record or row from a database table.
SELECT col1, col2, col3 FROM (SELECT * FROM tcard SAMPLE(1) ORDER BY dbms_random.VALUE) WHERE branch = 1 CANCELDATE > SYSDATE AND ROWNUM <= 100);
[PL\SQL] Select a random row from a database table
Friday, 5 February, 2010
1 comment
There is some way to select a random record or row from a database table.
SELECT col1, col2, col3 FROM (SELECT * FROM tcard SAMPLE(1) ORDER BY dbms_random.VALUE) WHERE branch = 1 CANCELDATE > SYSDATE AND ROWNUM <= 100);
[PLSQL] Increasing results
Thursday, 27 August, 2009
Leave a comment
data
|
WITH t AS ( SELECT 01 mnth,10 sm FROM dual UNION ALL SELECT 02, 20 FROM dual UNION ALL SELECT 03, 30 FROM dual UNION ALL SELECT 04, 40 FROM dual ) SELECT mnth, sm, SUM(sm) over(ORDER BY mnth ROWS UNBOUNDED PRECEDING) FROM t;
result
|
[PL\SQL] Increasing results
Thursday, 27 August, 2009
Leave a comment
data
|
WITH t AS ( SELECT 01 mnth,10 sm FROM dual UNION ALL SELECT 02, 20 FROM dual UNION ALL SELECT 03, 30 FROM dual UNION ALL SELECT 04, 40 FROM dual ) SELECT mnth, sm, SUM(sm) over(ORDER BY mnth ROWS UNBOUNDED PRECEDING) FROM t;
result
|
[PLSQL] Pivot Query
Wednesday, 26 August, 2009
Leave a comment
data
|
WITH t AS ( SELECT 1 mnth, 11 city, 1 summa FROM dual UNION ALL SELECT 1 mnth, 22 city, 25 summa FROM dual UNION ALL SELECT 1 mnth, 33 city, 1 summa FROM dual UNION ALL SELECT 2 mnth, 11 city, 52 summa FROM dual UNION ALL SELECT 2 mnth, 22 city, 3 summa FROM dual UNION ALL SELECT 2 mnth, 33 city, 3 summa FROM dual UNION ALL SELECT 3 mnth, 11 city, 3 summa FROM dual UNION ALL SELECT 3 mnth, 22 city, 65 summa FROM dual UNION ALL SELECT 3 mnth, 33 city, 6 summa FROM dual ) select city , sum(decode(mnth, 1, summa, 0)) month1 , sum(decode(mnth, 2, summa, 0)) month2 , sum(decode(mnth, 3, summa, 0)) month3 , sum(sum(summa)) over(partition by city) total from t group by city;
result
|
[PL\SQL] Pivot Query
Wednesday, 26 August, 2009
Leave a comment
data
|
WITH t AS ( SELECT 1 mnth, 11 city, 1 summa FROM dual UNION ALL SELECT 1 mnth, 22 city, 25 summa FROM dual UNION ALL SELECT 1 mnth, 33 city, 1 summa FROM dual UNION ALL SELECT 2 mnth, 11 city, 52 summa FROM dual UNION ALL SELECT 2 mnth, 22 city, 3 summa FROM dual UNION ALL SELECT 2 mnth, 33 city, 3 summa FROM dual UNION ALL SELECT 3 mnth, 11 city, 3 summa FROM dual UNION ALL SELECT 3 mnth, 22 city, 65 summa FROM dual UNION ALL SELECT 3 mnth, 33 city, 6 summa FROM dual ) select city , sum(decode(mnth, 1, summa, 0)) month1 , sum(decode(mnth, 2, summa, 0)) month2 , sum(decode(mnth, 3, summa, 0)) month3 , sum(sum(summa)) over(partition by city) total from t group by city;
result
|
[PLSQL] How to Join on top (First) row
Wednesday, 26 August, 2009
1 comment
SELECT * FROM (SELECT ROWNUM t FROM DUAL CONNECT BY LEVEL < 6) JOIN (SELECT v, ROW_NUMBER() OVER(PARTITION BY v ORDER BY 1) rwnm FROM (SELECT 1 v FROM DUAL UNION ALL SELECT 1 FROM DUAL UNION ALL SELECT 2 FROM DUAL)) ON t = v AND rwnm = 1;
[PL\SQL] How to Join on top (First) row
Wednesday, 26 August, 2009
1 comment
SELECT *
FROM (SELECT ROWNUM t FROM DUAL CONNECT BY LEVEL < 6)
JOIN (SELECT v, ROW_NUMBER() OVER(PARTITION BY v ORDER BY 1) rwnm
FROM (SELECT 1 v
FROM DUAL
UNION ALL
SELECT 1 FROM DUAL
UNION ALL
SELECT 2 FROM DUAL)) ON t = v
AND rwnm = 1;
[/sourcecode]
performance
Tuesday, 18 August, 2009
Leave a comment
DECLARE l_start NUMBER; l_end NUMBER; BEGIN ... l_start := dbms_utility.get_time; ... l_end := round((dbms_utility.get_time - l_start) / 100, 2); dbms_output.put_line(l_end || ' seconds...'); END;
performance
Tuesday, 18 August, 2009
Leave a comment
DECLARE l_start NUMBER; l_end NUMBER; BEGIN ... l_start := dbms_utility.get_time; ... l_end := round((dbms_utility.get_time - l_start) / 100, 2); dbms_output.put_line(l_end || ' seconds...'); END;
Recent Comments