CREATE TABLE FiveMinutesData (
TimeStamp datetime NOT NULL,
V_Ph1 float, V_Ph2 float, V_Ph3 float,
P_Ph1 float, P_Ph2 float, P_Ph3 float,
P_Ph1_Day float, P_Ph2_Day float, P_Ph3_Day float,
Flags int,
PRIMARY KEY (TimeStamp)
);
sqlite> select min(timestamp), max(timestamp) FROM fiveminutesdata group by strftime('%Y',datetime(TimeStamp,'localtime'));
1290948000|1647001800
sqlite> select min(timestamp), max(timestamp) FROM fiveminutesdata group by strftime('%Y',datetime(TimeStamp,'unixepoch'));
1290948000|1293812700
1293873900|1325347800
1325410500|1356970500
1357032600|1388507700
1388565900|1420070100
1420070700|1451606100
1451606400|1483228500
1483228800|1514764500
1514764800|1546300500
1546300800|1577836500
1577836800|1609458900
1609459200|1640994600
1640997300|1647001800
It looks that the yearly grouping with , when using localtime, is grouping everything in on line.
Is there an explanation for this behavior?
A work around ?
The column timestamp contains integer values which are unix epoch values, so you have to use the modifier 'unixepoch' and 'localtime':
SELECT strftime('%Y', datetime(timestamp, 'unixepoch', 'localtime')) year,
MIN(timestamp) min_timestamp,
MAX(timestamp) max_timestamp
FROM fiveminutesdata
GROUP BY year;
Or, simpler:
SELECT strftime('%Y', timestamp, 'unixepoch', 'localtime') year,
MIN(timestamp) min_timestamp,
MAX(timestamp) max_timestamp
FROM fiveminutesdata
GROUP BY year;
Related
I'm pulling data from my energy provider as a rawTable and then would like to perform some calculations to place the data in an easier way to search as well as to do calculations.
The data I receive from the energy supplier lands in this table:
CREATE TABLE "rawData" (
"ID" INTEGER NOT NULL,
"consumption" REAL NOT NULL,
"startTime" TEXT NOT NULL,
"endTime" TEXT NOT NULL,
PRIMARY KEY("ID" AUTOINCREMENT)
);
I then created this table to hold the data I want per day.
CREATE TABLE "structuredData" (
"rowID" INTEGER NOT NULL,
"Year" INTEGER NOT NULL,
"Month" INTEGER NOT NULL,
"Day" INTEGER NOT NULL,
"PeakConsumption" REAL,
"OffPeakConsumptioon" REAL,
PRIMARY KEY("rowID" AUTOINCREMENT)
);
To get the offpeak consumption from the rawData, I run the following query:
INSERT INTO structuredData (Year, Month, Day, OffPeakConsumptioon)
SELECT strftime('%d', startTime) as valDay,
strftime('%m', startTime) as valMonth,
strftime('%Y', startTime) as valYear,
SUM(consumption) as valTotalDay
FROM rawData
WHERE
strftime('%Y', startTime)>='2021'
AND (strftime('%H:%M:%S',startTime) >= "00:30:00"
AND strftime('%H:%M:%S',startTime) < "04:30:00")
GROUP BY valYear, valMonth, valDay ;
Then I need to essentially do the same to the Peak Consumption and came up with this query:
UPDATE structuredData
SET PeakConsumption = daily.amt
FROM (SELECT SUM(consumption) as amt, strftime('%d', startTime) as valDay, strftime('%m', startTime) as valMonth, strftime('%Y', startTime) as valYear
FROM rawData
WHERE strftime('%Y', startTime)>='2021'
AND (strftime('%H:%M:%S',startTime) >= "00:00:00" AND strftime('%H:%M:%S',endTime) <= "00:30:00"
OR strftime('%H:%M:%S',startTime) >= "04:30:00" AND strftime('%H:%M:%S',endTime) <="23:30:00")
GROUP BY valYear, valMonth, valDay) AS daily
WHERE structuredData.Day = daily.valDay
and structuredData.Month = daily.valMonth
and structuredData.Year = daily.valYear;
However, this tells me there's an error.
Execution finished with errors.
Result: near "FROM": syntax error
At line 1:
UPDATE structuredData
SET PeakConsumption = 1
FROM
Any ideas where I may be messing up??
I have a SQLite database with a counter and timestamp in unixtime as showed below:
+---------+------------+
| counter | timestamp |
+---------+------------+
| | 1582933500 |
| 1 | |
+---------+------------+
| 2 | 1582933800 |
+---------+------------+
| ... | ... |
+---------+------------+
I would like to calculate how 'counter' has increased in current day and current week.
It is possible in a SQLite query?
Thanks!
Provided you have SQLite version >= 3.25.0 the SQLite window functions will help you achieve this.
Using the LAG function to retrieve the value from the previous record - if there is none (which will be the case for the first row) a default value is provided, that is same as current row.
For the purpose of demonstration this code:
SELECT counter, timestamp,
LAG (timestamp, 1, timestamp) OVER (ORDER BY counter) AS previous_timestamp,
(timestamp - LAG (timestamp, 1, timestamp) OVER (ORDER BY counter)) AS diff
FROM your_table
ORDER BY counter ASC
will give this result:
1 1582933500 1582933500 0
2 1582933800 1582933500 300
In a CTE get the min and max timestamp for each day and join it twice to the table:
with cte as (
select date(timestamp, 'unixepoch', 'localtime') day,
min(timestamp) mindate, max(timestamp) maxdate
from tablename
group by day
)
select c.day, t2.counter - t1.counter difference
from cte c
inner join tablename t1 on t1.timestamp = c.mindate
inner join tablename t2 on t2.timestamp = c.maxdate;
With similar code get the results for each week:
with cte as (
select strftime('%W', date(timestamp, 'unixepoch', 'localtime')) week,
min(timestamp) mindate, max(timestamp) maxdate
from tablename
group by week
)
select c.week, t2.counter - t1.counter difference
from cte c
inner join tablename t1 on t1.timestamp = c.mindate
inner join tablename t2 on t2.timestamp = c.maxdate;
I have 'SchoolYearStartEnd' table
CREATE TABLE SchoolYearStartEnd (
id INT PRIMARY KEY UNIQUE,
StartDate DATE,
EndDate DATE
);
and the second 'SchoolYearsTeachingDays' table
CREATE TABLE SchoolYearsTeachingDays (
aDate DATE PRIMARY KEY UNIQUE
);
which I want to fill out with dates from a CTE like this:
WITH RECURSIVE dates(x) AS (
SELECT (SELECT StartDate FROM SchoolYearStartEnd)
UNION ALL
SELECT DATE(x, '+1 DAYS') FROM dates WHERE x < (SELECT EndDate FROM SchoolYearStartEnd)
)
SELECT * FROM dates WHERE CAST(STRFTIME('%w',x) AS INTEGER) > 0
;
I tried with this code here:
INSERT INTO SchoolYearsTeachingDays (aDate) VALUES (
WITH RECURSIVE dates(x) AS (
SELECT (SELECT StartDate FROM SchoolYearStartEnd)
UNION ALL
SELECT DATE(x, '+1 DAYS') FROM dates WHERE x < (SELECT EndDate FROM SchoolYearStartEnd)
)
SELECT * FROM dates WHERE CAST(STRFTIME('%w',x) AS INTEGER) > 0 -- To exclude Sundays.
;
);
but without success. I get these errors:
Error: near "RECURSIVE": syntax error
Error: near ")": syntax error
So what am I missing here?
Best, Pal
When you are inserting from a SELECT query, you must not use VALUES:
INSERT INTO SchoolYearsTeachingDays (aDate)
WITH RECURSIVE dates(x) AS (...)
SELECT * FROM dates ...;
If I query:
select max(date_created) date_created
on a datefield in PL/SQL (Oracle 11g), and there are records that were created on the same date but at different times, Max() returns only the latest times on that date. What I would like to do is have the times be ignored and return ALL records that match the max date, regardless of their associated timestamp in that column. What is the best practice for doing this?
Edit: what I'm looking to do is return all records for the most recent date that matches my criteria, regardless of varying timestamps for that day. Below is what I'm doing now and it only returns records from the latest date AND time on that date.
SELECT r."ID",
r."DATE_CREATED"
FROM schema.survey_response r
JOIN
(SELECT S.CUSTOMERID ,
MAX (S.DATE_CREATED) date_created
FROM schema.SURVEY_RESPONSE s
WHERE S.CATEGORY IN ('Yellow', 'Blue','Green')
GROUP BY CUSTOMERID
) recs
ON R.CUSTOMERID = recs.CUSTOMERID
AND R.DATE_CREATED = recs.date_created
WHERE R.CATEGORY IN ('Yellow', 'Blue','Green')
Final Edit: Got it working via the query below.
SELECT r."ID",
r."DATE_CREATED"
FROM schema.survey_response r
JOIN
(SELECT S.CUSTOMERID ,
MAX (trunc(S.DATE_CREATED)) date_created
FROM schema.SURVEY_RESPONSE s
WHERE S.CATEGORY IN ('Yellow', 'Blue','Green')
GROUP BY CUSTOMERID
) recs
ON R.CUSTOMERID = recs.CUSTOMERID
AND trunc(R.DATE_CREATED) = recs.date_created
WHERE R.CATEGORY IN ('Yellow', 'Blue','Green')
In Oracle, you can get the latest date ignoring the time
SELECT max( trunc( date_created ) ) date_created
FROM your_table
You can get all rows that have the latest date ignoring the time in a couple of ways. Using analytic functions (preferrable)
SELECT *
FROM (SELECT a.*,
rank() over (order by trunc(date_created) desc) rnk
FROM your_table a)
WHERE rnk = 1
or the more conventional but less efficient
SELECT *
FROM your_table
WHERE trunc(date_created) = (SELECT max( trunc(date_created) )
FROM your_table)
I have a database table field named User_Created_Date of type Varchar.I want to write a query to fetch all records where the difference between Today's date and User_Created_Date is greater than 31 days
pls help
Since your VARCHAR column's date format is DD/MM/YY, use:
select * from Your_Table
where DATEDIFF(day, CONVERT(datetime, User_Created_Date, 3), GETDATE()) > 31;