Query with input parameter won't compile as a view - sql

So I am trying to create a view that allows the user (from the front-end) query on it by specifying a specific date (in the form of a varchar/string). The view/query will then return data for the month of the specified date and also data for 13 months prior to it. When I run the following query and enter a valid 'string'/date , I am able to return results successfully. However, when I try to create a view with the same query, i am receiving the following error:
SQL Error: ORA-01027: bind variables not allowed for data definition operations
Below is the query:
SELECT person_name
, person_age
, person_dob
FROM person p
WHERE p.person_dob >= add_months(to_date(:par_date, 'yyyy-mm-dd'), -13);
Here is some sample data:
Person_Name Person_Age Person_DOB
Jon 18 01-01-1990
Jacob 21 09-04-1994
Heidi 28 04-02-2010
mark 30 05-02-2011
Helga 18 03-02-2015
Mike 18 01-02-1992
Larry 18 01-04-1993
I want to return the following result after specify :par_date as '2020-03-02'.
Person_Name Person_Age Person_DOB
Heidi 28 04-02-2010
mark 30 05-02-2011
Helga 18 03-02-2015

You unfortunately cannot do this. The closest you could come would be to create a table function, but that will be much less efficient than just selecting from the table with a 'WHERE' clause.
Ask Tom has a work around, but it is complex, much more work, and potentially very inneficient. Just use a 'WHERE' clause on the view or table
https://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:9536277800346269502

Related

View with input parameters

Let's say that I have a view with the following structure (and sample data):
Sample_Name Sample_Date Sample_ID
John 01-01-2015 453
Jacob 10-10-2016 777
Jingle 11-11-2017 888
Heimer 12-12-2018 999
Schmidt 07-07-2019 333
If someone specifies the year (i.e. '2019'), I want to return that year and the year prior to it. So the following data set would return:
Sample_Name Sample_Date Sample_ID
Heimer 12-12-2018 999
Schmidt 07-07-2019 333
Is there any way for me to create the view with input parameters (similar to a function), where the user can specify what date they are looking for? Wen the date is specified, the view should return data from that year and the year prior to it. I know with functions you can do something similar but according to my google search, this isn't possible in Oracle :(
It doesn't make sense.
A view is just a stored query. If you want to fetch certain data from it, use a where clause, e.g.
select sample_name, sample_date, sample_id
from your_view
where extract (year from sample_date) >= :par_year - 1;
(where you'd enter 2019 into :par_year, when prompted) (: might need to be replaced by &, depending on a tool you use).

SQL style query in MATLAB

Can I do SQL-style query on an in-memory dataset (or cellarray, or structure, etc) in MATLAB?
Why I ask is, sometimes, I don't want to talk to the database for 1000 times when I want to do different operations on each of the 1000 rows of data. Instead, I'd rather read all 1000 from the database and operate on them in MATLAB.
For example, I have read the following out of from the database:
age first_name last_name income
30 Mike Smith 45
17 David Oxgon 17
22 Osama Lumbermaster 3
Now I want to find out the full names of the people that are under the age of 25. I know how to do it, but is there any syntax as clean and intuitive as SQL like this?
SELECT first_name + ' ' + last_name AS name FROM people WHERE age < income
In the docs page Access Data in a Table (see the example Index Using a Logical Expression) it shows that your examples could be achieved as follows:
MyTable({'first_name','last_name'}, MyTable.age < MyTable.income)
These docs don't specifically explain how to merge the name and surname into one variable but I'm sure it's easy. Give it a try and let us know if you get it.

Rows to Dynamic columns in Access

I need a setup in Access where some rows in a table are converted to columns...for example, lets say I have this table:
Team Employee DaysWorked
Sales John 23
Sales Mark 3
Sales James 5
And then through the use of a query/form/something else, I would like the following display:
Team John Mark James
Sales 23 3 5
This conversion of rows to columns would have to be dynamic as a team could have any number of Employees and the Employees could change etc. Could anyone please guide me on the best way to achieve this?
You want to create a CrossTab query. Here's the SQL that you can use.
TRANSFORM SUM(YourTable.DaysWorked) AS DaysWorked
SELECT YourTable.Team
FROM YourTable
GROUP BY YourTable.Team
PIVOT YourTable.Employee
Of course the output is slightly different in that the columns are in alphabetical order.
Team James John Mark
Sales 5 23 3
For more detail see Make summary data easier to read by using a crosstab query at office.microsoft.com

Table structure of a student

I want a table structure which can store the details of the student like the below format.
If the student is in
10 th standard -> I need his aggregate % from 1st standard to 9th standard.
5 th standard -> I need his aggregate % from 1st standard to 4th standard.
1 st standard -> No aggregate % has to be displayed.
And the most important thing is ' we need to use only one table'. Please form a table structure with no redundant values.
Any ideas will be greatly appreciated......
No friends this is not a home work. This is asked in Oracle interview, conducted in Hyderabad day before yesterday '24th July, 2010',. He asked me the table structure.
He even did not asked me the query. He asked me how I will design the table. Please advice me.
id | name | grade | aggregate
This would do the trick, id is your primary key, name is students first last name, grade is what grade he is in and aggregate is aggregate % based on the grade.
Fro example some rows might be:
10 | Bill Cosby | 10 | 90
11 | Jerry Seinfeld | 4 | 60
Bill Cosby would have aggregate percent of 90 in grades 1-9, and jerry would have 60 in grades 1-3. In this case it is one table and boils down to you managing the rule of aggregation for this table, since it has to be one table.
If this is an interview question, it looks like they would like to check your knowledge on Nested Tables. Essentially you would have one column as roll number, and other column which is a nested table as Class and Percentage.

SQL command for PROGRESS database

Please bear with me new to SQL- I am trying to write an SQL command with a join in a PROGRESS db. I would like to then select only the first matching record from the join. I thought to use LIMIT but PROGRESS does not support that. MIN or TOP would also work I think but having trouble with the syntax. Here is current syntax:
SELECT esthead_0."k-est-code", estdie_0."estd-size2", estdie_0."k-cmp-no", estdie_0."estd-cal"
FROM VISION.PUB.estdie estdie_0
INNER JOIN VISION.PUB.esthead esthead_0 ON estdie_0."k-est-code" = esthead_0."k-est-code"
WHERE estdie_0."k-cmp-no" = (SELECT MIN("k-cmp-no")
FROM VISION.PUB.estdie estdie_0 )
This will select the MIN from the whole table but I would like the MIN of the records the join returns for each "k-est-code".
To do what you're accomplishing, you need to use Aggregate functions and GROUP BY.
Here is the correct query:
SELECT esthead_0."k-est-code", estdie_0."estd-size2", MIN(estdie_0."k-cmp-no") AS k-cmp-no-minimum, estdie_0."estd-cal"
FROM VISION.PUB.estdie estdie_0
INNER JOIN VISION.PUB.esthead esthead_0 ON estdie_0."k-est-code" = esthead_0."k-est-code"
GROUP BY esthead_0."k-est-code", estdie_0."estd-size2", estdie_0."estd-cal"
The general syntax for adding a GROUP BY / Aggregate query is:
use an aggregate function like MIN(), MAX(), AVG(), SUM() to select which column you want ... (choose the function depending on whether you want minimum, maximum etc). There are those I listed which are standard, and then often your database will give you some additional ones as well.
Add every other column you're selecting EXCEPT the ones in the function to a GROUP BY at the end of your query.
Your GROUP BY has to occur after your WHERE, but before your ORDER BY.
If you want to do WHERE-like filtering on the function (say you wanted only k-cmp-no over 100), you use HAVING after the group by, e.g.:
HAVING MIN(estdie_0."k-cmp-no") > 100
Google for Group By and Aggregate functions for more info on this SQL concept. It works the same in all databases as it's standard ANSI SQL. See this page for a more thorough introduction with examples: http://www.w3schools.com/sql/sql_groupby.asp
Progress (OE 11.2) supports OFFSET FETCH which is same as LIMIT OFFSET in mysql.
Example:
SQLExplorer>select FirstName , LastName , EmpNum from pub.employee order by empnum offset 10 rows fetch next 10 rows only;
FirstName LastName EmpNum
------------------------------ -------------------------------------------------- -----------
Frank Garsen 11
Jenny Morris 12
Luke Sanders 13
Marcy Adams 14
Alex Simons 15
Holly Atkins 16
Larry Barry 17
Jean Brady 18
Larry Dawsen 19
Dan Flanagan 20
Hope this helps