How to select from views bigquery? - view

I want to retrieve my real time Google Analytics data from Bigquery views. Attached is my current queries:
select datetime(timestamp_micros(visitstarttime*1000000),"Asia/Jakarta") as ga_datetime
, device.operatingSystem as ga_os
, trafficSource.source as ga_source
, trafficSource.medium as ga_medium
,COUNT(DISTINCT CONCAT(CAST(fullvisitorid AS string),CAST(visitid AS string))) AS ga_session
from `43864393.ga_realtime_sessions_view_201810*`
where _TABLE_SUFFIX between '01' and '31'
group by 1
, 2
, 3
, 4
i am using Standard SQL and Bigquery returns
Views cannot be queried through prefix
update:
I have the following views in my bigquery:
ga_realtime_sessions_view_20181017
ga_realtime_sessions_view_20181018
ga_realtime_sessions_view_20181019
and counting until ga_realtime_sessions_view_20181031
i want to select from all of those views.
How do i resolve this?

You cannot use wildcard character with views.If you still want to combine the results, you may want to look at UNION ALL
You can use UNION ALL in standard SQL to combine the results from multiple tables/views.
Here is an example query using UNION ALL
WITH subQ1 AS (SELECT column1 FROM view_1),
subQ2 AS (SELECT column1 FROM view_2)
SELECT * FROM subQ1
UNION ALL
SELECT * FROM subQ2;
Refer to the documentation here
UNION ALL

Related

Multi values search from one field PLSQL

I'm creating a report in PLSQL.
The report is working fine and additionally, I need to add when users search multiple values from one field separating by ;. It should select all the data that he entered.
Example: Salesman - Amanda; Michelle; Sharmain
Then it should select data regarding amenda, michelle and sharmain.
My code only returns data for one value only.
please refer this line from the code and ifsapp.Customer_Order_API.Get_Salesman_Code(i.order_no) LIKE '&Salesman'
where i.catalog_group='FPMB'
and i.order_no like 'M%'
and ((i.invoice_date between to_date( '&From_Date', 'YYYY/MM/DD' ) and to_date( '&To_Date', 'YYYY/MM/DD' ) ) or ('&From_Date' is null and '&To_Date' is null))
and t.source_ref1=i.order_no
and (t.source_ref3=i.release_no)
and (t.source_ref2=i.line_no)
and i.contract=t.contract
and t.transaction_code='OESHIP'
and t.qty_reversed=0
and t.source_ref1=i.order_no
and t.serial_no <> '*'
and t.cost<>0.00
and i.order_no LIKE '&Order_No
and ifsapp.Customer_Order_API.Get_Salesman_Code(i.order_no) LIKE '&Salesman'
You can use hierarchy query as follows:
ifsapp.Customer_Order_API.Get_Salesman_Code(i.order_no) IN
(SELECT TRIM(REGEXP_SUBSTR('&Salesman','[^;]+',1,LEVEL))
FROM DUAL CONNECT BY TRIM(REGEXP_SUBSTR('&Salesman','[^;]+',1,LEVEL)) IS NOT NULL)

Oracle pagination sql using rownum and order by unique column , order by makes the sql very slow

I have a typical oracle pagination sql called from a web application like this.
SELECT *
FROM
(SELECT * ( Very complex inner queries )
FROM xyz table
ORDER BY unique_colunn DESC ==> killer
)
WHERE rownum >= 50 and rownum<100
The sql works fine(returns data) within 2 or 3 seconds , but once the order by clause is introduced, it kills the query , it takes 200+ secs , but i cannot remove the order by unique column , because that is the one that drives the pagination logic , since it is a inline view not able to add any tuning hints , any pointers?
Have tried rank() , row_num over etc instead of using order by in the where condition as suggested , nothing works.

How to query view in oracle with non column in view

I have created a view as
create or replace view view_emp as
select empno, ename, job
from emp;
and I want to query view_query with non column in view.
select empno, ename
from view_emp
where deptno=10;
When Im trying to query like this it is giving me an error saying
ORA-00904: "TEST_EMP"."DEPTNO": invalid identifier
I have a requirement like this example. Could any one can give me solution for my requirement.
Actually I work on ERP Solutions Inventory Modules.
I have a requirement to find the unit wise , prodct wise stock opening balance and closing balance in date range.
I want a view like
create view stk_bal
as
select unit_code,prod_code, sum(opening_bal) opening_bal, sum(closing_bal) closing_bal
from
(
select unit_code, prod_code, ob_qty opeing_bal, 0 closing_bal
from (table 1) join (table 1.1)
where crtd_date between '02-feb-2013' and '02-mar-2013'
union all
select unit_code, prod_code, 0 , grn_qty
from (table 2) join (table 2.1)
where grn_date between '02-feb-2013' and '02-mar-2013'
union all
select unit_code, prod_code, 0, stk_transfer_in_qty
from (table 3) join (table 3.1)
where stk_trnin_date between '02-feb-2013' and '02-mar-2013'
)
group by unit_code, prod_code;
and I created view by making union all with 3 set of tables where i had hardcoded the dates
But i want to query the view in date range which has no date column in it.
Now I want to query the the view stk_bal in the date range.
You need to add deptno in your view definition:
create or replace view view_emp as
select empno, ename, job
,DEPTNO
from emp;
But of course, the real question is: what exactly is your requirement and why do you need a view.

pl-sql include column names in query

A weird request maybe but. My boss wants me to create an admin version of a page we have that displays data from an oracle query in a table.
The admin page, instead of displaying the data (query returns 1 row), needs to return the table name and column name
Ex: Instead of:
Name Initial
==================
Bob A
I want:
Name Initial
============================
Users.FirstName Users.MiddleInitial
I realize I can do this in code but would rather just modify the query to return the data I want so I can leave the report generation code mostly alone.
I don't want to do it in a stored procedure.
So when I spit out the data in the report using something like:
blah blah = MyDataRow("FirstName")
I can leave that as is but instead of it displaying "BOB" it would display "Users.FirstName"
And I want to do the query using select * if possible instead of listing all the columns
So for each of the columns I am querying in the * , I want to get (instead of the column value) the tablename.ColumnName or tablename|columnName
hope you are following- I am confusing myself...
pseudo:
select tablename + '.' + Columnname as WhateverTheColumnNameIs
from Table1
left join Table2 on whatever...
Join Table_Names on blah blah
Whew- after writing all this I think I will just do it on the code side.
But if you are up for it maybe a fun challenge
Oracle does not provide an authentic way(there is no pseudocolumn) to get the column name of a table as a result of a query against that table. But you might consider these two approaches:
Extract column name from an xmltype, formed by passing cursor expression(your query) in the xmltable() function:
-- your table
with t1(first_name, middle_name) as(
select 1,2 from dual
), -- your query
t2 as(
select * -- col1 as "t1.col1"
--, col2 as "t1.col2"
--, col3 as "t1.col3"
from hr.t1
)
select *
from ( select q.object_value.getrootelement() as col_name
, rownum as rn
from xmltable('//*'
passing xmltype(cursor(select * from t2 where rownum = 1))
) q
where q.object_value.getrootelement() not in ('ROWSET', 'ROW')
)
pivot(
max(col_name) for rn in (1 as "name", 2 as "initial")
)
Result:
name initial
--------------- ---------------
FIRST_NAME MIDDLE_NAME
Note: In order for column names to be prefixed with table name, you need to list them
explicitly in the select list of a query and supply an alias, manually.
PL/SQL approach. Starting from Oracle 11g you could use dbms_sql() package and describe_columns() procedure specifically to get the name of columns in the cursor(your select).
This might be what you are looking for, try selecting from system views USER_TAB_COLS or ALL_TAB_COLS.

Trying to select all records where two fields are distinct (but the rest don't have to be)

I have a table that has 14 columns in it. These columns are color, type, ft, date, count, etc. What I need is to select all distinct records of id and type with the most recent date. So, for example...
color------type-----------date
red--------work-----------01/01/01
red---------play----------02/02/02
red---------play----------03/03/03
In this case, I want to return red, work, 01/01/01 and red, play 03/03/03. Hopefully this makes sense. I've tried different combinations of select unique and select distinct and group bys, and I haven't been able to come up with anything.
Here is the SQL statement I'm trying:
select distinct
chock_id,
roll_type,
max(chock_service_dt),
chock_id_dt,
chock_seq_num,
chock_service_cmnt,
total_rolled_lineal_ft,
total_rolled_tons,
chock_usage_cnt,
chock_insert_dt,
record_modify_dt,
next_chock_service_dt_act,
previous_alarm_value,
upload_complete_yn
from
tp07_chock_summary_row
group by
chock_id,
roll_type,
chock_service_dt,
chock_id_dt,
chock_seq_num,
chock_service_cmnt,
total_rolled_lineal_ft,
total_rolled_tons,
chock_usage_cnt,
chock_insert_dt,
record_modify_dt,
next_chock_service_dt_act,
previous_alarm_value,
upload_complete_yn;
Here's a screenshot. Like I said in a comment below, like in rows 2 and 4, I can't have multiple records with the same chock_id and roll_type.
Given your new requirements, which you did not explain initially, this should do it:
select
chock_id,
roll_type,
chock_service_dt,
chock_id_dt,
chock_seq_num,
chock_service_cmnt,
total_rolled_lineal_ft,
total_rolled_tons,
chock_usage_cnt,
chock_insert_dt,
record_modify_dt,
next_chock_service_dt_act,
previous_alarm_value,
upload_complete_yn
from (
select
chock_id,
roll_type,
chock_service_dt,
chock_id_dt,
chock_seq_num,
chock_service_cmnt,
total_rolled_lineal_ft,
total_rolled_tons,
chock_usage_cnt,
chock_insert_dt,
record_modify_dt,
next_chock_service_dt_act,
previous_alarm_value,
upload_complete_yn,
row_number() over (
partition by chock_id, roll_type
order by chock_service_dt desc
) rn
from
tp07_chock_summary_row
) where rn = 1
select color, type, max(date)
from ...
group by color, type
select
color,
type,
max(date)
from
yourtable
group by
color,
type

Resources