简体   繁体   中英

selecting the rows based on the distinct column values

I have the following data in a table sample . I am using Oracle database.


column1      column2         column3
A             01           2017-07-27-04.24.58.851340
B             01           2017-07-27-06.43.19.654420
C             01           2017-08-10-08.26.47.633480

I need to pull out the count of distinct records based on SUBSTR(column3, 1,10) value.

For example:

In the first row, the SUBSTR(column3, 1,10) value of column3 is 2017-07-27 ,

In the second row, the SUBSTR(column3, 1,10) value of column3 is 2017-07-27 which is same as first row

In third row, the SUBSTR(column3, 1,10) value of column3 is 2017-08-10 which is different from first two rows.

So based on distinct SUBSTR(column3, 1,10) values, the count should be returned as 2 .

Can anybody help me out?

select count(distinct SUBSTR(column3, 1,10))
from sample

Since you appear to be storing timestamp values as strings (which you should not do, and ought to store them as timestamps) you can convert them to a timestamp and then truncate the time component to the start of the day:

SELECT   COUNT( DISTINCT TRUNC( TO_TIMESTAMP( column3, 'YYYY-MM-DD-HH24.MI.SS.FF6' ) ) )
           AS num_days
FROM     your_table

But you could also just use the string value:

SELECT   COUNT( DISTINCT SUBSTR( column3, 1, 10 ) ) AS num_days
FROM     your_table

If you convert your table to store them as timestamps then you can just use:

SELECT   COUNT( DISTINCT TRUNC( column3 ) ) AS num_days
FROM     your_table

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