How can I use indexed variable in a Firebird stored procedure? I mean, I have output parameters ODATE1, ODATE2, ODATE3, can I use as ':ODATE' || COUNTER to set the value in a loop?
I have 2 tables like this:
1. T_EMPLOYEE
---------------
| ID_E | NAME |
---------------
| 1 | Anne |
---------------
| 2 | Bob |
---------------
2. T_WORK
----------------------------
| ID_W | DATE_W | ID_E |
----------------------------
| 1 | 2021-01-01 | 1 |
----------------------------
| 2 | 2021-01-01 | 2 |
----------------------------
| 3 | 2021-01-02 | 1 |
----------------------------
| 4 | 2021-01-03 | 2 |
----------------------------
From that table I want to make a stored procedure to get this result:
DASHBOARD
-----------------------------------------------------------
| OID_E | ONAME | ODATE1 | ODATE2 | ODATE3 |
----------------------------------------------------------
| 1 | Anne | 1 | 1 | 0 |
-----------------------------------------------------------
| 2 | Bob | 1 | 0 | 1 |
-----------------------------------------------------------
I tried using EXECUTE STATEMENT like this in stored procedure:
DECLARE VARIABLE COUNTER INT;
BEGIN
FOR
SELECT ID_E, NAME FROM T_EMPLOYEE
INTO :OID_E, :ONAME
D0
BEGIN
COUNTER = 1;
WHILE (COUNTER<=3) DO
BEGIN
EXECUTE STATEMENT 'SELECT COUNT(*) FROM T_WORK WHERE DATE_W = ''2021-01-0' || COUNTER ||
''' AND ID_E = ' || :OID_E || ' INTO :ODATE' || COUNTER;
COUNTER = COUNTER + 1;
END
SUSPEND;
END
END /*procedure*/
The procedure can't be compiled.
Then I tried the simple one like this without COUNTER index replacement:
DECLARE VARIABLE COUNTER INT;
BEGIN
FOR
SELECT ID_E, NAME FROM T_EMPLOYEE
INTO :OID_E, :ONAME
D0
BEGIN
COUNTER = 1;
WHILE (COUNTER<=3) DO
BEGIN
EXECUTE STATEMENT 'SELECT COUNT(*) FROM T_WORK WHERE ID_E = :OID_E ' ||
' AND DATE_W =''2021-01-02'' INTO :ODATE2';
COUNTER = COUNTER + 1;
END
SUSPEND;
END
END /*procedure*/
The procedure can be compiled, but when I execute, it will raise this error:
SQL Error: Dynamic SQL Error SQL error code = #1 Token unknown - line #1, column #2 #1. Error Code: -104. Invalid token
Please give me insight. How to use EXECUTE STATEMENT to make a flexible looping to set indexed variable.
Or you have another solution for my needs.
Additional information: Firebird v2.5
You cannot dynamically reference PSQL variables (including parameters) like this. However, you don't need to jump through all these hoops to get the desired results.
You can use something like the following(which doesn't even need a procedure):
select e.ID_E as OID_E, e.NAME as ONAME,
count(case when w.DATE_W = date '2021-01-01' then 1 end) as ODATE1,
count(case when w.DATE_W = date '2021-01-02' then 1 end) as ODATE2,
count(case when w.DATE_W = date '2021-01-03' then 1 end) as ODATE3
from T_EMPLOYEE e
inner join T_WORK w
on w.ID_E = e.ID_E
group by e.ID_E, e.NAME
order by e.ID_E
Fiddle: https://dbfiddle.uk/?rdbms=firebird_3.0&fiddle=59066afc0fd7f6b5cb87eac99164e899
Related
Suppose I have a simple sheet like
|| A | B | C | D | E | F | G
==============================
1 || 1 | 0 | | ? | 1 | 0 | 1
I want to create a new row where each value is either 1 or 0. The logic is if the cell is either blank or ? then it should be 0.
The closest I got was
=ArrayFormula(if(A1:G1="?", 0, A1:G1))
which gave me
|| A | B | C | D | E | F | G
==============================
1 || 1 | 0 | | ? | 1 | 0 | 1
2 || 1 | 0 | | 0 | 1 | 0 | 1
But as soon as I add an OR for checking blanks with
=ArrayFormula(if(or(A1:G1="?", isblank(A1:G1)), 0, A1:G1))
then I only get 1 cell:
|| A | B | C | D | E | F | G
==============================
1 || 1 | 0 | | ? | 1 | 0 | 1
2 || 1 | | | | | |
What am I doing wrong? Or is there a better way to do this?
Two ways:
Simply place the other condition in the value_is_false location
=ArrayFormula(if(A1:G1="?", 0, IF(ISBLANK(A1:G1), 0, A1:G1)))
Use + to represent OR. This works because "true" values are evaluated to 1 and "false" values are evaluated to 0. So, 0+0=false, 1+0=true. For AND, you multiply...0*0=false, 1*0=false, 1*1=true.
=ArrayFormula(if((A1:G1="?")+(ISBLANK(A1:G1)), 0, A1:G1))
I start in Ruby and Ruby on rails. I am trying to modify the access to this object (below) in base via the form and I will like to filter by the values that are in the table node_tags.
def map
......
nodes = Node.bbox(bbox).where(:visible => true).includes(:node_tags).limit(MAX_NUMBER_OF_NODES + 1)
.....
end
Probably on SQL request
SELECT * FROM nodes n INNER JOIN node_tags nt on n.node_id = nt.node_id where nt.k = 'alert' and nt.v = 'true'
Table nodes
| node_id | latitude | longitude | changeset_id | visible | timestamp | tile | version | redaction_id |
|---------|-----------|-----------|--------------|---------|-----------|------|---------|--------------|
| 11 | 473705641 | 3955487 | 11 | TRUE |
| 12 | 473705641 | 3955487 | 12 | TRUE |
table node_tags
| node_id | k | v |
|---------|-------|------|
| 11 | name | bob |
| 12 | alert | true |
If I understand right, you need to get sql as in your example by ORM (ActiveRecord).
Try this:
nodes = Node.where(:visible => true).join(:node_tags).where("node_tags.k = 'alert' and node_tags.v = 'true'")
First all I'm very new to Progress 4GL and still trying to get my head around how nesting FOR EACH blocks works. I have the following two tables that I'm getting information out of, ivc_header and ivc_mchgs:
ivc_header
invoice_nbr | sold_to_cust_nbr | sold_to_cust_seq | invoice_amt
1000051 | 70 | 0 | $1,000
1000049 | 70 | 1 | $1,500
1000010 | 310 | 0 | $2,000
1000011 | 310 | 1 | $2,500
ivc_mchgs
invoice_nbr | line_nbr | misc_seq_nbr | extension
1000051 | 1 | 1 | $300
1000051 | 1 | 2 | $200
1000051 | 2 | 1 | $100
1000049 | 1 | 1 | $400
1000049 | 1 | 2 | $100
1000049 | 2 | 1 | $150
1000010 | 1 | 1 | $50
1000010 | 1 | 2 | $50
1000010 | 2 | 1 | $100
1000011 | 1 | 1 | $75
1000011 | 1 | 2 | $80
1000011 | 2 | 1 | $90
Just FYI, the primary key for ivc_header is invoice_nbr and for ivc_mchgs the primary is a composite key consisting of invoice_nbr, line_nbr, and misc_seq_nbr. The foreign key is invoice_nbr.
Just a note about the data, the information in ivc_mchgs are miscellaneous charges by invoice line_nbr.
What I'm trying to get is the total invoice_amt and extension by sold_to_cust_nbr + sold_to_cust seq. After doing some research I've decided to put the totals in variables instead of using Progress' built in ACCUMULATE function.
Here is the code that I have:
DEFINE VARIABLE cCustNum AS CHARACTER NO-UNDO.
DEFINE VARIABLE dInvoiceSubTotal AS DECIMAL NO-UNDO.
DEFINE VARIABLE dSurchargeTotal AS DECIMAL NO-UNDO.
FOR EACH ivc_header
NO-LOCK
WHERE (ivc_header.sold_to_cust_nbr = "000070")
OR (ivc_header.sold_to_cust_nbr = "000310")
BREAK BY ivc_header.sold_to_cust_nbr:
IF FIRST-OF(ivc_header.sold_to_cust_nbr) THEN
ASSIGN dInvoiceSubTotal = 0.
ASSIGN dInvoiceSUbTotal = dInvoiceSUbTotal + ivc_header.invoice_amt.
IF LAST-OF(ivc_header.sold_to_cust_nbr) THEN
DISPLAY ivc_header.sold_to_cust_nbr + ivc_header.sold_to_cust_seq FORMAT "x(9)" LABEL "CustNum"
dInvoiceSUbTotal LABEL "SubTotal".
FOR EACH ivc_mchgs WHERE ivc_header.invoice_nbr = ivc_mchgs.invoice_nbr
NO-LOCK
BREAK BY ivc_mchgs.invoice_nbr:
IF FIRST-OF(ivc_mchgs.invoice_nbr) THEN
ASSIGN dSurchargeTotal = 0.
ASSIGN dSurchargeTotal = dSurchargeTotal + ivc_mchgs.extension.
IF LAST-OF (ivc_mchgs.invoice_nbr) THEN
DISPLAY
dSurchargeTotal LABEL "Surcharge".
END.
END.
This code will give me the total invoice_amt by sold_to_cust_nbr + sold_to_cust_seq and totals the extension by invoice_nbr. What I can't figure out how to do is get a total of extension by sold_to_cust_nbr + sold_to_cust_seq.
Any help is appreciated.
Thanks
I think you might not be aware that you can specify multiple BY clauses.
IOW you might want to code the inner FOR EACH something like this:
FOR EACH ivc_mchgs NO-LOCK WHERE ivc_header.invoice_nbr = ivc_mchgs.invoice_nbr
BREAK BY ivc_mchgs.invoice_nbr
BY ivc_mchgs.sold_to_cust_nbr
BY ivc_mchgs.sold_to_cust_seq:
IF FIRST-OF(ivc_mchgs.invoice_nbr) THEN
ASSIGN dSurchargeTotal = 0.
IF FIRST-OF(ivc_mchgs.sold_to_cust_nbr ) THEN ...
etc.
On the assumption you want both the exchange total by invoice and summary, then you could just do this:
DEFINE VARIABLE cCustNum AS CHARACTER NO-UNDO.
DEFINE VARIABLE dInvoiceSubTotal AS DECIMAL NO-UNDO.
DEFINE VARIABLE dSurchargeTotal AS DECIMAL NO-UNDO.
DEFINE VARIABLE dSurchargeSubTl AS DECIMAL NO-UNDO.
FOR EACH ivc_header
NO-LOCK
WHERE (ivc_header.sold_to_cust_nbr = "000070")
OR (ivc_header.sold_to_cust_nbr = "000310")
BREAK BY ivc_header.sold_to_cust_nbr:
IF FIRST-OF(ivc_header.sold_to_cust_nbr) THEN
ASSIGN dInvoiceSubTotal = 0
dSurchargeSubTl = 0.
ASSIGN dInvoiceSUbTotal = dInvoiceSUbTotal + ivc_header.invoice_amt.
IF LAST-OF(ivc_header.sold_to_cust_nbr) THEN
DISPLAY ivc_header.sold_to_cust_nbr + ivc_header.sold_to_cust_seq FORMAT "x(9)" LABEL "CustNum"
dInvoiceSUbTotal LABEL "SubTotal"
dSurchargeSubTL LABEL "Srchg SubTl".
FOR EACH ivc_mchgs WHERE ivc_header.invoice_nbr = ivc_mchgs.invoice_nbr
NO-LOCK
BREAK BY ivc_mchgs.invoice_nbr:
IF FIRST-OF(ivc_mchgs.invoice_nbr) THEN
ASSIGN dSurchargeTotal = 0.
ASSIGN dSurchargeTotal = dSurchargeTotal + ivc_mchgs.extension.
IF LAST-OF (ivc_mchgs.invoice_nbr) THEN DO:
DISPLAY dSurchargeTotal LABEL "Surcharge".
ASSIGN dSurchargeSubTl = dSurchargeSubTl + dSurchargeTotal.
END.
END.
END.
The elegant way would be to combine both queries using a left outer join, and use the ACCUMULATE functions, but this should work.
Can not understand why aggregateQuery always returns an empty result. Tried to test in aql, the same problem: 0 rows in set.
Indexes are all there.
aql> show indexes
+---------------+-------------+-----------+------------+-------+------------------------------+-------------+------------+-----------+
| ns | bin | indextype | set | state | indexname | path | sync_state | type |
+---------------+-------------+-----------+------------+-------+------------------------------+-------------+------------+-----------+
| "test" | "name" | "NONE" | "profiles" | "RW" | "inx_test_name" | "name" | "synced" | "STRING" |
| "test" | "age" | "NONE" | "profiles" | "RW" | "inx_test_age" | "age" | "synced" | "NUMERIC" |
aql> select * from test.profiles
+---------+-----+
| name | age |
+---------+-----+
| "Sally" | 19 |
| 20 | |
| 22 | |
| 28 | |
| "Ann" | 22 |
| "Bob" | 22 |
| "Tammy" | 22 |
| "Ricky" | 20 |
| 22 | |
| 19 | |
+---------+-----+
10 rows in set (0.026 secs)
aql> AGGREGATE mystream.avg_age() ON test.profiles WHERE age BETWEEN 20 and 29
0 rows in set (0.004 secs)
It seems that you are trying the example here.
There are two problems about the udf script. I paste the code of the lua script :
function avg_age(stream)
local function female(rec)
return rec.gender == "F"
end
local function name_age(rec)
return map{ name=rec.name, age=rec.age }
end
local function eldest(p1, p2)
if p1.age > p2.age then
return p1
else
return p2
end
end
return stream : filter(female) : map(name_age) : reduce(eldest)
end
First, there is no bin named 'gender' in your set, so you got 0 rows after aggregateQuery.
Second, this script isn't doing exactly what the function name 'avg_age' means, it just return the eldest record with name and age.
I paste my code bellow, it just replace the reduce func, and alert the map and filter func to meat the demand. You can just skip the filter process.
function avg_age(stream)
count = 0
sum = 0
local function female(rec)
return true
end
local function name_age(rec)
return rec.age
end
local function avg(p1, p2)
count = count + 1
sum = sum + p2
return sum / count
end
return stream : filter(female) : map(name_age) : reduce(avg)
end
The output looks like bellow :
AGGREGATE mystream.avg_age() ON test.avgage WHERE age BETWEEN 20 and 29
+---------+
| avg_age |
+---------+
| 22 |
+---------+
1 row in set (0.001 secs)
I am working on a rather large query, but am now stuck on the last bit. Given this example table:
Key1 | Key2 | SomeCol |
0 | 0 | ABC |
0 | 1 | 123 |
------------------------------
1 | 5 | ABC |
1 | 6 | DEF |
1 | 7 | ABC |
------------------------------
2 | 4 | ABC |
2 | 5 | 456 |
2 | 6 | 456 |
------------------------------
3 | 4 | ABC |
3 | 5 | 456 |
3 | 6 | ABC |
------------------------------
4 | 4 | ABC |
4 | 5 | ABC |
4 | 6 | ABC |
At this point in my query, I have extracted sequential(Key1, Key2) portions of a table and grouped by Key1. I wish to determine if all the values of SomeCol are identical, except for the first row.
Expected results:
Key1 | Key2 | SomeCol |
0 | 0 | ABC |
2 | 4 | ABC |
4 | 4 | ABC |
I know I can use something like.Any(g => g.SomeCol.Distinct().Count() == 1) in a case where I need all entries to be the same, but I can't seem to figure out how to get the syntax right to Skip(1). Also, I feel that my method of checking equality is sort of a hack. I know I can do this processing easily in C#, but I want to get as much of the processing to happen on the database side. Since my query is currently written in extension methods, I would appreciate in answer in the same syntax. Thanks!
What I have so far:
resultFromRestOfQuery
.GroupBy(g => g.Key1)
????
.SelectMany(g => g.Take(1).Select(h => h)
UPDATE
Alright, tested this on your values and it works.
var result = collection
.OrderBy(p => p.Key1)
.ThenBy(p => p.Key2)
.GroupBy(p => p.Key1)
.Where(p => p.Skip(1)
.Select(j => j.SomeCol)
.Distinct().Count() == 1)
.Select(p => p.First())
.ToList();
UPDATE #2
Perhaps this will help you with performance. Here is another version of this query without using Distinct(). Note the p.Count() > 1 - this is to avoid selecting the 1st row in a group when there's only 1 element in a group. If it's okay to select the first row when there's only one row, simply remove this part of the condition.
var result = collection
.OrderBy(p => p.Key1)
.ThenBy(p => p.Key2)
.GroupBy(p => p.Key1)
.Where(p => p.Count() > 1 && p.Skip(1)
.Select(j => j.SomeCol)
.All(j => j == p.Last().SomeCol))
.Select(p => p.First())
.ToList();