I have a view containg a column named "Started At". How do i select this in a SELECT statement ?
Thank you!
Try with
SELECT "Started At"
FROM your_table
You can break the Oracle Database schema object name rules (no reserved words, start with A-Z, length 30 char, etc..) by enclosing the name within double quotes. To later access the object you MUST enclose the name by double quotes.
To the point:
me#XE> create table t ("x" int);
Table created.
me#XE> select x from t;
select x from t
*
ERROR at line 1:
ORA-06553: PLS-306: wrong number or types of arguments in call to 'OGC_X'
me#XE> select "x" from t;
no rows selected
me#XE> create view v as select * from t;
View created.
me#XE> select x from v;
select x from v
*
ERROR at line 1:
ORA-06553: PLS-306: wrong number or types of arguments in call to 'OGC_X'
me#XE> select "x" from v;
no rows selected
me#XE>
Related
I have a sample data, as shown below;
NAME
Values
Typeof(Values)
BNL
[1,2]
VARCHAR
As seen the data type is VARCHAR for the second column.
Expected Output
Name
Values
BNL
1
BNL
2
In a way, we want to split the row into two rows depending on how many elements we have in the Values column. I was looking into FLATTEN as an option;
select temp.*,
t.value
from temp,
table(flatten(temp.values)) as t;
But since it is a VARCHAR, we get following error message;
invalid type [VARCHAR(16777216)] for parameter '1'
Can I please get some help here?thanx
One way is to use TRY_PARSE_JSON:
select temp.*,
t.value
from temp,
table(flatten(TRY_PARSE_JSON(temp.values))) as t;
I have one column in SSRS report which should show the value according to the parameter, if the parameter is selected to Million Euro then it should show the value in Million Euro which means the value should be divided by 10^6,if the parameter is selected as K Euro then the value should be as K Euros which means the value should be divided by 10^3.
I tried to create a dataset where I wrote the SQL query and made a parameter but it didn't worked.
Can anyone give the solution like what should be my dataset query and how should I map it with the parameter.
I used
if #CurrencyConv='MEuro' BEGIN
select 1/Power(10,6) as ProductValue
end
else BEGIN select 1/Power(10,3) as ProductValue
END
And made a parameter where I hardcoded the available values as 'MEuro' and 'KEuro'
And in that column's expression I multiplied the column value with Dataset Value(ProductValue)
Column name is Converted Booking and I have to multiply that column according to the parameter value so if the value is MEuro then it should be [Converted Booking]*1/Power(10,6)
In Sql the below query is working
select [Converted Booking]*1/Power(10,6) from T_GCP_REP
How to apply same thing in SSRS
If you want the final calculated value in you dataset then change your dataset query to
SELECT
*,
CAST([Converted Booking] as float) / CASE WHEN #CurrencyConv = 'MEuro' THEN 1000000 ELSE 1000 END as ConvertedBookingFinal
from T_GCP_REP
OR
If you want to do this purely in SSRS (maybe you cannot change your dataset query?)
The Set the parameter values to 1000 and 1000000 respectively.
Then in the report textbox set the expression to
= Fields.Converted_Booking.Value / Parameters!myParameterName.Value
you can leave the parameter labels as "MEuro" etc you only need to change the value.
If you want the field in your SQL, you can add it using a CASE statement.
SELECT *, 1/Power(10, CASE WHEN #CurrencyConv = 'MEuro' THEN 6 ELSE 3 END) as ProductValue
FROM TABLE
or you could use the new IIF in SQL like SSRS has
SELECT *, 1/Power(10, IIF(#CurrencyConv = 'MEuro', 6, 3)) as ProductValue
FROM TABLE
For an SSRS expression, an IIF statement could also be used:
=1/Power(10, IIF(Parameters!CurrencyConv.Value = 'MEuro', 6, 3))
Though it might be better to use MEuro as the parameter label and have 6 as the value (with KEuro, 3 for the other possible value). Then you could shorten it to:
=1/Power(10, Parameters!CurrencyConv.Value)
Is there is a way to create ENUM type with custom order index. It is possible to update pg_type afterwards and set enumsortorder field to desired value, but the question is it possible to do during creation? E.g. something like
CREATE TYPE environment AS ENUM ('stage' 10, 'prod' 20);
The sort order is defined by the type declaration:
create type my_enum as enum ('first', 'third');
with t(v) as (
values ('first'::my_enum), ('third')
)
select *
from t
order by 1;
v
-------
first
third
(2 rows)
You can add a new label in a desired position:
alter type my_enum add value 'second' before 'third';
with t(v) as (
values ('first'::my_enum), ('third'), ('second')
)
select *
from t
order by 1;
v
--------
first
second
third
(3 rows)
Read about CREATE TYPE and ALTER TYPE
If the above is not sufficient use a map function, e.g.:
create or replace function my_enum_map(my_enum)
returns int language sql as $$
select case $1
when 'first' then 100
when 'second' then 50
when 'third' then 75
end
$$;
with t(v) as (
values ('first'::my_enum), ('third'), ('second')
)
select *
from t
order by my_enum_map(v);
v
--------
second
third
first
(3 rows)
You can also store the values used by the function in a table to make modifications easier.
My action passes a list of values from a column x in table y to the view. How do I write the following SQL: SELECT x FROM y, using DAL "language", when x and y are variables given by the view. Here it is, using exequtesql().
def myAction():
x = request.args(0, cast=str)
y = request.args(1, cast=str)
myrows = db.executesql('SELECT '+ x + ' FROM '+ y)
#Let's convert it to the list:
mylist = []
for row in myrows:
value = row #this line doesn't work
mylist.append(value)
return (mylist=mylist)
Also, is there a more convenient way to convert that data to a list?
First, note that you must create table definitions for any tables you want to access (i.e., db.define_table('mytable', ...)). Assuming you have done that and that y is the name of a single table and x is the name of a single field in that table, you would do:
myrows = db().select(db[y][x])
mylist = [r[x] for r in myrows]
Note, if any records are returned, .select() always produces a Row object, which comprises a set of Row objects (even if only a single field was selected). So, to extract the individual values into a list, you have to iterate over the Rows object and extract the relevant field from each Row object. The above code does so via a list comprehension.
Also, you might want to add some code to check whether db[y] and db[y][x] exist.
I would like to create a PostgreSQL function that does something like the following:
CREATE FUNCTION avg_purchases( IN last_names text[] DEFAULT '{}' )
RETURNS TABLE(last_name text[], avg_purchase_size double precision)
AS
$BODY$
DECLARE
qry text;
BEGIN
qry := 'SELECT last_name, AVG(purchase_size)
FROM purchases
WHERE last_name = ANY($1)
GROUP BY last_name'
RETURN QUERY EXECUTE qry USING last_names;
END;
$BODY$
But I see two problems here:
It is not clear to me that array type is the most useful type of input.
This is currently returning zero rows when I do:
SELECT avg_purchases($${'Brown','Smith','Jones'}$$);
What am I missing?
This works:
CREATE OR REPLACE FUNCTION avg_purchases(last_names text[] = '{}')
RETURNS TABLE(last_name text, avg_purchase_size float8)
LANGUAGE sql AS
$func$
SELECT last_name, avg(purchase_size)::float8
FROM purchases
WHERE last_name = ANY($1)
GROUP BY last_name
$func$;
Call:
SELECT * FROM avg_purchases('{foo,Bar,baz,"}weird_name''$$"}');
Or (example with dollar-quoting):
SELECT * FROM avg_purchases($x${foo,Bar,baz,"}weird_name'$$"}$x$);
How to quote string literals:
Insert text with single quotes in PostgreSQL
You don't need dynamic SQL here.
While you can wrap it into a plpgsql function (which may be useful), a simple SQL function is doing the basic job just fine.
You had type mismatches:
The result of avg() may be numeric to hold a precise result. A cast to float8 (alias for double precision) makes it work. For perfect precision, use numeric instead.
The OUT parameter last_name must be text instead of text[].
VARIADIC
An array is a useful type of input. If it's easier for your client you can also use a VARIADIC input parameter that allows to pass the array as a list of elements:
CREATE OR REPLACE FUNCTION avg_purchases(VARIADIC last_names text[] = '{}')
RETURNS TABLE(last_name text, avg_purchase_size float8)
LANGUAGE sql AS
$func$
SELECT last_name, avg(purchase_size)::float8
FROM purchases
JOIN (SELECT unnest($1)) t(last_name) USING (last_name)
GROUP BY 1
$func$;
Call:
SELECT * FROM avg_purchases('foo', 'Bar', 'baz', '"}weird_name''$$"}');
Or (with dollar-quoting):
SELECT * FROM avg_purchases('foo', 'Bar', 'baz', $y$'"}weird_name'$$"}$y$);
Stock Postgres only allows a maximum of 100 elements. This is determined at compile time by the preset option:
max_function_args (integer)
Reports the maximum number of function arguments. It is determined by the value of FUNC_MAX_ARGS when building the server. The default value is 100 arguments.
You can still call it with array notation when prefixed with the keyword VARIADIC:
SELECT * FROM avg_purchases(VARIADIC '{1,2,3, ... 99,100,101}');
For bigger arrays (100+), consider unnest() in a subquery and JOIN to it, tends to scale better:
Optimizing a Postgres query with a large IN