понедельник, 19 июня 2017 г.

SQL: collection of used queries - tips and tricks

1) Extract Random row which has only 1 record, for example ccid=756;

SELECT PACKAGENAME FROM WEB_PACKAGE_INFO HAVING COUNT(PACKAGENAME)=1 GROUP BY PACKAGENAME ORDER BY  dbms_random.value;

For another DB servers:
Select a random row with MySQL:
SELECT column FROM table
ORDER BY RAND()
LIMIT 1

Select a random row with PostgreSQL:
SELECT column FROM table
ORDER BY RANDOM()
LIMIT 1

Select a random row with Microsoft SQL Server:
SELECT TOP 1 column FROM table
ORDER BY NEWID()

Select a random row with IBM DB2
SELECT column, RAND() as IDX
FROM table
ORDER BY IDX FETCH FIRST 1 ROWS ONLY

Select a random record with Oracle:
SELECT column FROM
( SELECT column FROM table
ORDER BY dbms_random.value )
WHERE rownum = 1

2) Format date to view ISO8601 yyyy-mm-ddThh:mi:ss.mmmZ

select to_char(STARTDATE,'YYYY-MM-DD"T"HH24:MI:SS"Z"') as STARTDATE from WEB_PACKAGE_INFO;

3) It gives new random row every time when executed:
select * from
(select
PACKAGEREGION,PACKAGENAME, PACKAGE_ID, CAMCPCKGID, 
    to_char(STARTDATE,'YYYY-MM-DD"T"HH24:MI:SS"Z"') as STARTDATE,
    to_char(EXPIRATIONDATE,'YYYY-MM-DD"T"HH24:MI:SS"Z"') as EXPIRATIONDATE,
STATUS, PACKAGEID, CHANNELID, BOC, MAJOR, CALLSIGN, CCID, SOURCEID,
    to_char(CHSTART,'YYYY-MM-DD"T"HH24:MI:SS"Z"') as CHSTART,
    to_char(CHEND,'YYYY-MM-DD"T"HH24:MI:SS"Z"') as CHEND,
CSS, CHANNELSTATE, COUNTRY, SAT, SID, TEXT, CHANNEL_ID
from web_package_info where status='A' ORDER BY dbms_random.random)
where rownum = 1;

But such constraction won't give such result:
select
PACKAGEREGION,PACKAGENAME, PACKAGE_ID, CAMCPCKGID, 
    to_char(STARTDATE,'YYYY-MM-DD"T"HH24:MI:SS"Z"') as STARTDATE,
    to_char(EXPIRATIONDATE,'YYYY-MM-DD"T"HH24:MI:SS"Z"') as EXPIRATIONDATE,
STATUS, PACKAGEID, CHANNELID, BOC, MAJOR, CALLSIGN, CCID, SOURCEID,
    to_char(CHSTART,'YYYY-MM-DD"T"HH24:MI:SS"Z"') as CHSTART,
    to_char(CHEND,'YYYY-MM-DD"T"HH24:MI:SS"Z"') as CHEND,
CSS, CHANNELSTATE, COUNTRY, SAT, SID, TEXT, CHANNEL_ID
from web_package_info where status='A' ORDER BY dbms_random.random;

Комментариев нет:

Отправить комментарий

SEO: Эксперимент: как Яндекс и Google учитывают ключевые слова в URL

Эксперимент: как Яндекс и Google учитывают ключевые слова в URL Эксперимент: как Яндекс и Google учитывают ключевые слова в URL Дата пу...