How to pull data from the past 3 days, not including today's and yesterday's from Bigquery - wildcard

Using standars SQL. When the table consists of wildcard tables.
I Try to use this:
SELECT *
FROM `Resourse.Reports_Reg.Session_streaming`
WHERE
SUBSTR(_table_suffix, 0, 6) =
FORMAT_DATE("%E4Y%m", DATE_SUB(CURRENT_DATE, INTERVAL 3 day))
This request, I thought, should return data for the last 5 days. But I still do not understand what data it returns to me. How to pull data from the past 3 days, not including today's and yesterday's

As it is, you are not selecting anything into _table_suffix given that your query has no wildcard.
If your table have for instance a name structure like:
`Resourse.Reports_Reg.Session_streaming_20170820`
where the very last string is a date with format "%Y%m%d", then selecting past third and second days can be done like:
SELECT
*
FROM `Resourse.Reports_Reg.Session_streaming_*`
WHERE _TABLE_SUFFIX BETWEEN FORMAT_DATE("%Y%m%d", DATE_SUB(CURRENT_DATE, INTERVAL 3 day)) AND FORMAT_DATE("%Y%m%d", DATE_SUB(CURRENT_DATE, INTERVAL 2 day))
Notice the wildcard "*" is selecting the date "20170820" for instance. After that, there's just a where clause selecting appropriate dates.

Related

SQL does not show me the time (hour by day)

SQL does not show me the time, I have a business day table, and I need that for each business day it shows me a time range from 9 to 19 hours to then show the number of transactions per hour, for each day of the month.
I can not generate stored procedure only query can be performed not to alter the database by company policy
I have a query like that.
SELECT
TO_CHAR(cd.days, 'yyyymmdd') AS fechaTransaccion,
NVL(SUM(data.numTransacionExitosa),0) AS numTransacionExitosa,
NVL(SUM(data.numTransactionPendiente),0) AS numTransactionPendiente
FROM
calendardays cd
LEFT JOIN
data
ON
cd.days = data.settledate
WHERE
cd.DAYs BETWEEN ADD_MONTHS(TRUNC(to_date(20200201, 'yyyymmdd'), 'MM'),-1) AND ADD_MONTHS(LAST_DAY(TRUNC(to_date(20200201, 'yyyymmdd'))),-1)
and substr(SETTLEDATE, 10, 2) between 9 and 19
GROUP BY
cd.days, substr(SETTLEDATE, 10, 2)
ORDER BY
cd.days
i need show like this
Hard to understand/answer without seeing your data, but if you need to extract hour from date, follow the below example:
select extract(hour from cast(sysdate as timestamp)) hour from dual;

SQLite strftime() weekday

I have been trying with no success to to count how many values were created in a specific week day:
SELECT count(*) as count FROM packets WHERE strftime("%w", timeIn) = '1';
I have this values in timeIn
1472434822.60033
1472434829.12632
1472434962.34593
I don't know what I am doing wrong here.
furthermore, if I use this:
SELECT count(*) as count FROM packets WHERE strftime("%w", timeIn) = '6';
I get
2
which makes no sense. Thank you in advance.
You appear to be storing the date as the number of seconds since 1970 (the Unix epoch) - a common representation. The time strings accepted by the SQLite date functions (see the Time Strings section) default to interpreting numeric time strings as a Julian day numbers:
Similarly, format 12 is shown with 10 significant digits, but the date/time functions will really accept as many or as few digits as are necessary to represent the Julian day number.
You can see this with the following SELECT:
SELECT strftime('%Y-%m-%d', 1472428800.6) AS t
the result of which is:
4026-48-26
For your date representation to be interpreted as a Unix epoch, you need to include 'unixepoch' in the strftime call:
SELECT strftime('%Y-%m-%d', 1472428800.6, 'unixepoch') AS t
which returns:
2016-08-29
If you modify your SELECT to be:
SELECT count(*) as count FROM packets WHERE strftime("%w", timeIn, 'unixepoch') = '6'
you should see results more inline with your expectations.

How do you update an existing date to a random date in a range?

In one of my tables I have datetime field in which the data in the table column is populated with something like "2016-01-07 01:33:00".
What I want to do is change ONLY the date to a random date within a range (ie: 2016-02-01 thru 2016-02-28) without changing the time. The end result might be "2016-02-13 01:33:00".
What mysql command string would accomplish this task?
Something like
UPDATE someTable SET someDate = DATE_ADD(
someDate,
INTERVAL
DATEDIFF(rangeStart, someDate) +
ROUND(RAND()*DATEDIFF(rangeEnd, rangeStart))
DAY
);
where someTable.someDate is your existing data, and rangeStart and rangeEnd are the boundaries of your target date range.
Here you take the initial date, add enough days to it to reach the range start, and then further add a random number of days no greater than the number of days in your target range.
In MsSQL it could be:
select dateadd(day,cast((RAND() * 30) as int),getdate())
Substitute getdate() with your input date.
(RAND() * 30) is used to randomly generate a number of days up to 30.

How to add/subtract date/time components using a calculated interval?

I'd like to get this to work in Teradata:
Updated SQL for better example
select
case
when
current_date between
cast('03-10-2013' as date format 'mm-dd-yyyy') and
cast('11-03-2013' as date format 'mm-dd-yyyy')
then 4
else 5
end Offset,
(current_timestamp + interval Offset hour) GMT
However, I get an error of Expected something like a string or a Unicode character blah blah. It seems that you have to hardcode the interval like this:
select current_timestamp + interval '4' day
Yes, I know I hardcoded it in my first example, but that was only to demonstrate a calculated result.
If you must know, I am having to convert all dates and times in a few tables to GMT, but I have to account for daylight savings time. I am in Eastern, so I need to add 4 hours if the date is within the DST timeframe and add 5 hours otherwise.
I know I can just create separate update statements for each period and just change the value from a 4 to a 5 accordingly, but I want my query to be dynamic and smart.
Here's the solution:
select
case
when
current_date between
cast('03-10-2013' as date format 'mm-dd-yyyy') and
cast('11-03-2013' as date format 'mm-dd-yyyy')
then 4
else 5
end Offset,
(current_timestamp + cast(Offset as interval hour)) GMT
You have to actually cast the case statement's return value as an interval. I didn't even know interval types existed in Teradata. Thanks to this page for helping me along:
http://www.teradataforum.com/l081007a.htm
If I understand correctly, you want to multiply the interval by some number. Believe it or not, that's literally all you need to do:
select current_timestamp as right_now
, right_now + (interval '1' day) as same_time_tomorrow
, right_now + (2 * (interval '1' day)) as same_time_next_day
Intervals have always challenged me for some reason; I don't use them very often. But I've had this little example in my Teradata "cheat sheet" for quite a while.
Two remarks:
You could return an INTERVAL instead of an INT
The recommended way to write a date literal in Teradata is DATE 'YYYY-MM-DD' instead of CAST/FORMAT
select
case
when current_date between DATE '2013-03-10' and DATE '2013-11-03'
then interval '4' hour
else interval '5'hour
end AS Offset,
current_timestamp + Offset AS GMT

SQLite : obtain current week

I'm trying to obtain the current week for date comparison in SQLite.
I have no problem for last month, last year, today, yesterday... but don't find the solution to have the current week.
I tried lot of things like:
SELECT tastings.* FROM tastings
WHERE (DATE(tastings.date) > DATE('now','weekday 1','+ 7 days'))
Can you help me ? Thanks.
This code gives you the week number where the first day of week is monday. It also works well for last and first weeks of the year.
strftime('%W', 'now', 'localtime', 'weekday 0', '-6 days')
I guess you want compare 2 date, Assume you have a table named _testTbl and have 3 column _id INTEGER, _name TEXT, _recordDate TEXT
you want name that record this week
you can use below code:
SELECT * FROM _testTbl
WHERE _recordDate > datetime('now', 'start of day', 'weekday 6', '-7 day')
note that this week start by saturday (sunday 0, monday 1, ..., saturday 7)
this t-sql means:
datetime is a sqlite date and time function.
first parameter is given time: 'now' means the current time.
second parameter take the time to start of day.
third parameter take time to the next weekday number (in this case, saturday).
fourth parameter take time to start of week
What is stored inside the tastings.date column? Note that SQLite does not have “timestamp” type affinity, so probably you store Text (some representation of the date) or integer (julian day, epoch…)
All time and date functions expect a valid time string and convert that time string to another string format. If tastings.date contains a week number then use:
AND cast(tastings.date AS TEXT) = strftime('%W','now')
This helps me to compare the 2 dates using the week of the year.
AND ( strftime('%W', tastings.date) = strftime('%W', 'now') )
Thanks you.

Resources