简体   繁体   中英

How to Select a substring in Oracle SQL up to a 4th char tyoe

I am trying to find a way in oracle SQL to take all the text after the 4th "/" and count how many is match. I need to do this in one SQL statement..

table name: access_log
col name: download
col name: time-stamp
value: Download file:/webdocs/data/groupXXX/case/03_28_54_9_0000011856.pdf

I am trying to end up with

case/03_28_54_9_0000011856.pdf

then a count of how many matches? can this be done?

I would go with this:

SELECT
    SUBSTR(DOWNLOAD, INSTR(DOWNLOAD, '/', 1, 4) + 1) AS FILENAME,
COUNT(SUBSTR(DOWNLOAD, INSTR(DOWNLOAD, '/', 1, 4) + 1)) AS DOWNLOADS
FROM ACCESS_LOG
GROUP BY SUBSTR(DOWNLOAD, INSTR(DOWNLOAD, '/', 1, 4) + 1)
ORDER BY DOWNLOADS DESC;

It returns this resultset:

|                       FILENAME | DOWNLOADS |
|--------------------------------|-----------|
| case/03_28_54_9_0000011856.pdf |         3 |
| case/04_28_54_9_0000011856.pdf |         2 |

For a demo see here: SQL Fiddle

try

SELECT SUBSTR(download, INSTR(download, '/', 1, 4)+1)  FROM access_log

to get the total try:

SELECT COUNT(*) , T1.file_name  
FROM (SELECT SUBSTR(download, INSTR(download, '/', 1, 4)+1) as file_name  
           FROM access_log) T1
GROUP BY T1.file_name  ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM