I have data below.
id: 1, created_at: "2022-01-01 02:00:00"
id: 2, created_at: "2022-02-02 23:00:00",
...
I need to group by created_at ("%Y-%m") after shift DateTime by adding 2hours.
Therefore after shifting hours, the date must be changed.
id: 1, created_at: "2022-01-01 04:00:00"
id: 2, created_at: "2022-02-03 01:00:00",
...
I can add hours using date_add function. But I don't know how to group by "%Y-%m" using created_at added hours in one query. Could you help me this?
select *, date_add(created_at, interval 2 hour) cr from clients where created_at between "${fromUtc}" and "${toUtc} order by created_at
Does this help?
SELECT
*,
DATE_ADD(created_at, INTERVAL 2 HOUR) cr
FROM
clients
WHERE created_at BETWEEN "${fromUtc}"
AND "${toUtc}"
GROUP BY YEAR(cr),
MONTH(cr)
ORDER BY created_at
Related
Would this be a correct/fast way to check if all days from the previous month are in my table? My Snowflake table has a 'date' column that is a 'date' type:
I'm doing this but I feel there must be a better way?
SELECT *
FROM dfp.revenue_allocation
WHERE YEAR("date") = '{{ execution_date.year }}'
AND MONTH("date") = '{{ execution_date.month }}'-1
To get best performance, expression on columns should be avoided:
SELECT *
FROM dfp.revenue_allocation
WHERE "date" >= TRUNC( CAST(? AS DATE) - INTERVAL '1 MONTH', 'MONTH')
AND "date" < TRUNC( CAST(? AS DATE), 'MONTH');
TRUNC
Truncates a date, time, or timestamp to the specified part
Here's the insert that I used:
db2.Insert(new QuizHistory()
{
QuizId = quiz,
Cards = 0,
Points = points,
UtcNow = (int)Math.Truncate(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds),
Viewed = 1,
Deck = deck
});
I tried looking at the different sql functions but now I am more confused than ever.
select * QuizHistory << but just for the last 24 hours.
As you are storing the date as seconds since january 1, 1970, a solution would be to use strftime :
select *
from QuizHistory
where UtcNow > strftime('%s', 'now', '-1 day')
i.e. with %s as format (seconds since 1970-01-01), for the now date with a -1 day modifier
I have a certain DATETIME value, and I would like to get the DATETIME value for a given weekday 'n' (where n is an integer from 1 thru to 7) that is just before the given date.
Question: How would I do this given a value for currentDate and a value for lastWeekDay?
For example, if given date is 06/15/2015 in mm/dd/yyyy format, then what is the date for a weekday of 6 that came just before 06/15/2015. In this example, given date is on Monday and we want the date for last Friday (i.e. weekday =6).
declare #currentDate datetime, #lastWeekDay int;
set #currentDate = getdate();
set #lastWeekDay = 6;--this could be any value from 1 thru to 7
select #currentDate as CurrentDate, '' as LastWeekDayDate --i need to get this date
UPDATE 1
In addition to the excellent answer by Anon, I also found an alternate way of doing it, which is as given below.
DECLARE #currentWeekDay INT;
SET #currentWeekDay = DATEPART(WEEKDAY, #currentDate);
--Case 1: when current date week day > lastWeekDay then subtract
-- the difference between the two weekdays
--Case 2: when current date week day <= lastWeekDay then go back 7 days from
-- current date, and then add (lastWeekDay - currentWeekDay)
SELECT
#currentDate AS CurrentDate,
CASE
WHEN #currentWeekDay > #lastWeekDay THEN DATEADD(DAY, -1 * ABS(CAST(#lastWeekDay AS INT) - CAST(#currentWeekDay AS INT)), #currentDate)
ELSE DATEADD(DAY, #lastWeekDay - DATEPART(WEEKDAY, DATEADD(DAY, -7, #currentDate)), DATEADD(DAY, -7, #currentDate))
END AS LastWeekDayDate;
Calculate how many days have passed since a fixed date, modulo 7, and subtract that from the input date. The magic number '5' is because Date Zero (1900-01-01) is a Monday. Shifting that forward 5 days makes the #lastWeekDay range [1..7] map to the range of weekdays [Sunday..Saturday].
SELECT DATEADD(day,-DATEDIFF(day,5+#lastWeekDay,#currentDate)%7,#currentDate)
I avoid the DATEPART(weekday,[...]) function because of SET DATEFIRST
I want to use pandas.date_range function as follows:
import pandas as pd
start_date = '2013-01-01'
end_date = '2014-03-01'
dates = pd.date_range(start_date, end_date, freq='M')
When I print dates, the first value in the range is '2013-01-31' instead of my defined start_date, while the last value jumps to the end of the month of the value defined by end_date. This happens with every kind of date I define.
print dates
# output:
#<class 'pandas.tseries.index.DatetimeIndex'>
#[2013-01-31 00:00:00, ..., 2014-03-31 00:00:00]
#Length: 15, Freq: M, Timezone: None
Am I doing something wrong?
The freq='M' indicates that the date range will use dates which are the ends of months. To use dates which are the start of months, use freq='MS'. There is a list of the available aliases and their meanings, here.
>>> pd.date_range(start_date, end_date, freq='MS')
<class 'pandas.tseries.index.DatetimeIndex'>
[2013-01-01, ..., 2014-03-01]
Length: 15, Freq: MS, Timezone: None
In my SQlite table, it has DateTime field example : order_date
How to perform these tasks:
Retrieve records by Month like Jan, Feb, Mar and so on in Year 2013.
Retrieve records by Begin date and End Date with criteria like above(1)
Thanks
How to convert `DateTime` for format requires by SQLite for (1) and (2)
DateTime Dt = new DateTime.Today();
Month-year ? Year?
Records = await db.QueryAsync("Select * From Purchase where order_date=" + "Month-Year");
In SQLite , which operator is correct : `=` or `==`?
----- Update:
using SQlite:strftime
SELECT * FROM tablename WHERE strftime('%Y', Order_Date) = '2013' AND strftime('%m', Order_Date) = '2';
1) Is there a function to get month as number from DateTime?
I assume you are using sqlite-net.
As such you may use parameters in your query:
//Returning all records from February 2013
Records = await db.QueryAsync<Purchase>("Select * From Purchase where order_date >= ? and order_date < ?", new DateTime(2013, 2, 1), new DateTime(2013, 3, 1));