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.
Related
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
full disclosure this is part of a homework question but I have tried 6 different versions and I am stuck.
I am trying for find 1 manager every time the query runs. I.e I put the department id in and 1 name pops out. currently, I get all the names, multiple times. I have tried a nesting with an '=' not nesting, union, intersection, etc. I can get the manager id with a basic query, I just can't get the name. the current version looks like this:
select e.ename
from .emp e
where d.managerid in (select unique d.managerid
from works w, .dept d, emp e1
where d.did=1 and e1.eid=w.eid and d.did=w.did );
I realize its probably a really basic mistake that I am not seeing - any ideas?
Its not clear what do you mean get 1 menager any time. are it should be different menagers any time or the same?
Lest go throw your query:
you select all empolyes from table emp where manager_id in next query dataset
You get all managers for dep=1. The rest of tables and conditions are not influent on result dataset.
I theing did is primary key for table dept, If so your query may be rewritten to
select e.ename
from emp e
where d.managerid in (select unique d.managerid
from dept d
where d.did=1);
but this query return to you all emploees and not manager from dept=1
and if you need a manager. you should get emploee who is a manager. If eid is primary key of employee, and managerid is id from employee table you need something like:
select e.ename
from emp e
where e1.eid in (select unique d.managerid
from dept d
where d.did=1);
query 1: I need to get the dept code from one table.
query 2: use that dept code to query another table which also has got another set of dept code. kind of one is to many, one dept referring to many depts.
NOTE: they don't have the same column name in two tables.
and the final result should be union of 1st query and 2nd query.
for eg: query 1 result : ECE
query 2 result : EEE, Mech, Comp. Sc.
I need the result to be ECE, EEE, Mech, Comp. Sc.
declare default_dept_Code varchar2(10);
begin
select dept_code into default_dept_Code from (select dept_code from
course_per WHERE student_no ='526765771');
dbms_output.put_line(default_dept_Code);
SELECT dept_code FROM course_per WHERE student_no ='526765771'
union all
select add_dept_code from addition_dept where dept_Code = default_dept_Code;
I'm unable to execute this query, it has got error. What are the other best ways I can handle it, I need to put this in a VIEW. I tried to create temp table and insert the select result into it, did not work. I'm a new bee to Oracle. I don't want to use cursor, if that is the only option I can go for it.
From what I understand you can write your query like this:
SELECT dept_code as code
FROM course_per
WHERE student_no ='526765771'
UNION ALL
SELECT add_dept_code as code
FROM addition_dept
WHERE dept_Code = (
SELECT dept_code
FROM course_per
WHERE student_no ='526765771');
I have a table with date column and would like to obtain a string with distinct years:
ID DATA
1 01/01/2010
2 02/01/2010
3 01/03/2011
4 03/01/2014
5 05/02/2014
From the above table and using listagg I want to get the years
2010
2011
2014
But when I run the following query:
SELECT LISTAGG(EXTRACT(year from data),',')
WITHIN GROUP (ORDER BY data)
FROM (SELECT distinct EXTRACT(year from data)
FROM t_teste)
I get the following error
ORA-00904: "DATA": invalid identifier
Any idea what I'm doing wrong?
You're using a subquery that is not specifying an alias for your extracted year value, and the data column from your table is no longer visible - as it is only in scope inside the subquery.
You can add a column alias and then refer to that directly in the outer query, without needing another extract:
select listagg(anno, ',') within group (order by anno)
from (
select distinct extract(year from data) as anno
from t_teste
);
LISTAGG(ANNO,',')WITHINGROUP(ORDERBYANNO)
-----------------------------------------
2010,2011,2014
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.