Understanding case statement in sas proc sql - postgresql

I am having difficulty in understand this sas code.
select
case
when DM_TURNOVER_TMP_STOCK."LIITM"n then
DM_TURNOVER_TMP_STOCK."LIITM"n
else
DM_TURNOVER_TMP_SALES."SDITM"n
end as "LIITM"n
case
when DM_TURNOVER_TMP_STOCK."LIMCU"n then
DM_TURNOVER_TMP_STOCK."LIMCU"n
normally we use sas in sql in condition statment of column but here seems to be diffrent.Please help me in understanding this in postgres term.

Assuming this is a query from tables DM_TURNOVER_TMP_STOCK and DM_TURNOVER_TMP_SALES,
when DM_TURNOVER_TMP_STOCK.LIITM is not missing and non zero, LIITM will get the value of DM_TURNOVER_TMP_STOCK.LIITM.
Otherwise, it will get the value of DM_TURNOVER_TMP_SALES.SDITM.

Related

Snowflake : Unsupported subquery type cannot be evaluated

I am using snowflake as a data warehouse. I have a CSV file at AWS S3. I am writing a merge sql to merge data received in CSV to the table in snowflake. I have a column in time dimension table with data type as Number(38,0) data type in SF. This table holds all dates time, one e.g. is of column
time_id= 232 and time=12:00
In CSV I am getting a column with the label as time and value as 12:00.
In merge sql I am fetching this value and trying to get time_id for it.
update table_name set start_time_dim_id = (select time_id from time_dim t where t.time_name = csv_data.start_time_dim_id)
On this statement I am getting this error "SQL compilation error: Unsupported subquery type cannot be evaluated"
I am struggling to solve it, during this I google for it and got one reference for it
https://github.com/snowflakedb/snowflake-connector-python/issues/251
So want to make sure if anyone have encountered this issue? If yes, will appreciate pointers over it.
It seems like a conversion issue. I suggest you to check the data in CSV file. Maybe there is a wrong or missing value. Please check your data, and make sure it returns numeric values
create table simpleone ( id number );
insert into simpleone values ( True );
The last statement fails with:
SQL compilation error: Expression type does not match column data type, expecting NUMBER(38,0) but got BOOLEAN for column ID
If you provide sample data, and SQL to produce this error, maybe we can provide a solution.
unfortunately correlated and nested subqueries in Snowflake are a bit limited at this stage.
I would try running something like this:
update table_name
set start_time_dim_id= time_id
from time_dim
where t.time_name=csv_data.start_time_dim_id

How can I execute an "Explain" statement with a prepared query in PostgrSQL that has bind parameters?

I would like to be able to execute an explain statement on a query that has bind parameters. For example:
EXPLAIN SELECT * FROM metasyntax WHERE id = $1;
When I attempt to execute this, I get the following error:
ERROR: bind message supplies 0 parameters, but prepared statement "" requires 1
I understand it's telling me that it wants me to supply a value for the query. However, I may not necessarily know the answer to that. In other SQL dialects such as Oracle, it will generate an explain plan without needing me to supply a value for the parameter.
Is it possible to get an explain plan without binding to an actual value? Thanks!
Assuming the parameter is an integer:
PREPARE x(integer) AS SELECT * FROM metasyntax WHERE id = $1;
Then run the following six times, where “42” is a representative value:
EXPLAIN (ANALYZE, BUFFERS) EXECUTE x(42);
Normally PostgreSQL will switch to a generic plan for the sixth run, and you will see a plan that contains the placeholder $1.
No.
The optimiser is allowed to change the query plan based on the parameter. Imagine if the parameter was null - it's obvious that no rows will be returned, so the DB may return an empty rowset instantly.
Just use a representative value.

TSQL openquery with different result sets

all.
I am using openquery to call remote stored procedure at the linked server.
If the procedure's work was successed - the result set contains a,b,c columns.
If there was inner error, it returns only error column in result set.
As I know, to select from openquery results - we need to call it with 'WITH RESULT SETS', but I dont know what result set from that two will be returned.
How can I correctly call the procedure through openquery and handle the error from returning column in error case?
Thank you.
With regards, Yuriy.
You may be able to use a TRY/CATCH, TRY one ResultSet, CATCH the other;
https://msdn.microsoft.com/en-us/library/ms179296.aspx

Loop through array parameter WITHOUT foreach

CREATE OR REPLACE FUNCTION fnMyFunction(recipients recipient[]) ...
FOREACH v_recipient IN ARRAY recipients
LOOP
v_total := v_total + v_recipient.amount;
INSERT INTO tmp_recipients(id, amount)
VALUES(v_recipient.id, v_recipient.amount::numeric(10,2));
END LOOP;
...
This works great in dev environment, but just found out the release environment is 8.4 which doesn't appear to support the FOREACH construct. I was hoping someone might shed some light on an alternative implementation for loop though the array parameter set and using values from array in a similar fashion to avoid a complete refactor.
The error message I am receiving is:
ERROR: syntax error at or near "FOREACH" SQL state: 42601 Context: SQL
statement in PL/PgSQL function "fnMyFunction" near line ##
The db environment is on a shared host so I have no options for platform upgrade.
I tagged postgres 9.1 and 8.4 because the function works properly in 9.x but fails on 8.4.
Use unnest; I think it was in 8.4. Untested but I think is right:
FOR v_recipient IN SELECT vr FROM unnest(recipients) x(vr)
LOOP
....
END LOOP;
If you can't do that you'll have to loop over array_length using indexing into the array.
The way you have it, you execute one INSERT at a time. With relational databases, set-based operations are regularly much faster than iterating through records one at a time.
This should be simpler, faster and work with PostgreSQL 8.4 or later:
INSERT INTO tmp_recipients(id, amount)
SELECT (r.col).*
FROM (SELECT unnest(recipients) AS col) r
This assumes that the composite base type of the array consists of (id, amount) - in that order - and the type of amount can be coerced to numeric(10,2). Else, or just to be sure, be more explicit:
INSERT INTO tmp_recipients(id, amount)
SELECT (r.col).id, (r.col).amount::numeric(10,2)
FROM (SELECT unnest(recipients) AS col) r
The parentheses around (r.col) are not optional. They are required to disambiguate the syntax for the composite type.

Use Expression Result (e.g. Month) as Column Name in SQL Express

I'm writing an SQL expression and I'd like to use the current month as the column name/header.
Code:
Select MONTH(GETDATE()) AS MONTH(GETDATE())
FROM SomeTable;
Error:
Error 102: Incorrect syntax near 'GETDATE'.
This is for a school project and I'm not sure if it's possible. If it is, I'd like to possibly convert that Month number to the actual month name. Thanks in advance.
Oh, and I'm using LinqPad to test the queries on a remote DB and SQL Express Server (Transact-SQL).
Cheers,
Lindsay
I think, You can not use function in column alias, if you try to then you get this error incorrect syntex "Expecting ID, QUOTED_ID, STRING, or TEXT_LEX" which means the alias text has to be hard coded.
I would suggest, you use your front end application to set current month as header, instead of relying on back end sql query.
The alias for your computed columns shouldn't contain any function - just text:
SELECT
MONTH(GETDATE()) AS 'Month'
FROM
dbo.SomeTable