I have an sql statement:
select _WSTART startTime, _WEND endTime, avg(drp) drp, avg(intv) intv, avg(pdr) pdr, avg(dyp) dyp, avg(dy) dy from st_pptn where tm >= 1653619243000 interval(1h) fill(VALUE, 0)
It reported an error when executing:
Start(end) time of query range required or time range too large
I did not understand exactly what it means.
Related
I am trying to subtract one column from another column, and then sum the result. However, whatever I change I keep getting one of the two errors as mentioned in the title.
From all other questions posted, I derived that my statement should be something like:
SELECT SUM(EndTime-BeginTime) AS TotalTime FROM TimeRegister
WHERE OrderNumber = 00000 AND Activity = 11111;
As per suggestion in another topic, I changed the statement to:
SELECT SUM(CONVERT(varchar(EndTime-BeginTime), 108)) AS TotalTime FROM TimeRegister
WHERE OrderNumber = 00000 AND Activity = 11111;
However, I still receive the error.
The times are stored as follows:
1-1-1900 7:30:00
Thanks for any suggestion...
Edit 1: Oh, I am using a Microsoft Query through ODBC for SQL in Excel, kind of like:
Excel vlookup incorporating SQL table
Edit 2: I prefer to have the output again in the HH:MM format.
The thing is, when I copy the complete database to excel, everything works just fine. I can subtract the columns and sum up. I just dont want to that manually every day....
I just checked the SQL database, and the column is of Date/Time type.
First of all, trying to solve date or numeric issues by converting to text never solves anything, it adds additional problems like failed conversions or unexpected results.
SQL Server doesn't have a time interval type. If you subtract two datetime values you get back another datetime value that represents the difference as the offset since 1900-01-01. The following query :
select getdate() - dateadd(d,-1,getdate())
Will return :
1900-01-02 00:00:00.000
Which is 1 full day after 1900-01-01.
datetime values can't be summed though. SQL Server does have a time type but that just represents the time of day. Even if today - yesterday worked, converting that to time would return 00:00.
A quick solution is to use DATEDIFF to calculate the difference between two dates in whatever unit is required - hourss, minutes, seconds etc. Be aware though that DATEDIFF returns the number of interval boundaries crossed. DATEDIFF(DAY,...) between 11pm yesterday and 1am today will return 1, because 1 day boundary was crossed.
You can use
SELECT sum(datediff(minute,EndTime,BeginTime)) AS TotalMinutes
FROM TimeRegister
WHERE OrderNumber = 00000 AND Activity = 11111;
To calculate the difference in minutes and format it as a time interval on the client.
Another option is to cast the datetime to a float before summing, then back to datetime:
SELECT cast( sum(cast(EndTime - BeginTime as datetime)) as datetime) AS TotalOffset
FROM TimeRegister
WHERE OrderNumber = 00000 AND Activity = 11111;
A 2-day duration would appear as :
1900-01-03 00:00:00.000
This works because datetime can be cast to a float whose integral part represents the offset from 1900-01-01 and the fractional part the time.
A client written in a language like C# that does support intervals could subtract 1900-01-01 from this to get back the actual duration, eg :
TimeSpan duration = sumResult - new DateTime(1900,1,1);
Another option would be to avoid the final cast and just use the resulting float value as the number of days.
Displaying in Excel
The last option could be the easiest way to display the duration in Excel! Excel dates are actually stored as floats. What makes them appear as dates or times is the cell's formatting.
You can set a cells format to [h]:mm:ss to display any number as a duration. The [] is important - without it Excel will only display the time part of the "date".
If you enter 2 in a cell, h:mm:ss will show it as 0:00:00 while [h]:mm:ss will display 48:00:00, 48 hours.
Despite its strangeness,
SELECT sum(cast(EndTime - BeginTime as datetime)) AS TotalDuration
FROM TimeRegister
WHERE OrderNumber = 00000 AND Activity = 11111;
May be the best option to display a duration sum in Excel
You seem to want datediff():
SELECT SUM(DATEDIFF(second, BeginTime, EndTime) AS TotalTime
FROM TimeRegister
WHERE OrderNumber = 00000 AND Activity = 11111;
How about the following:
WITH totalTime AS (
SELECT SUM(DATEDIFF(second, EndTime, BeginTime)) as TimeInSecond
FROM TimeRegister
WHERE OrderNumber = 00000 AND Activity = 11111;
)
SELECT RIGHT('0' + CAST(TimeinSecond / 3600 AS VARCHAR),2) + ':' +
RIGHT('0' + CAST((TimeinSecond / 60) % 60 AS VARCHAR),2) + ':' +
RIGHT('0' + CAST(TimeinSecond % 60 AS VARCHAR),2) AS TimeElapsed
FROM totalTime;
In this solution, the time is converted into seconds, before being totalled, and then converted to HH:MM:SS.
I'm trying to compute Teradata queries execution time for a specific Teradata user. I currently have the following query inspired from there:
Calculating-the-actual-query-run-time
Query:
SELECT date
,a.username
,a.errorcode
,SUBSTR(b.sqltextinfo, 1, 15000)
,a.starttime
,a.firstresptime
,a.firststeptime
,((a.firstresptime - a.starttime) HOUR(4) TO SECOND(2)) AS elapsedtime
,((a.firstresptime - a.firststeptime) HOUR(4) TO SECOND(2)) AS executiontime
,elapsedtime - executiontime AS delaytime
FROM dbc.QryLogV a
INNER JOIN dbc.QryLogSqlV b ON a.procid = b.procid AND a.queryid = b.queryid
WHERE a.Username = 'xxx';
Sadly it's triggering the error:
Executed as Single statement. Failed [7453 : HY000] Interval field overflow.
Years ago the datataype for the timestamps in DBQL changed from TIMESTAMP(2) to TIMESTAMP(6), now when you try to get SECOND(2) in your result it overflows. To fix this either use SECOND(6) or ``SECOND`.
Btw, depending on your release, you'll find ElapsedTime & DelayTime pre-calculated in dbc.QryLogV.
New here and was wondering if anyone can shed some light into this ORA-00936 error with my SQL Query.
select Count(Auftragsnr) as Open_Orders
from wa_kopf
where convert(varchar(4), Aender_Datum, 104) = convert(varchar(4), getdate(), 104)
and zustand < (60);
Get the following error:
14:47:37 [SELECT - 0 row(s), 0.000 secs] [Error Code: 936, SQL
State: 42000] ORA-00936: Missing Expression
Any help would be greatly appreciated.
Regards
JRidge
You're using SQL Server syntax, which won't work with an Oracle database. You seem to be trying to format a date as dd.mm.yyyy (but in a 4-character string, somehow?). There is no getdate function in Oracle either, but it does have sysdate instead.
So the Oracle equivalent for what I think you're trying to do would be:
where to_char(Aender_Datum, 'dd.mm.yyyy') = to_char(sysdate, 'dd.mm.yyyy')
But that would just mean you're comparing dates as string, which isn't generally a good idea. If you're looking for any value from today you could use the trunc() function, which strips the time portion from a date by default; if your column is only representing a date and the time is always midnight then you can do:
where Aender_Datum = trunc(sysdate)
If your column has varying times then you could truncate that as well:
where trunc(Aender_Datum) = trunc(sysdate)
... but that can have a performance impact if the column is indexed, and an alternative is to use a range comparison:
where Aender_Datum >= trunc(sysdate)
and Aender_Datum < trunc(sysdate) + 1
Query
I'm getting a syntax error:
SELECT "LastFilterDate"
FROM "Filter Cartridge Updates"
WHERE "Filter Cartridge Updates"."LastFilterDate" < DATE_SUB(CURDATE(),INTERVAL 180 DAY)
I want to select the LastFilterDateS that are older than the current date minus 180 days (6 months).
I'm using LibreOffice base.
error in question:
SQL Status: HY000 Error code: 1000
syntax error, unexpected $end, expecting BETWEEN or IN or
SQL_TOKEN_LIKE
Sandesh gave a fantastic link on the subject, but I'm still getting a syntax error.
variations I've tried:
SELECT *
FROM Filter Cartridge Updates
WHERE LastFilterDate BETWEEN DATE( DATE_SUB( NOW() , INTERVAL 180 DAY ) ) AND DATE ( NOW() )
SELECT *
FROM "Filter Cartridge Updates"
WHERE "Filter Cartridge Updates"."LastFilterDate" BETWEEN DATE( DATE_SUB( NOW() , INTERVAL 180 DAY ) ) AND DATE ( NOW() )
The problem is that you're using MYSQL syntax for an SQL that is not MYSQL.
You can find some help on the LibreOffice website. I found this: =DATEDIF("1974-04-17";"2012-06-13";"d") which yields the number of days between 2 dates.
So you could change your where clause to be (if this allows substitution - you'll have to test that), to:
Where =DATEDIF(last_filter_date,current_date;"d") > 180
Note: I only put stubs in for the dates, you'll have to format those correctly.
I have a table which stores an amount of seconds as integer.
I want to display it and use it as Time or Date.
If I write this:
Select Cast(ColAmountofSeconds as Time) as ThisTime From MyTable;
same with:
Select Cast(ColAmountofSeconds as Date) as ThisTime From MyTable;
I get the following error:
Overflow occurred during data type conversion. conversion error from
string "14".
Note "14" is the value of the first row in the ColAmountofSeconds column.
This is so natural in SQL Server, that I can't believe the amount of time I've spent on figuring this out.
EDIT
I can't believe this is the answer:
Update MyTable
Set TIMESPENT = time '00:00:00' + ColAmountOfSeconds;
Firebird cast function does not support converting a numeric value to date, time or timestamp.
You can take advantage of the fact that Firebird supports arithmethic between dates and numeric values, so you can write your query like this:
select dateadd(second, ColAmountOfSeconds, cast('00:00:00' as time))
from myTable;
--or the equivalent:
select cast(cast('2013-01-01' as timestamp) + cast(ColAmountofSeconds as double precision) / 86400 as TIME)
from myTable;