Conditional forward fill in dolphindb - sql-update

To simplify my question, consider the following table in dolphindb:
t=table(1..5 as id, 1 NULL 3 NULL NULL as x)
I would like to forward fill nulls ONLY for the rows that I specify. For example, only for the row with id=2. I tried the following SQL query but the result was unexpected.
update t set x=ffill(x) where id=2
The table t was not updated at all. I would appreciate it if someone can point out the mistake in my code. Thanks!

The 'where' condition in SQL is always executed before the 'select' calculations. Therefore in this case you lose the information about id=1. Try this:
update t set x=iif(id==2, ffill(x), x)
This uses dolphindb function iif. Effectively it is a loop.

Related

Refreshing IG after save if validations pass

I have an interactive grid with some data, when users add a new row it didnt show up(found that this is because I have written a custom procedure to save and I dont return the PK).
So I set up a dynamic action, on Save[Interactive Grid] with a server side condition Type: Inline Validation Errors NOT displayed, with a true action of Refresh region.
But the condition doesent appear to work.
When adding row which passes the validations, it refreshes just fine, but when adding a row that fails validations, the validation error shows up and a popup comes up asking if I am sure I want to procede as changes were made(tries to refresh).
I have tried changing the condition to Inline Validation Errors displayed, but that doesent work. I have tried changing Event scope to Dynamic(which I dont even know what it does), but that didnt help.
I also changed the IG primary key to ROWID from the default PK from my table if that might help with the custom save procedure.
I would prefer to fix this w/o editing the save procedure as if I have to I will need to fix up a LOT of save procedures. But if I cant I am willing to fix them. But I am not sure how.
My save procedure is formated as follows:
PROCEDURE save_table (s_variable1 IN table.variable1%TYPE
, s_variable2 IN table.variable2%TYPE
, row_status IN VARCHAR2)IS
BEGIN
CASE row_status
WHEN 'C' THEN
INSERT INTO table(variable1, variable2)
VALUES (variable1_seq.nextval, s_variable2);
WHEN 'U' THEN
UPDATE table
SET variable2= s_variable2
WHERE variable1= s_variable1;
WHEN 'D' THEN
DELETE FROM table
WHERE variable1= s_variable1;
END CASE;
END save_table;
If someone could tell me the easiest way to fix this to return the PK(variable1) or ROWID so that the IG updates that one row so I dont even need the Dynamic Action.
EDIT:
Found the easiest way to do this is to change the procedure and found out how:
PROCEDURE save_table (s_variable1 IN OUT table.variable1%TYPE
, s_variable2 IN table.variable2%TYPE
, row_status IN VARCHAR2)IS
a_variable1 table.variable1%TYPE;
BEGIN
CASE row_status
WHEN 'C' THEN
a_variable1 := variable1_seq.nextval;
INSERT INTO table(variable1, variable2)
VALUES (a_variable1, s_variable2)
RETURNING a_variable1 INTO s_variable1;
Only posted the section I changed.
RETURNING INTO clause allows us to return column values for rows affected by DML statements. You can use this in your procedure to return the column value as shown below.
DECLARE
l_id t1.id%TYPE;
BEGIN
INSERT INTO t1 VALUES (t1_seq.nextval, 'FOUR')
RETURNING id INTO l_id;
COMMIT;
DBMS_OUTPUT.put_line('ID=' || l_id);
END;
For more details on Returning into see this link

How to Replace Specific value with other value in django database column?

I have a Table "Subscriber" in which one column whose name is Status.
I want to replace value of "status" column.Where status = 1 and 3 change it with 4. In other words all '1 and 3' status convert into 4.
What should i do to solve this problem.
#e4c5's solution is perfectly good, though I try to reserve Q objects for complex queries, in this case, I might just go with the __in filter: Subscriber.objects.filter(status__in=[1, 3]).update(status=4)
Use a Q object to get the OR condition. The rest of it is a simple call to update
Subscriber.objects.filter(Q(status=1)|Q(status=3)).update(status=4)
Keyword argument queries – in filter(), etc. – are “AND”ed together.
If you need to execute more complex queries (for example, queries with
OR statements), you can use Q objects.
Read the documentation for update here: https://docs.djangoproject.com/en/1.9/ref/models/querysets/#update

Select Statement Vs Find in Ax

while writing code we can either use select statement or select field list or find method on table for fetching the records.
I wonder which of the statement helps in better performance
It really depends on what you actually need.
find() methods must return the whole table buffer, that means, all of the columns are projected into the buffer returned by it, so you have the complete record selected. But sometimes you only need a single column, or just a few. In such cases it can be a waste to select the whole record, since you won't use the columns selected anyway.
So if you're dealing with a table that has lots of columns and you only need a few of them, consider writing a specific select statement for that, listing the columns you need.
Also, keep in mind that select statements that only project a few columns should not be made public. That means that you should NOT extract such statements into a method, because imagine the surprise of someone consuming that method and trying to figure out why column X was empty...
You can look at the find() method on the table and find out the same 'select'-statement there.
It can be the same 'select; statement as your own an the performance will be the same in this case.
And it can be different select statement then your own and the performance will be depend on indexes on the table, select statement, collected statistics and so on.
But there is no magic here. All of them is just select statement - no matter which method do you use.

C++: How to set GUID to NULL instead of GUID_NULL?

I have a database which uses GUIDs instead of, say, an ordinary counter for the ID fields. But I can't seem to put NULL (instead of GUID_NULL) into such fields in DB even though. Yes, the field in the database does take NULL.
Let's say there is a parent-child relationship between two tables. So there is a parent and a child GUID references from one table to another. But the "root" parent does not have any parent and there I would like to be able to put NULL into its ParentUID database field. If I put GUID_NULL there then I will need a corresponding default row in the referenced table which has a GUID-value of GUID_NULL so that the foreign key constraint won't break.
Also, using GUID_NULL with default-rows at referenced tables will give me a resultset back when doing a standard join operation...which is not desirable.
They way it's done in code when inserting values into database is using a CCommand which takes structure that contains the values of the row fields to be inserted. One of these is a GUID type variable.
So it creates an SQL statement string looking like
INSERT INTO [tablename] (field1, field2, field3,...) VALUES(?,?,?,...)
and then in a loop there is something like:
command.field1 = 1;
command.field2 = 2;
command.GUIDField = ?????? //I want to put NULL here instead of GUID_NULL
command.Open(...);
I hope it is understandable what I wish to do and what the conditions in code are.
Thankful for help!
UPDATE:
Ok, it was very hard to exaplin correctly, but this is exactly what I want to do http://support.microsoft.com/kb/260900
Just that when I follow that example, it makes no difference...still I get FK constraint violation on insert so I suspect it is trying to insert GUID_NULL instead of NULL. :(
The link I had in my Update-section does work, my bad: http://support.microsoft.com/kb/260900
It is the answer to my problems, perhaps it will help someone else as well! :3

Check query results if no data is returned

I have a query that I am calling to update an email service. Most times it will have data in it, but in testing I came across the situation of it not returning any data because there was no data to return. In the case of no data it returns the error "Variable EDITEDACCTS is undefined".
I have tried wrapping the query in a <cftry> but it doesn't "fail" per se so it does not trip the <cfcatch>. I have also tried defining the variable
var EditedAccts = QueryNew("")
as well as simply trying
<cfif NOT isDefined(#EditedAccts#)>
and it always returns "Variable EDITEDACCTS is undefined".
I need a production ready solution to this and I'm hoping somewhere here on SO can help me out.
Thanks in advance for your help.
I have just found the answer. You set the "result" parameter in the query call and then you can check the recordcount field returned.
<cfquery name="EditedAccts" datasource="mydatasource" result="queryResult">
...query goes here...
</cfquery>
When using the "result" parameter you get a struct returned with the sql used, the cached setting, the execution time and the record count.
Now I can check the record count and proceed from there.
Hopefully this will help someone in the future.
I tried using result="queryResult" but when I tried to reference the query name I got something like this error - "The value of the attribute query, which is currently EditedAccts, is invalid". Instead, I used something like IsDefined("#EditedAccts#") - including the value in quotes did the trick for me. I am only new to ColdFusion but I am learning quickly that values in quotes are entirely different to values not in quotes, in terms of how a function will interpret a parameter.