I want to select the count number and group by a period of time (month, week, day, hour , ...) . So for example I want to select the number of rows and group them by 24h.
My table is created as follow. The date is timestamp.
CREATE TABLE MSG
(
MSG_ID decimal(22) PRIMARY KEY NOT NULL,
MSG_DATE timestamp,
FILE_REF varchar2(32),
FILE_NAME varchar2(64),
MSG_TYPE varchar2(2),
);
CREATE UNIQUE INDEX PK_FEI_MSG ON MSG(MSG_ID);
I tried with this query. With length depending on the time period. But how can I group from now .
SELECT substr(MSG_DATE, 1,length),COUNT(*) as total FROM MSG GROUP BY substr(MSG_DATE, 1, length)
But how can I group the date from now + time_period ?
The SQL SELECT Statement The SELECT statement is used to select data from a database. The data returned is stored in a result table, called the result-set.
The SELECT TOP statement is used to limit the number of rows which returns the result of the query. For example, if want to retrieve only two rows from the table we can use the following query. Therefore, we can limit the result set of the query. In the following SQL examples, we will limit the result set of the query.
Select command is used to fetch the data in a set of records from a table, view or a group of tables, views by making use of SQL joins.
SELECT * FROM ( SELECT * FROM yourTableName ORDER BY id DESC LIMIT 10 )Var1 ORDER BY id ASC; Let us now implement the above query. mysql> SELECT * FROM ( -> SELECT * FROM Last10RecordsDemo ORDER BY id DESC LIMIT 10 -> )Var1 -> -> ORDER BY id ASC; The following is the output that displays the last 10 records.
You can group by the TO_CHAR function.
select to_char(msg_date, 'X')
,count(*)
from msg
group
by to_char(msg_date, 'X')
...where X is the format specification:
You can combine them pretty much however you like
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With