I have a report in DataStudio and I can't solve the following problem:
I have a datasource (1), an SQL query, like the following:
SELECT
user_id,
user_name
FROM
users;
I have another datasource (2), with a query similar to this one:
SELECT result
FROM (
SELECT func1 (#user_id, #DS_START_DATE, #DS_END_DATE) as result
UNION ALL
SELECT func2 (#user_id, #DS_START_DATE, #DS_END_DATE) as result
UNION ALL
SELECT func3 (#user_id, #DS_START_DATE, #DS_END_DATE) as result
UNION ALL
...
UNION ALL
SELECT func27 (#user_id, #DS_START_DATE, #DS_END_DATE) as result
)
func1, func2, etc. are all stored-functions within BigQuery. Each of them performs a different and complex calculation, which returns a number. Altogether there are 27 functions
I would like to be able to select the user in the datasource (1) and at this moment execute the datasource (2) passing the "user_id" as a parameter for each of the functions.
How can I do something like that?
I tried to use custom parameters, but it forces me to manually inform each of the "users_ids" and I would like this to come automatically from a database table (datasource[1]).
Some help?
Thanks
Related
I have multiple CTEs and I want to pass in two parameters: one being the date and the other being the CTE table name.
This is in oracle db.
The CTEs are like this
WITH big_table1 AS
(
select * from mini_table1
),
big_table2 AS
(
select * from mini_table2
)
The date will be calculated by the select statements in the CTE. I want to do something like select * from &&big_table where &&date = date or any other way (in a view/store procedure) that would help me pass in dynamically the CTE name and a specific date (like '10-23-2021').
I have tried this and it works: select * from &&big_table where &&date = date. But it needs to be in a store proc or view or something that is established I guess. Not really sure why but those are the requirements I need to satisfy.
Appreciate any help.
We are using a query similar to the one below for a report
SELECT
visitId AS visitId,
hits.hitNumber AS hits_hitNumber,
hits.time AS hits_time,
hits.page.pagePath AS hits_page_pagePath,
-- hit scope custom dimensions
(SELECT value from hits.customDimensions where index=1) AS CD1,
(SELECT value from hits.customDimensions where index=2) AS CD2,
-- user and session level custom dimensions
(SELECT value from sessions.customDimensions where index=3) AS CD3
FROM `ga_sessions_20191031` AS sessions, UNNEST(hits) as hits
ORDER BY visitId, hits_hitNumber
LIMIT 50
The query uses un-nesting to flatten some of the custom dimensions. However the index values are hard coded in the query. So every time there is a new custom dimension defined, the query needs to be updated. Is it possible to use a subquery to select all available distinct index values and add them to the query dynamically ?
EDIT:
The following queries provide distinct index values : Is there a way to link them in first query ?
(hit scope )
SELECT
DISTINCT cds.index as hit_cd_index
FROM `ga_sessions_20191031` AS sessions, UNNEST(hits) as hits, UNNEST(hits.customDimensions) as cds
ORDER BY hit_cd_index
(user and session scope )
SELECT
DISTINCT session_cds.index as session_cd_index
FROM `ga_sessions_20191031`, UNNEST(customDimensions) as session_cds
ORDER BY session_cd_index asc
the most robust solution would be to add a table into your BigQuery dataset containing data from Management API so you'll be able to construct your select based on values from the most recent custom dimensions list: https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtReference/management/customDimensions/list
I would like to knew if if possible build a query statement who read the same table from 2 different partition.
In my scenario I have two partition (partition_A and partition_B) and a table "consume" , in nowadays I do this statement :
Select id, item
From partition_A(consume)
union all
Select id, item
From partition_B(consume);
but I would like to obtain the same result when I use this statement :
Select id, item from consume;
Is it possible ?
tks.
Do you want to like this?
Select id, item
From consume PARTITION partition_A(name of partition)
union all
Select id, item
From consume PARTITION partition_B;
Maybe I didn't understand your question.
Since two days ago (August 10th 2016), a query which used to work (using tables of the BQ Export for Google Analytics Premium) has stopped working. It returns the following error:
Error: Cannot union tables : Incompatible types.
'hits.latencyTracking.userTimingVariable' : TYPE_INT64
'hits.latencyTracking.userTimingVariable' : TYPE_STRING
After some investigation, it seems to be a problem with using IN in a WHERE clause when I query tables from before and after August 10th (table ga_sessions_20160810).
I've simplified my original query to provide a dummy one which has the same basic structure. The following query works (querying data from 2016-08-08 and 2016-08-09):
SELECT fullVisitorId, sum(totals.visits)
FROM (select * from TABLE_DATE_RANGE([XXXXXXXX.ga_sessions_],TIMESTAMP('2016-08-08'),TIMESTAMP('2016-08-09')))
WHERE fullVisitorId in(
SELECT fullVisitorId
FROM TABLE_DATE_RANGE([XXXXXXXX.ga_sessions_],TIMESTAMP('2016-08-08'),TIMESTAMP('2016-08-09'))
)
GROUP BY fullVisitorId
But this other one (just changing dates, in this case from 2016-08-09 and 2016-08-10) returns the error:
SELECT fullVisitorId, sum(totals.visits)
FROM (select * from TABLE_DATE_RANGE([XXXXXXXX.ga_sessions_],TIMESTAMP('2016-08-09'),TIMESTAMP('2016-08-10')))
WHERE fullVisitorId in(
SELECT fullVisitorId
FROM TABLE_DATE_RANGE([XXXXXXXX.ga_sessions_],TIMESTAMP('2016-08-09'),TIMESTAMP('2016-08-10'))
)
GROUP BY fullVisitorId
This last query works fine either if I delete the WHERE clause or if I just try the query within the IN, so I guess the problem is with the structure WHERE field IN(...). Furthermore, querying only data from 2016-08-10 does work. Also, the same happens using a field different to fullVisitorId and running the same queries in different BQ projects.
Looking to the error description, it should be a problem with variable types, but I don't know what is hits.latencyTracking.userTimingVariable. My query used to work properly, so I can't figure out what has changed that produces the error. Have some fields changed their type or what happened?
Has anyone experienced this? Is this a bug or a new behavior in BigQuery? How can this error be solved?
As you are using * in select clause it might causing problem when union is happening its trying to combine two different column types ( as schema changed from INT64 to STRING).
I have two approaches
1) use only those fields required by you than using * in select clause
SELECT fullVisitorId, sum(totals.visits)
FROM (select fullVisitorId,totals.visits from TABLE_DATE_RANGE([XXXXXXXX.ga_sessions_],TIMESTAMP('2016-08-09'),TIMESTAMP('2016-08-10')))
WHERE fullVisitorId in(
SELECT fullVisitorId
FROM TABLE_DATE_RANGE([XXXXXXXX.ga_sessions_],TIMESTAMP('2016-08-09'),TIMESTAMP('2016-08-10'))
) GROUP BY fullVisitorId
2) using views to split inner queries and use the view later in the query. (even in view you need to use only use those fields which are required )
SELECT fullVisitorId, sum(totals.visits)
FROM [view.innertable2]
WHERE fullVisitorId in(
SELECT fullVisitorId from [view.innertable1] ) GROUP BY fullVisitorId
This will exclude the hits.latencyTracking.userTimingVariable so there will be no error.
If the fields that you are querying are compatible, you may try using Standard SQL wildcard tables (you'll have to uncheck use Legacy SQL box if you are doing this from the UI). Something like this:
SELECT fullVisitorId, sum(totals.visits)
FROM `xxxxxxxx.ga_sessions_*`
WHERE _TABLE_SUFFIX BETWEEN '20160808' and '20160810'
GROUP BY fullVisitorId;
I have created a View:
CREATE VIEW Lunch
AS
SELECT 'Beer' AS item
UNION SELECT 'Olives'
UNION SELECT 'Bread'
UNION SELECT 'Salami'
UNION SELECT 'Calamari'
UNION SELECT 'Coffee';
GO
Then I executed the following query in a different query.
SELECT item FROM Lunch
This is resulting in the data from the view being returned. There are not any underlying tables to hold data. But still the system is showing records. How is this possible?
I don't fully understand the question. The view is not "holding data", it is holding a query. The query statement has constants in it that are turned into a result set that can be used by other queries.
You can think of a query as substituting the text of the query directly into a statement. So, when you do:
select *
from lunch;
SQL really processes this as:
select *
from ( SELECT 'Beer' AS item
UNION SELECT 'Olives'
UNION SELECT 'Bread'
UNION SELECT 'Salami'
UNION SELECT 'Calamari'
UNION SELECT 'Coffee'
) t
This is a good model of what happens, although it is not quite what really happens. What really happens is that the view is compiled and the compiled code is inserted into the compiled code for the query.
There is another concept of "materialized views". These are views where you have indexes and the values are actually stored in the database. However, this is not an example of a materialized view.
It doesn't actually hold data, but it can evaluate SQL, which is all you're doing in the definition.
So the following can be evaluated:
SELECT 'Beer' AS item
UNION SELECT 'Olives'
UNION SELECT 'Bread'
UNION SELECT 'Salami'
UNION SELECT 'Calamari'
UNION SELECT 'Coffee';
The same as a view defined with:
SELECT * FROM Customers
SQL View Reference:
A view can be thought of as either a virtual table or a stored query. The data accessible through a view is not stored in the database as a distinct object. What is stored in the database is a SELECT statement. The result set of the SELECT statement forms the virtual table returned by the view. A user can use this virtual table by referencing the view name in Transact-SQL statements the same way a table is referenced.
A view is a Saved Select Statement in your case view definition does not have any underlying tables, but the select statement itself has hard coded (Constant) values.
Therefore it will always return this data.
Again to be clear it is not storing any data but the Select Statement which happens to return some constant values.
Yes, the data is shown because is statically included into the query itself.
I don't see anything uncommon here, the view refers to the query that embed the data .