How to include CASE statements in a SQL function definition - sql-function

The following function definition does not give any errors, but neither does it work as intended. Can anyone help?
It is supposed to return one of the text abbreviations when supplied an int:
CREATE FUNCTION foo(a int)
RETURNS character
LANGUAGE SQL
AS $$
SELECT CASE C.INSURANCETYPE
WHEN 0 THEN 'MB' --MEDICARE
WHEN 1 THEN 'MC' --MEDICAID
WHEN 2 THEN 'OF' --OTHER FED
WHEN 3 THEN 'CH' --CHAMPVA
WHEN 4 THEN 'CI' --GROUP
WHEN 5 THEN 'OF' --OTHER FED
WHEN 6 THEN 'CI' --OTHER GROUP
END
FROM C
$$;
SELECT foo(0);
...returns nothing

Try this:
Declare #insuranceType as char
SET #insuranceType = (SELECT CASE C.INSURANCETYPE
WHEN 0 THEN 'MB' --MEDICARE
WHEN 1 THEN 'MC' --MEDICAID
WHEN 2 THEN 'OF' --OTHER FED
WHEN 3 THEN 'CH' --CHAMPVA
WHEN 4 THEN 'CI' --GROUP
WHEN 5 THEN 'OF' --OTHER FED
WHEN 6 THEN 'CI' --OTHER GROUP
END
FROM C)
RETURN #insuranceType

`CREATE or REPLACE FUNCTION NUM_TO_INSURANCE(NUM int) RETURNS varchar AS $body$
declare REL varchar;
begin
SELECT
CASE NUM --block_1
WHEN 0 THEN 'MB' --MEDICARE
WHEN 1 THEN 'MC' --MEDICAID
WHEN 2 THEN 'OF' --OTHER FED
WHEN 3 THEN 'CH' --CHAMPVA
WHEN 4 THEN 'CI' --GROUP
WHEN 5 THEN 'OF' --OTHER FED
WHEN 6 THEN 'CI' --OTHER GROUP
END
INTO REL;
RETURN REL;
END;
$body$ LANGUAGE plpgsql;`

Related

How reorder in between an existing range

given scenario,
id name sequence
1 a 1
2 b 2
3 c 3
4 d 4
5 e 5
User table has sequence which represent the order of users to display in the front end.
in the above example if I try to insert a user in between user id 3, then the expected behaviour should be
id name sequence
1 a 1
2 b 2
3 c 3
4 d 5
5 e 6
6 f 4
here position calculated using the last sequence input
last_sequence = 3.
Similarly, the user can repeat the same kind of operation, and it should be reordered according to in the database.
Note: not JQuery sorting.
My try
seq = last_sequence
users.where("last_sequence >= ? and id != ?",3,6).each do |u|
u.update_attributes(sequence: seq+1 )
seq = u.sequence + 1
end
I know the above is wrong and wrapping my head to find a solution
I resolved like the below
seq = latest_updated_user.sequence
user.where("sequence >= ? and id != ?",last_user.sequence, last_user.id).order(:sequence, :created_at).each do |v|
v.update_attributes(sequence: seq)
seq += 1
end

Join two different table having common row using PIG

Suppose I have two datasets .
DS1:
a 1
b 2
c 3
d 4
e 5
DS2:
1 pass
2 fail
3 pass
4 pass
5 fail
and i want to get a output like :
a 1 pass
b 2 fail
c 3 pass
d 4 pass
e 5 fail
now my question is,what pigcommand should i use to get the desire output?
JOIN.Assuming the data in the files are tab delimited.
A = LOAD 'ds1' USING PigStorage('\t') AS (a1:charrarray,a2:int);
B = LOAD 'ds2' USING PigStorage('\t') AS (b1:int,a2:chararray);
C = JOIN A BY a2, B BY b1;
D = FOREACH C GENERATE C.$0,C.$1,B.$1;
DUMP D;

SAS print variable on one line

I have google the whole universe but cannot find out this.
Given data set A:
a b
1 2
3 4
1 2
I want to print this to result in this way:
a 1 3 1
b 2 4 2
Also print each variable, name first then content on one line to result.
I think you're looking for for proc transpose:
proc transpose data = A out = A_transpose;
var a b;
run;
Then you can print this with proc print:
proc print data = A_transpose;
run;

Functional impact of declaring local variables via function parameters

In writing some one-off Lua code for an answer, I found myself code golfing to fit a function on a single line. While this code did not fit on one line...
foo=function(a,b) local c=bob; some_code_using_c; return c; end
...I realized that I could just make it fit by converting it to:
foo=function(a,b,c) c=bob; some_code_using_c; return c; end
Are there any performance or functional implications of using a function parameter to declare a function-local variable (assuming I know that a third argument will never be passed to the function) instead of using local? Do the two techniques ever behave differently?
Note: I included semicolons in the above for clarity of concept and to aid those who do not know Lua's handling of whitespace. I am aware that they are not necessary; if you follow the link above you will see that the actual code does not use them.
Edit Based on #Oka's answer, I compared the bytecode generated by these two functions, in separate files:
function foo(a,b)
local c
return function() c=a+b+c end
end
function foo(a,b,c)
-- this line intentionally blank
return function() c=a+b+c end
end
Ignoring addresses, the byte code report is identical (except for the number of parameters listed for the function).
You can go ahead and look at the Lua bytecode generated by using luac -l -l -p my_file.lua, comparing instruction sets and register layouts.
On my machine:
function foo (a, b)
local c = a * b
return c + 2
end
function bar (a, b, c)
c = a * b
return c + 2
end
Produces:
function <f.lua:1,4> (4 instructions at 0x80048fe0)
2 params, 4 slots, 0 upvalues, 3 locals, 1 constant, 0 functions
1 [2] MUL 2 0 1
2 [3] ADD 3 2 -1 ; - 2
3 [3] RETURN 3 2
4 [4] RETURN 0 1
constants (1) for 0x80048fe0:
1 2
locals (3) for 0x80048fe0:
0 a 1 5
1 b 1 5
2 c 2 5
upvalues (0) for 0x80048fe0:
function <f.lua:6,9> (4 instructions at 0x800492b8)
3 params, 4 slots, 0 upvalues, 3 locals, 1 constant, 0 functions
1 [7] MUL 2 0 1
2 [8] ADD 3 2 -1 ; - 2
3 [8] RETURN 3 2
4 [9] RETURN 0 1
constants (1) for 0x800492b8:
1 2
locals (3) for 0x800492b8:
0 a 1 5
1 b 1 5
2 c 1 5
upvalues (0) for 0x800492b8:
Not very much difference, is there? If I'm not mistaken, there's just a slightly different declaration location specified for each c, and the difference in the params size, as one might expect.

write function in PL/SQL to Sum within the same table

I am trying to write a PL/SQL function name hoursWorked that takes workerId and prjId and returns total amount of hours worker has worked on a project.
Table named PROJECTHOURS
WORKERPRJID | WORKERID | PRJID | TOTHOURS | CDATE
---------- ---------- ---------- ---------- ---------
1 1 1 10 1-JAN-14
2 1 1 7 2-JAN-14
3 1 1 6 4-JAN-14
4 2 1 5 11-JAN-14
5 2 1 9 15-JAN-14
6 2 1 7 13-JAN-14
7 1 2 5 11-JAN-14
8 2 2 9 15-JAN-14
9 2 2 7 13-JAN-14
I have tried the following:
CREATE OR replace PROCEDURE Hoursworked (j IN NUMBER,
n IN NUMBER)
IS
hours_worked projecthours.tothours%TYPE := 0;
BEGIN
SELECT workerid,
prjid,
SUM(tothours)
INTO hours_worked
FROM projecthours
WHERE workerid = j
AND prjid = n
GROUP BY j,
n;
RETURN hours_worked;
END;
SQL> show errors;
Errors for PROCEDURE HOURSWORKED:
LINE/COL ERROR
-------- -----------------------------------------------------------------
6/1 PL/SQL: SQL Statement ignored
6/52 PL/SQL: ORA-00947: not enough values
12/1 PL/SQL: Statement ignored
12/1 PLS-00372: In a procedure, RETURN statement cannot contain an
expression
Admittedly I am very inexperienced with SQL in general, so any insights are very welcome, as to what I am doing wrong.
The select is wrong, you select 3 values, but fetch into only one. The group by references are incorrect.
You don't need to select the workerid or the project id, neither do you need the group by.
Should be like below
CREATE OR replace PROCEDURE Hoursworked (j IN NUMBER,
n IN NUMBER,
hours_worked OUT NUMBER)
IS
BEGIN
SELECT SUM(tothours)
INTO hours_worked
FROM projecthours
WHERE workerid = j
AND prjid = n;
END;
In a procedure Return immediately ends program execution, and doesn't expect any parameters. Either change the program unit type to a function, or use OUT variables as above.
And please give better parameter names than "j" & "n"

Resources