The use of insert in ada? - ada

Can anyone please tell me the usage of Insert in Ada language. I had tried the net but i couldn't just figure out. It would be a great help if anyone could provide me with examples too.
Thanks in advance
Maddy

I've never used Insert before (don't often have to do anything but a simple Put_Line with strings) so knocked this together:
with Ada.Strings.Fixed;
with Ada.Text_IO; use Ada.Text_IO;
procedure Inserting is
Base : constant String := (1 .. 8 => ' ') & "aaaaaaaa" & (1 .. 8 => ' ');
begin
Put_Line (ASCII.HT & '|' & Base & '|');
for J in 1 .. 24 loop
declare
S : String := Base;
begin
Ada.Strings.Fixed.Insert (S,
Before => J,
New_Item => "ccc");
Put_Line (Integer'Image (J) & ASCII.HT & '|' & S & '|');
end;
end loop;
end Inserting;
and the output is
| aaaaaaaa |
1 |ccc aaaaaaaa |
2 | ccc aaaaaaaa |
3 | ccc aaaaaaaa |
4 | ccc aaaaaaaa |
5 | ccc aaaaaaaa |
6 | ccc aaaaaaaa |
7 | ccc aaaaaaaa |
8 | ccc aaaaaaaa |
9 | cccaaaaaaaa |
10 | acccaaaaaaa |
11 | aacccaaaaaa |
12 | aaacccaaaaa |
13 | aaaacccaaaa |
14 | aaaaacccaaa |
15 | aaaaaacccaa |
16 | aaaaaaaccca |
17 | aaaaaaaaccc |
18 | aaaaaaaa ccc |
19 | aaaaaaaa ccc |
20 | aaaaaaaa ccc |
21 | aaaaaaaa ccc |
22 | aaaaaaaa ccc|
raised ADA.STRINGS.LENGTH_ERROR : a-strfix.adb:358
which I hope gives the general flavour.
The Ada95AARM A.4.3 at http://www.adaic.com/standards/95aarm/html/AA-A-4-3.html (3) tells about the concepts behind this.

Related

Split column into different rows on SQLite recursively using delimiter ","

I have a SQLite table just like this:
the table name is 'surat'
But i want to make id_ayat to be split into different rows using SQLite query, and expected result just like this:
_id|id_surat|id_ayat
---+--------+-------
3 | 2 | 112
3 | 2 | 213
3 | 3 | 19
3 | 3 | 83
3 | 3 | 85
3 | 3 | 102
is that possible? what query that i can use in SQLite format?
With a recursive CTE:
with recursive cte as (
select _id, id_surat, id_ayat,
id_ayat + 0 col
from tablename
union all
select _id, id_surat, trim(substr(id_ayat, length(col) + 2)),
trim(substr(id_ayat, length(col) + 2)) + 0
from cte
where instr(id_ayat, ',')
)
select _id, id_surat, col id_ayat
from cte
order by _id, id_surat
See the demo.
Results:
| _id | id_surat | id_ayat |
| --- | -------- | ------- |
| 3 | 2 | 112 |
| 3 | 2 | 213 |
| 3 | 3 | 19 |
| 3 | 3 | 83 |
| 3 | 3 | 85 |
| 3 | 3 | 102 |

How can I execute multiple queries in one query?

I am building a chat application where I am using Firebase to send and receive messages. Once I send or receive a message, I am storing it to SQLite as follows. Now it the recent chats screen, I need the last message from all the unique chats, number of unread messages in those unique chats in one single query as I am observing the SQLite database.
Mid(STRING) | SentBy | SentTo | message | readTime | sentTime| Type
----------------+--------+--------+---------+----------+---------+------
A | AA | JD | M1 | 1 | 0 | S
B | JD | AA | M2 | 2 | 1 | s
C | AA | JD | M3 | 3 | 2 | s
D | AB | JD | m5 | null | 3 | s
E | AA | JC | M1 | 5 | 4 | s
F | JD | AB | M2 | 6 | 5 | s
G | AA | JD | M3 | 7 | 6 | s
H | AA | JC | m5 | 8 | 7 | s
I | AA | JD | M1 | null | 8 | s
J | JD | AA | M2 | 10 | 9 | s
K | AA | JD | M3 | 11 | 10 | s
L | AB | JC | m5 | 12 | 11 | s
M | AA | JD | M1 | 13 | 12 | s
N | JC | AA | M2 | 14 | 13 | s
O | AB | JD | M3 | 15 | 14 | s
P | JC | JD | m5 | 16 | 15 | s
I tried
SELECT *,COUNT() FROM messagesTable GROUP BY min ( sentBy, sentTo ), max( sentBy , sentTo ) ORDER BY sentTime desc
This query gives me the last messages from every combination of sentTo and sentBy. But I also need to know how many messages are unread for that combination. I want to run a query for every row like
SELECT COUNT() FROM messagesTable WHERE sentBy = message.sentBy, sentTo = message.sentTo, readTime = null
How can I run both queries in a single query?
You must group by the combination of (sentby, sentto) and with a straight count(*) get the total number of messages and with conditional aggregation you can get the number of unread mesages.
Then join to the result to the table to get also the last message:
select
g.user1, g.user2, g.lasttime, m.message lastmessage,
g.totalcounter, g.unreadcounter
from messagestable m inner join (
select
min(sentby, sentto) user1, max(sentby, sentto) user2,
max(senttime) lasttime, count(*) totalcounter,
sum(case when readtime is null then 1 else 0 end) unreadcounter
from messagestable
group by user1, user2
) g
on g.user1 = min(m.sentby, m.sentto) and g.user2 = max(m.sentby, m.sentto)
and g.lasttime = m.senttime
order by g.lasttime desc
See the demo.
Results:
| user1 | user2 | lasttime | lastmessage | totalcounter | unreadcounter |
| ----- | ----- | -------- | ----------- | ------------ | ------------- |
| JC | JD | 15 | m5 | 1 | 0 |
| AB | JD | 14 | M3 | 3 | 1 |
| AA | JC | 13 | M2 | 3 | 0 |
| AA | JD | 12 | M1 | 8 | 1 |
| AB | JC | 11 | m5 | 1 | 0 |

add and subtract by type

I have a SQLite table payments:
+------+--------+-------+
| user | amount | type |
+------+--------+-------+
| AAA | 100 | plus |
| AAA | 200 | plus |
| AAA | 50 | minus |
| BBB | 100 | plus |
| BBB | 20 | minus |
| BBB | 5 | minus |
| CCC | 200 | plus |
| CCC | 300 | plus |
| CCC | 25 | minus |
I need to calculate the sum with type 'plus' and subtract from it the sum with type 'minus' for each user.
The result table should look like this:
+------+--------+
| user | total |
+------+--------+
| AAA | 250 |
| BBB | 75 |
| CCC | 475 |
I think that my query is terrible, and I need help to improve it:
select user,
(select sum(amount) from payments as TABLE1 WHERE TABLE1.type = 'plus' AND
TABLE1.user= TABLE3.user) -
(select sum(amount) from payments as TABLE2 WHERE TABLE2.type = 'minus' AND
TABLE2.user= TABLE3.user) as total
from payments as TABLE3
group by client
order by id asc
The type is easier handled with a CASE expression. And then you can merge the aggregation into the outer query:
SELECT user,
SUM(CASE type
WHEN 'plus' THEN amount
WHEN 'minus' THEN -amount
END) AS total
FROM payments
GROUP BY client
ORDER BY id;

Pig get distinct rows with counts

I have a pig table (called table1) containing many duplicates and more than one column (called col1, col2)
Here is a simple example
| col1 | col2 |
-----------------
| 111 | bbb |
| 111 | ccc |
| 111 | bbb |
| 222 | bbb |
I would like to get the distinct lines with the count of their appearance (like using uniq -c in bash), so that the result would be :
| count |col1 | col2 |
-----------------
| 2 | 111 | bbb |
| 1 | 111 | ccc |
| 1 | 222 | bbb |
What is the syntax for such a command?
Please try the below:
A = LOAD 'data'....;
GR = GROUP A by (col1,col2);
CNT = FOREACH GR GENERATE FLATTEN (group) AS (col1,col2) , COUNT(A) as cnt_col;
dump CNT;

Subset a dataframe using a array created by itself in R

Sorry about that I don't how to describe my question in Title.
My problem is that
I have a dataframe as below
| id | content | created_at |
|----|---------|---------------------|
| 1 | hello | 2014-12-10 00:00:00 |
| 2 | world | 2013-11-11 00:00:00 |
| 3 | oh~no | 2012-10-10 00:00:00 |
| 4 | helpme | 2011-09-11 00:00:00 |
I want to subset this frame by time interval
for example:
subset: 2011 - 2012
| 4 | helpme | 2011-09-11 00:00:00 |
subset: 2012 - 2013
| 3 | oh~no | 2012-10-10 00:00:00 |
subset: 2013 - 2014
| 2 | world | 2013-11-11 00:00:00 |
subset: 2014 - 2015
| 1 | hello | 2014-12-10 00:00:00 |
Below is how I try to resolve this problem
I try to create a true,false array and do in each row
ifelse(
difftime(DF$created_at,as.Date(ISOdate(seq(2004,2014),1,1))) >= 0 &
difftime(DF$created_at,as.Date(ISOdate(seq(2005,2015),1,1))) < 0
, assign_to_subset_X, do_nothing)
but....
I don't think this is a good idea especially I already use R....
then I find some solutions such as apply
apply(DF, 2, do_a_function_to_subset)
but I still have no idea to write this function
please give me a hint.
Here is one possible solution
library(lubridate)
df <- read.table(textConnection(" id | content | created_at
1 | hello | 2014-12-10 00:00:00
2 | world | 2013-11-11 00:00:00
3 | oh~no | 2012-10-10 00:00:00
4 | helpme | 2011-09-11 00:00:00 "), header=TRUE, sep="|")
df$ts <- ymd_hms(df$created_at)
## create an interval
myInt <- ymd("2011-01-01") %--% ymd("2011-12-31")
df[df$ts %within% myInt, ]

Resources