Why do queries with parameters involving calculations not work in SSRS? - oracle

Why does this code work in SSRS without issues:
SELECT DISTINCT DEPARTMENT FROM MY_TABLE
WHERE MONTH > :O_MONTH
But this does not:
SELECT DISTINCT DEPARTMENT FROM MY_TABLE
WHERE MONTH > trunc(:O_MONTH-30)
I'm just curious, what is the reason for this? Is there a way around this so I can embed the query for a dataset? When I add the second version of the query that "Define Query Parameter" window pops up, telling me something is wrong.
Please help. Thanks
UPDATE:
Both queries work in TOAD for example, but the second, the one I need, does not work in Visual Studio. Something about doing the deduction from the input parameter is causing issues. Please help find a way around this!

Can you try the following options.
SELECT DISTINCT DEPARTMENT FROM MY_TABLE
WHERE MONTH > trunc(:O_MONTH) - 30;
OR
SELECT DISTINCT DEPARTMENT FROM MY_TABLE
WHERE MONTH > trunc(CAST (:O_MONTH AS DATE)) - 30;
OR
SELECT DISTINCT DEPARTMENT FROM MY_TABLE
WHERE MONTH > (CAST :O_MONTH AS DATE) - 30;

If you are passing a string, use TO_DATE conversion. For example: trunc(to_date(:o_month,'mm-dd-yyyy'))-30.

Related

Recover SQL query results in SQL Developer?

So I worked on a bunch of queries yesterday, and before I could get to exporting the results to an external file, my database went down. Is there any way to see the results of your past few queries in SQL Developer? I know there are ways to see your past queries, but I am looking for the results of my queries. Finding them would save me and my team a lot of rework.
Any help would be much appreciated. Thanks!
Edit: I am asking how to find the results of the SQL queries I ran yesterday. Not the queries themselves.
Use a SELECT statement with an AS OF clause. This retrieves data as it existed at some point in the past.
For example, this query returns the record from current state for Chung.
SELECT * FROM employees
WHERE last_name = 'Chung';
And below query retrieves the state of the record for Chung at 9:30AM, April 4, 2004:
SELECT * FROM employees
AS OF TIMESTAMP
TO_TIMESTAMP('2004-04-04 09:30:00', 'YYYY-MM-DD HH:MI:SS')
WHERE last_name = 'Chung';
You can also restore like this:
INSERT INTO employees
(SELECT * FROM employees
AS OF TIMESTAMP
TO_TIMESTAMP('2004-04-04 09:30:00', 'YYYY-MM-DD HH:MI:SS')
WHERE last_name = 'Chung');
Drag the queries from SQL history to worksheet and modify with AS OF clause.
Refer to the source of this answer for more info:
https://docs.oracle.com/cd/B28359_01/appdev.111/b28424/adfns_flashback.htm#g1025568

I'd like to select an arbitrary date from Oracle

Everyone knows you can do SELECT SYSDATE FROM DUAL to get a single row representing today's date.
I'd like to do something similar, but it would look like:
SELECT MAGICDATE FROM DUAL
WHERE MAGICDATE = Prompted in the application software
Bit of a long story why...
I'm using a package called SAP Business Objects. It translates user queries into SQL and can get a little tricky. I would like to pull out the user prompts into separate little sub-queries, then use the results of the prompts to power my main queries.
The queries look like:
select user_id, test_centre, test_date, count(*)
from
Lots of tables
where user_id in Prompted User Id
and test_centre in Prompted test_centre
and test_date between Prompted Date 1 and Prompted Date 2
Here's one way of doing it:
SELECT TRUNC(SYSDATE) + dbms_random.value(-5000, 5000) random_date,
TRUNC(SYSDATE) + floor(dbms_random.value(-5000, 5000)) random_datetime
FROM dual;
I've provided ways to get both a random date and a random date + time. Note that I've used TRUNC(SYSDATE) as the seed date and range of 5000 days before and after. You may wish to change the seed date so that it's SYSDATE or even a fixed date, as well as to amend the date ranges as per your requirements.
Can you just do this?
select to_date([prompted_date],'someformat') from dual
The actual answer to your question is:
select #prompt(.....) from dual
But I don't think that is actually what you're looking for. Can you be more specific about what you are attempting to do?

Adding today's date to Oracle query results when using wildcard *

this might be a lack of very basic knowledge, but I just can't figure it out. Searching for the answer and trial and error haven't helped much.
Returning all recordsets from a table (SELECT * FROM X) --> no problem.
Returning today's date (SELECT TO_CHAR(SYSDATE, 'DD-MM-YYYY') FROM DUAL) --> no problem.
Returning all recordsets from the same table as well as today's date --> no luck. I have tried subselects, union, joins, with-statements, ... it's driving me nuts.
When I name the columns I want returned (SELECT Columname1, Columnname2, to_char(sysdate....)) it works. This problems seems to only occur when using wildcards.
How do I get Oracle to return "all columns", today's date"?
Thanks!
You have to prefix the wildcard with the table name (or alias, if you've used one):
SELECT X.*, TO_CHAR(SYSDATE, 'DD-MM-YYYY') AS TODAYS_DATE FROM X
Using the wildcard is generally not considered a good idea, as you have no control over the order the columns are listed (if the table was built differently in different environments) and anyone consuming this output may be thrown if the table definition changes in the future, e.g. by adding another column. It's better to list all the columns individually.

INSERT..SELECT in Oracle always fails with "SQL command not properly ended"

I am proficient in SQL-Server and other forms of SQL, but am trying to learn Oracle SQL. For some reason I cannot get even the simplest form of INSERT INTO .. SELECT .. to work, it always fails with "SQL command not properly ended."
Here is my current example:
CREATE TABLE table1 (year INT, id INT, dat DATE, categ VARCHAR(99));
INSERT INTO table1
(year, id, dat, categ)
select year, id, dat, categ from table1 where id=5000 and year=2013;
Here's a SqlFiddle of it: http://sqlfiddle.com/#!4/c4d34/1
I cannot seem to figure out what's wrong here. I have checked about a dozen other related question here at SO and more than another dozen on Google but all of the answers either don't apply, or don't work. I have also tried about a million variations of the commands above, nothing seems to work.
Any help greatly appreciated.
FWIW, I now think that this is just a SQLFiddle problem, as many had contended.
The Oracle User who reported the problem to me with my code, was of course using the full SQL statement, before I had stripped it down to try to isolate the problem. That query had a completely different problem that just happened to report the same error in SQLFiddle. Specifically, its problem was that I was using As for table aliases, which apparently are invalid in Oracle (or perhaps, just in the query I had written).
In any event, sincere thanks to all who tried to help me.
CREATE TABLE table1 (year INT, id INT, dat DATE, categ VARCHAR(99))
/
INSERT INTO table1
(year, id, dat, categ)
select year, id, dat, categ from table1 where id=5000 and year=2013
This works, that is, if you paste both statements in the left (schema) window in SQL fiddle. I dont' think SQL Fiddle allows insert..select in the SQL window at all.
SQL Fiddle
CREATE TABLE table1 (year INT, id INT, dat DATE, categ VARCHAR(99))
//
INSERT INTO table1 (year, id, dat, categ)
SELECT year, id, dat, categ
FROM table1
WHERE id = 5000 AND year=2013
//
I don't know why you are facing this problem but there is no issue with syntax
I think it is just how you are executing the query on fiddle i just changed the execution flow and moved Insert statement in schema build section then the whole thing worked fine without changing a word (but i have inserted some sample data to show the exact working)
see this http://sqlfiddle.com/#!4/38e62/1

MonetDB: Group by different parts of a timestamp

I have a timestamp column in a monetdb table which I want to occasionally group by hour and occasionally group by day or month. What is the most optimal way of doing this in MonetDB?
In say postgres you could do something like:
select date_trunc('day', order_time), count(*)
from orders
group by date_trunc('day', order_time);
Which I appreciate would not use an index, but is there any way of doing this in MonetDB without creating additional date columns holding day, month and year truncated values?
Thanks.
You could use the EXTRACT(DAY FROM order_time) possibly as part of a subquery before grouping.
It might be a little late for answer, but the following should work for truncating to day precision:
SELECT CAST(order_time AS DATE) AS order_date, count(*)
FROM orders
GROUP BY order_date;
It works by casting the timestamp value to type DATE which is a MonetDB built-in type and the cast is pretty fast.
It does not have the flexibility of date_trunc in Postgres, but if you need to go to monthly of yearly precision, you could use the somewhat slower but usable EXTRACT to get the relevant parts of the timestamp and group by them. For monthly grouping, you could do:
SELECT EXTRACT(YEAR FROM order_time) AS y,
EXTRACT(MONTH FROM order_time) AS m,
count(*)
FROM orders GROUP BY y, m;
The only disadvantage is that you will have the date split to two columns.

Resources