I am trying to get the following out my firebird db:
I have records with a Timestamp attribute and an attribute with a currency value. Now I want query the currency value cumulated by month and now comes the tricky thing for me: I want each month starting from the first day at 16:30:00 p.m til the next first day in the next month at 16:29:59. I am not sure how to add this in the where condition or somewhere else? Thx for helping me!
eg.
Timestamp Value
...
2012-07-31 17:05:03 23,--
2012-08-01 12:23:15 15,--
cut:
2012-08-01 16:35:56 25,--
2012-08-02 12:23:15 5,--
2012-08-25 18:03:34 6,--
2012-08-31 08:03:55 9,--
2012-09-01 12:23:15 7,--
cut:
2012-09-01 16:47:43 3,--
2012-09-02 19:13:10 8,--
2012-09-10 19:13:10 18,--
2012-09-30 19:13:10 3,--
2012-10-02 19:13:10 8,--
....
July: 23,-- + 15,-- = 38,-
August: 52,--
September: 32,--
Oktober: 8,-- and so on...
You need to shift the date back (or forward) so that the start time of this month falls on 00:00 of the first of the month, and then simply group by the year and month part of the timestamp:
With a simple table I get the expected results using:
WITH shifteddate AS (
SELECT DATEADD(MINUTE, -30, DATEADD(HOUR, -16, infodate)) shifteddate, infovalue
FROM SUMEXAMPLE
)
SELECT EXTRACT(YEAR FROM shifteddate), EXTRACT(MONTH FROM shifteddate), SUM(infovalue)
FROM shifteddate
GROUP BY 1, 2
You could also use subquery instead of a CTE, but I just find this easier to read.
Related
I have a list of julian dates that I need to keep in order ex. 362, 363, 364, 365, 001, 002, 003. My query starts with getting the last julian date processed and each date after that. Right now it will max my lowest date out at 365 and I can't get the records that follow it. The same set of data also has a date field with the year attached but it doesn't seem to be helpful since those records won't be gathered until the rollover is corrected. Here is my simplified query:
select JulianDate, RecordDate
from table
where JulianField > #LowestJulianDate
and RecordDate between GetDate() and DateAdd(day, 6, GetDate())
Sample date:
JulianDate
RecordDate
362
2020-12-28
363
2020-12-29
364
2020-12-30
365
2020-12-31
001
2021-01-01
002
2021-01-02
003
2021-01-03
Desired output:
JulianDate
362
363
364
365
001
002
003
So if you'll imagine we start on day 362, our #LowestJulianDate is 362, and our record date range is today and the next 6 days, completing that list of julian dates.
How can I get the dates to go in order and resolve in a rollover?
You cannot by just using the "JulianDate" which is actually the DayOfYear value. You would need to also store the year that it refers to either separately or as part of the "JulianDate" value. For example, instead of "362" you need "2021362".
well why not sorting by year column and Julian date column ?
select JulianDate, RecordDate
from table
order by yearcolumn,JulianDate
What we are doing in the case of not having a year and wanting to sort a list on the year rollover for a 7 day rolling window is looking at the left 1 of the Julian day. If it's less than 3 roll it's rolled over. We sort into 2 baskets (old year and new year), order them, then recombine them with the new year's data being the "greatest" in the list.
We look at the left 1 because in our application, the last day of data we get may be 357 and the rollover may be 003 for example.
I want to retrieve the data between a 6 day time period.
The output I want is:
Date
--------
2019-05-01
2019-05-04
2019-06-01
2019-06-06
2019-07-01
This is my query so far:
select date from data d
where CAST(d.createdate as Date) between CAST('2019-05-01' as Date)
AND DATEADD(CAST(dd,6,'2016-07-01') as Date)
Why is this not retrieving the results I want?
You have several problems with your query.
The first is with your DATEADD statement which is all mixed up. You are not nesting the casted date into the statement properly. This is the corrected version:
DATEADD(dd, 6, CAST('2016-07-01' as Date))
The second is that your select projection refers to the column date which does not exist. Instead, you probably want your createdate column.
The third is that your between clause is back to front. You are saying between 2019-05-01 and 2016-07-01 but the smaller date must come first.
In fact, your given example is incorrect. In your question, you say "want to retrieve the data between two dates only for 6 days." So, why would you start with a date in 2016 and then jump to a date in 2019 and add 6 days to the date in 2019? If you want to use the DATEADD approach, you need to use the same date in both positions.
So here is your corrected query:
select d.createdate from data d
where CAST(d.createdate as Date) between CAST('2019-05-01' as Date)
AND DATEADD(dd, 6, CAST('2019-05-01' as Date))
I need to get the difference between 2 date time in minutes(Time difference in minutes). And the last difference will be calculated based on 6 PM of every date.
Sample data: need result of last column
User_Name Date Time difference in minutes
User 1 1/1/06 12:00 PM 30
user 2 1/1/06 12:30 PM 315
user 3 1/1/06 5:45 PM 15
Here the date will be always in same date and the last user date difference calculated based on default value 6PM. Assuming the dates of any user will not cross 6PM time.
Please suggest how to write the query for the same.
You could use the lead window function.
I assume your table is called mytable and the date column is mydate (it is a bad idea to call a column Date as it is a reserved word).
select user_name,
round((lead(mydate, 1, trunc(mydate)+18/24)
over (partition by trunc(mydate) order by mydate)
- mydate) *24*60) as difference
from mytable
I found the solution.. if its not correct let me know
SELECT User_name,created_date,
trunc(to_number((cast(nvl(lead (created_date,1) OVER (ORDER BY created_date),TRUNC(SYSDATE) + (19/24)) as date) - cast(created_date as date)))*24*60) as difference
FROM users;
I am writing a query to convert a character to Date Time
The following query extracts my time stamps in Character format.
select
(to_char(TO_CHAR(MDY(month(current- 1 units month), 1,year(current- 1 units month)),'%Y-%m-%d')||' 13:00:00')),
(to_char(TO_CHAR((DATE(DATE(extend(TODAY, YEAR TO MONTH)) - 1 UNITS DAY)+1),'%d-%m-%Y')||' 13:00:00'))
from dual
Output:
`T 0÷
2015-08-01 13:00:00 01-09-2015 13:00:00
2015-08-01 13:00:00 01-09-2015 13:00:00
Now I am trying to convert the Character to Time stamp using DATETIME(2001-12-31 15:32:55) YEAR TO SECOND function. I am getting syntax error.
select
DATETIME(to_char(TO_CHAR(MDY(month(current- 1 units month), 1,year(current- 1 units month)),'%Y-%m-%d')||' 13:00:00')) YEAR TO SECOND ,
DATETIME(to_char(TO_CHAR((DATE(DATE(extend(TODAY, YEAR TO MONTH)) - 1 UNITS DAY)+1),'%d-%m-%Y')||' 13:00:00') ) YEAR TO SECOND
from dual
How ever the following is working fine:
select DATETIME(2001-12-31 15:32:55) YEAR TO SECOND
from dual
Thanks in Advance. Please do not suggest answers for Oracle. its damn easy in Oracle.
Try using a CAST to convert your output as a DATETIME YEAR TO SECOND:
select
(to_char(TO_CHAR(MDY(month(current- 1 units month), 1,year(current- 1 units month)),'%Y-%m-%d')||' 13:00:00'))::DATETIME YEAR TO SECOND ,
(to_char(TO_CHAR((DATE(DATE(extend(TODAY, YEAR TO MONTH)) - 1 UNITS DAY)+1),'%Y-%m-%d')||' 13:00:00'))::DATETIME YEAR TO SECOND
from dual
That seems to work OK, but I'd suggest you don't do this. Also, note that DATETIME is an ISO standard: YYYY-MM-DD HH:MM:SS.FFF (or part thereof). Your second example with the date in English format is not going to parse to a DATETIME.
Your first algorithm is not leap-day safe, and the second example is horribly over-complicated. You can determine 13:00 on the first day of the current month using the far simpler construction:
(TODAY-DAY(TODAY)+1)::DATETIME YEAR TO SECOND + INTERVAL(13) HOUR TO HOUR
This also has the benefit of avoiding casting back and forth between DATE and CHAR. The calculation of the same time the previous month can be written as:
MDY(MONTH(TODAY-DAY(TODAY)),1,YEAR(TODAY-DAY(TODAY)))::DATETIME YEAR TO SECOND + INTERVAL(13) HOUR TO HOUR
... which will do the right thing on Feb 29/March 1 in a leap year, which your algorithm won't.
The construction TODAY-DAY(TODAY) will always produce the last day of the prior month.
I'm executing the next query in sql server 2012.
select *
from table
where date > convert(date, '2015/02/12')
order by date asc
but I'm getting the next set:
2015-02-12 06:40:42.000
2015-02-12 06:45:44.000
2015-02-12 06:48:15.000
2015-02-12 07:06:28.000
2015-02-12 07:26:46.000
...
I can fix this by changing the date to '2015/02/13', but I have the doubt about this behavior, why am I getting dates from feb 12 when I am specifying that I need only later dates ?. I also tried using cast('2015/02/12' as date), but I could not have the answer I was looking for
Because dates without times are intepreted as 12:00 midnight on that date. If you want only dates after February 12, 2015, then select all dates greater than or equal to February 13, 2015 (which again will be interpreted as midnight on February 13th).
select *
from table
where date >= convert(date, '2015/02/13')
order by date asc
why am I getting dates from feb 12 when I am specifying that I need only later dates ?
You're not specifying that you need only dates after Feb 12. You're asking for every row in which the value in the "date" column is greater than '2015-02-12'.
The value '2015-02-12 06:40:42.000' is greater than '2015-02-12'.
When comparing the date 2015/02/12 with your datetime data, this will implicitly compare the converted date 2015-02-12 00:00:000, the date at the beginning of the day with all of your data in column date.
But you are actually comparing datetime data, which has a time part as well, which gets compared.
And because you're comparing the beginning of the day (2015-02-12 00:00:000) with a value which is after it, for example 2015-02-12 06:40:42, all of the dates from will be displayed, because 6:40 AM is after (greater than) 0:00 AM.
Try this:
SELECT *
FROM TABLE
WHERE DATE >= DATEADD(SECOND, -1, '2015/02/13')
jarlh is right, though I'll clarify a little. Each of the "dates" you show above fall after 12:00 midnight starting 2015-02-12. They are actually timestamps.
If you don't want to see anything for the day specified in the filter, you add a day and use the greater-than-or-equal-to (>=) operator.
SELECT *
FROM table
WHERE (date >= DATEADD(d, 1, CONVERT(date, '2015/02/12')))
ORDER BY date ASC