BigQuery Analytics current day query - google-analytics

I'm trying to write a query that could be executed every day to get up to date statistics for my analytic events.
Unfortunately my table path looks like this:
appname-72a9c.analytics_211719331.events_20210418
I can easily query data by providing hardcoded string, but is there a way to dinamically specify the date?
I tried writing something like this:
SELECT
e.param1, e.param2
FROM
FORMAT_DATETIME("appname-72a9c.analytics_211719331.events_%Y%m%d", CURRENT_DATE()) AS e
But it doesn't work.

You can parse the date string piece of your table name like this:
FROM `my_project.my_dataset.events_*`
WHERE parse_date('%Y%m%d', _table_suffix) = CURRENT_DATE() -1
This is a way to hardcode a query for yesterday, which you could customise as required.

Related

Date Function for Access Query

I am pulling data from 4 tables in a combination of 3 queries. All 3 queries contain one field that is common "PurchaseOrderNo", I also have a "DateUpdated" & "TimeUpdate" field I think I might be able to use for this issue. The final query produces some filtered data and only the information needed. I am trying to figure out how to specify the query to only produce new data results since the query was last run, if that makes sense. this is my SQL, ignore the filters in place already. Date format = MM/DD/YYYY Time format = ##.####
SELECT po_detail2.PurchaseOrderNo, po_detail2.VendorNo, po_detail2.ItemCode, po_detail2.LotSerialNo, IM068_MXPUnivProdCode.UDF_UNIQUE_KEY, Right([UDF_UNIQUE_KEY],1) AS SIZE_INDEX, Left([UDF_UNIQUE_KEY],Len([UDF_UNIQUE_KEY])-1) AS INVENTORY_KEY
FROM po_detail2 LEFT JOIN IM068_MXPUnivProdCode ON po_detail2.LotSerialNo = IM068_MXPUnivProdCode.LotSerialNo
WHERE (((po_detail2.PurchaseOrderNo)="0056334" Or (po_detail2.PurchaseOrderNo)>"0056334") AND ((po_detail2.ItemCode)="K500" Or (po_detail2.ItemCode)="PC55"))
ORDER BY po_detail2.PurchaseOrderNo DESC;
I ended up using a qry that would send a timestamp to a table then filtering the results since the last qry was run. This is my qry SQL.
INSERT INTO tblQueryLastRun ( dtmQueryLastRun )
VALUES (Now());

PeopleSoft Query Manager - 'count' function

I'm using the current version of PeopleSoft and I'm using their Query manager. I've built a query that looks at the job table and a customized version of the job table (so I can see future hires). In order to do this I've created a union. Everything works fine, except now I want to do a count of the job codes.
When I put in a count, I get an error. I don't know how to get it to work properly. I also don't really know how to using the 'having' tab.
I've attached some screenshots, including the SQL code.
SQL:
Having tab
You have a criteria in your query:
AND COUNT(*) = A.JOBCODE
Your job codes are string values that uniquely identify a job. It will never be equal to a count.
If you remove that criteria, your query will work:
The bigger issue is, what do you want to count? If your query was simply:
SELECT DEPTID, JOBCODE, COUNT(*)
This will give the count of employees in this department and job code. In your description, you said that you wanted the count of job codes. But each row has JOBCODE on it. The count of job codes on the row is one. What do you really want? The count of job codes in the database? The count of job codes in the result set?
If you want to get anything other than the count of rows within the group, you are not able to put that logic in PeopleSoft Query. You will need to create a view in AppDesigner and then you can add that to the query.

Snowflake GMT String to Date Conversion

We receive a string '2019-11-30T18:00:00GMT-06:00' in the JSON file and this need to be converted to timestamp to load into the timestamp column in the snowflake. I tried multiple options convert_timezone,to_timestamp etc, however in vain, Can you please let me know how i represent this string (2019-11-30T18:00:00GMT-06:00) in data format for comversion.
Thanks !
Leveraging a more Snowflake way to do this, you'd want to run something like this:
SELECT TO_TIMESTAMP('2019-11-30T18:00:00GMT-06:00','YYYY-MM-DDTHH:MI:SSGMT-TZH:TZM');
The output of this will be the date/time of your account default along with the timezone offset that goes along with that.
Please try the below mentioned approach
if your table is create as below
CREATE TABLE TIMESTAMP_TEST(DATE TIMESTAMP);
Insert the value as
INSERT INTO TIMESTAMP_TEST SELECT REPLACE(REPLACE('2019-11-30T18:00:00GMT-06:00','GMT'),'T',' ') FROM DUAL
Thanks

Teradata declaring dates at start of the script

This may seem like a very basic question.
I have a very large SQL statement with lots of sub queries that contain date limits in multiple where clauses
We run this query on an ad-hoc basis where i have to change the date range in the query in about 20 places. The date range is the same in all the places. So for example 1-Jan-2016 to 7-Jan-2016 as an example
In Teradata is it possible to declare the date range at the start of the query for example like a variable and then reference this variable in the code so i only need to change it once?
I have seen the answer for declaration of variable in teradata but would like to see a simple example demonstrating the concept for a date range in a stored procedure
Thank you for your time
Instead of making it a variable, it should probably be a parameter. Your stored proc would be something like this:
REPLACE PROCEDURE MyStoredProcedure
(
IN StartDate DATE
,IN EndDate DATE
)
BEGIN
DECLARE SomeOtherDate DATE; --if you need an actual date variable
--Your logic goes here
END;
Then you would set the parameters when you call the stored proc
CALL MyStoredProcedure('2015-01-01','2015-12-31');

Time diff calculations where date and time are in seperate columns

I've got a query where I'm trying to get the hours in duration (eg 6.5 hours) between two different times.
In my database, time and date are held in different fields so I can efficiently query on just a startDate, or endDate as I never query specifically on time.
My query looks like this
SELECT COUNT(*), IFNULL(SUM(TIMEDIFF(endTime,startTime)),0) FROM events WHERE user=18
Sometimes an event will go overnight, so the difference between times needs to take into account the differences between the dates as well.
I've been trying
SELECT COUNT(*), IFNULL(SUM(TIMEDIFF(CONCAT(endDate,' ',endTime),CONCAT(startDate,' ',startTime))),0) FROM events WHERE user=18
Unfortunately I only get errors when I do this, and I can't seem to combine the two fields into a single timestamp.
Pretty sure your problem is that your concatenated values are being sent to TIMEDIFF() as strings rather than DATETIMEs. Try calling the DATETIME function on them:
SELECT COUNT(*), IFNULL(SUM(TIMEDIFF(DATETIME(CONCAT(endDate,' ',endTime)),DATETIME(CONCAT(startDate,' ',startTime)))),0) FROM events WHERE user=18
I don't have a MySQL DB in front of my to test that, but I think that or some similar form of it is what you are looking for. There's an example of it in the MySQL docs involving MICROSECOND:
http://dev.mysql.com/doc/refman/5.0/en/datetime.html
Edit: Hmm... looks like TIMEDIFF is supposed to work with strings. Worth trying anyway.
TIMEDIFF(endDate,startDate) + TIMEDIFF(endTime,startTime)

Resources