Distinct ipv4 subnet/supernet from the entries in Postgres - postgresql

Good day,
I would like to distinct the subnets/supernet from the entries of my table.
For example, to get only 10.40.3.0/24, 10.41.3.0/24, and 10.42.3.0/24 by one query. Right now I'm using this query to generate the output below (please note that this is a small portion of the entire dataset).
SELECT * FROM copy_tcp_3 WHERE dst_ip << '10.40.0.0/11' AND vlanid = 11 ORDER BY dst_ip;
src_ip | dst_ip | protocol | dst_port | vlanid
--------------+--------------+----------+----------+--------
10.72.21.221 | 10.40.3.105 | 6 | 7680 | 11
10.72.3.224 | 10.40.3.105 | 6 | 7680 | 11
10.72.3.224 | 10.41.3.10 | 6 | 7680 | 11
10.72.3.223 | 10.41.3.11 | 6 | 7680 | 11
10.72.3.224 | 10.41.3.11 | 6 | 7680 | 11
10.72.3.224 | 10.41.3.12 | 6 | 7680 | 11
10.72.3.224 | 10.41.3.13 | 6 | 7680 | 11
10.72.3.223 | 10.41.3.13 | 6 | 7680 | 11
10.72.21.221 | 10.41.3.18 | 6 | 7680 | 11
10.72.3.224 | 10.41.3.20 | 6 | 7680 | 11
10.72.3.223 | 10.41.3.20 | 6 | 7680 | 11
10.72.21.221 | 10.41.3.20 | 6 | 7680 | 11
10.72.3.224 | 10.41.3.21 | 6 | 7680 | 11
10.72.3.223 | 10.41.3.21 | 6 | 7680 | 11
10.72.21.221 | 10.41.3.26 | 6 | 7680 | 11
10.72.3.224 | 10.41.3.26 | 6 | 7680 | 11
10.72.21.221 | 10.41.3.27 | 6 | 7680 | 11
10.72.3.224 | 10.41.3.28 | 6 | 7680 | 11
10.72.21.221 | 10.41.3.30 | 6 | 7680 | 11
10.72.3.224 | 10.41.3.31 | 6 | 7680 | 11
10.72.3.223 | 10.41.3.31 | 6 | 7680 | 11
10.72.3.224 | 10.41.3.33 | 6 | 7680 | 11
10.72.3.224 | 10.41.3.38 | 6 | 7680 | 11
10.72.3.223 | 10.41.3.48 | 6 | 7680 | 11
10.72.21.221 | 10.41.3.159 | 6 | 7680 | 11
10.72.3.224 | 10.41.3.226 | 6 | 7680 | 11
10.72.3.223 | 10.41.3.226 | 6 | 7680 | 11
10.72.21.221 | 10.41.3.226 | 6 | 7680 | 11
10.72.3.224 | 10.41.3.227 | 6 | 7680 | 11
10.72.21.221 | 10.41.3.228 | 6 | 7680 | 11
10.72.3.224 | 10.42.3.134 | 6 | 7680 | 11
10.72.3.224 | 10.42.3.222 | 6 | 7680 | 11
10.72.3.224 | 10.42.3.227 | 6 | 7680 | 11
10.72.21.221 | 10.42.3.227 | 6 | 7680 | 11
10.72.3.224 | 10.42.3.228 | 6 | 7680 | 11
10.72.3.223 | 10.42.3.228 | 6 | 7680 | 11
10.72.3.224 | 10.42.3.230 | 6 | 7680 | 11
10.72.3.223 | 10.42.3.230 | 6 | 7680 | 11
10.72.3.223 | 10.42.3.231 | 6 | 7680 | 11
10.72.3.224 | 10.42.3.232 | 6 | 7680 | 11
10.72.3.224 | 10.42.3.233 | 6 | 7680 | 11
10.72.3.224 | 10.42.3.236 | 6 | 7680 | 11
10.72.3.223 | 10.42.3.236 | 6 | 7680 | 11
10.72.21.221 | 10.42.3.237 | 6 | 7680 | 11
10.72.3.223 | 10.42.3.237 | 6 | 7680 | 11
10.72.3.224 | 10.42.3.237 | 6 | 7680 | 11
10.72.3.224 | 10.42.20.42 | 6 | 7680 | 11
10.72.21.221 | 10.42.20.42 | 6 | 7680 | 11
SELECT DISTINCT(dst_ip) FROM copy_tcp_3 WHERE dst_ip << '10.41.0.0/11' AND vlanid = 11 ORDER BY dst_ip;"
dst_ip
--------------
10.40.3.105
10.41.3.10
10.41.3.11
10.41.3.12
10.41.3.13
10.41.3.18
10.41.3.20
10.41.3.21
10.41.3.26
10.41.3.27
10.41.3.28
10.41.3.30
10.41.3.31
10.41.3.33
10.41.3.38
10.41.3.48
10.41.3.159
10.41.3.226
10.41.3.227
10.41.3.228
10.42.3.134
10.42.3.222
10.42.3.227
10.42.3.228
10.42.3.230
10.42.3.231
10.42.3.232
10.42.3.233
10.42.3.236
10.42.3.237
10.42.20.42
10.42.25.80
10.43.3.222
Please note that I'm working with INET as type:
column_name | data_type
-------------+-----------
src_ip | inet
dst_ip | inet
protocol | integer
dst_port | integer
vlanid | integer
(5 rows)
Can you kindly check my question and advise me please?
Thank you in advance.
With kind regards
Farzad

For anyone interested, thanks to Mike I found a solution:
psql -U postgres -d tcp -c "SELECT DISTINCT(network(set_masklen(dst_ip, 24))) FROM copy_tcp_3 WHERE dst_ip << '10.40.0.0/11' AND vlanid = 11;"
network
---------------
10.40.3.0/24
10.41.3.0/24
10.42.3.0/24
10.42.20.0/24
10.42.25.0/24
10.43.3.0/24

Related

I2C is not working on raspberry pi 4 (but just sometimes)

My issue is that sometimes the i2c is not working on my raspberry pi 4. I have this error : unable to access '/dev/i2*': No such file or folder and Error: Could not open file /dev/i2c-1 or /dev/i2c/1': No such file or directory when executing i2cdetect -y 1.
I think I followed the installation instructions correctly :
I added this line in the /boot/config.txt file : dtparam=i2c1=on, dtparam=i2c_arm=on, dtoverlay=i2c-bcm2835, dtoverlay=i2c-dev
I have added i2c-dev and i2c-bcm2835 to the /etc/modules file
I find that when this error append, my gpio pins looks like this (gpio readall):
+-----+-----+---------+------+---+---Pi 4B--+---+------+---------+-----+-----+
| BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| | | 3.3v | | | 1 || 2 | | | 5v | | |
| 2 | 8 | SDA.1 | IN | 1 | 3 || 4 | | | 5v | | |
| 3 | 9 | SCL.1 | IN | 1 | 5 || 6 | | | 0v | | |
| 4 | 7 | GPIO. 7 | IN | 1 | 7 || 8 | 1 | ALT5 | TxD | 15 | 14 |
| | | 0v | | | 9 || 10 | 1 | ALT5 | RxD | 16 | 15 |
| 17 | 0 | GPIO. 0 | OUT | 1 | 11 || 12 | 0 | IN | GPIO. 1 | 1 | 18 |
| 27 | 2 | GPIO. 2 | IN | 0 | 13 || 14 | | | 0v | | |
| 22 | 3 | GPIO. 3 | IN | 0 | 15 || 16 | 0 | IN | GPIO. 4 | 4 | 23 |
| | | 3.3v | | | 17 || 18 | 1 | IN | GPIO. 5 | 5 | 24 |
| 10 | 12 | MOSI | IN | 0 | 19 || 20 | | | 0v | | |
| 9 | 13 | MISO | IN | 0 | 21 || 22 | 0 | IN | GPIO. 6 | 6 | 25 |
| 11 | 14 | SCLK | IN | 0 | 23 || 24 | 1 | IN | CE0 | 10 | 8 |
| | | 0v | | | 25 || 26 | 1 | IN | CE1 | 11 | 7 |
| 0 | 30 | SDA.0 | IN | 1 | 27 || 28 | 1 | IN | SCL.0 | 31 | 1 |
| 5 | 21 | GPIO.21 | OUT | 1 | 29 || 30 | | | 0v | | |
| 6 | 22 | GPIO.22 | IN | 0 | 31 || 32 | 1 | OUT | GPIO.26 | 26 | 12 |
| 13 | 23 | GPIO.23 | ALT4 | 1 | 33 || 34 | | | 0v | | |
| 19 | 24 | GPIO.24 | IN | 0 | 35 || 36 | 0 | IN | GPIO.27 | 27 | 16 |
| 26 | 25 | GPIO.25 | IN | 0 | 37 || 38 | 0 | IN | GPIO.28 | 28 | 20 |
| | | 0v | | | 39 || 40 | 0 | IN | GPIO.29 | 29 | 21 |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |
+-----+-----+---------+------+---+---Pi 4B--+---+------+---------+-----+-----+
And when everything is fine, it look like this :
+-----+-----+---------+------+---+---Pi 4B--+---+------+---------+-----+-----+
| BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| | | 3.3v | | | 1 || 2 | | | 5v | | |
| 2 | 8 | SDA.1 | ALT0 | 1 | 3 || 4 | | | 5v | | |
| 3 | 9 | SCL.1 | ALT0 | 1 | 5 || 6 | | | 0v | | |
| 4 | 7 | GPIO. 7 | IN | 1 | 7 || 8 | 1 | ALT5 | TxD | 15 | 14 |
| | | 0v | | | 9 || 10 | 1 | ALT5 | RxD | 16 | 15 |
| 17 | 0 | GPIO. 0 | OUT | 1 | 11 || 12 | 0 | IN | GPIO. 1 | 1 | 18 |
| 27 | 2 | GPIO. 2 | IN | 0 | 13 || 14 | | | 0v | | |
| 22 | 3 | GPIO. 3 | IN | 0 | 15 || 16 | 0 | IN | GPIO. 4 | 4 | 23 |
| | | 3.3v | | | 17 || 18 | 1 | IN | GPIO. 5 | 5 | 24 |
| 10 | 12 | MOSI | IN | 0 | 19 || 20 | | | 0v | | |
| 9 | 13 | MISO | IN | 0 | 21 || 22 | 0 | IN | GPIO. 6 | 6 | 25 |
| 11 | 14 | SCLK | IN | 0 | 23 || 24 | 1 | IN | CE0 | 10 | 8 |
| | | 0v | | | 25 || 26 | 1 | IN | CE1 | 11 | 7 |
| 0 | 30 | SDA.0 | IN | 1 | 27 || 28 | 1 | IN | SCL.0 | 31 | 1 |
| 5 | 21 | GPIO.21 | OUT | 1 | 29 || 30 | | | 0v | | |
| 6 | 22 | GPIO.22 | IN | 0 | 31 || 32 | 1 | OUT | GPIO.26 | 26 | 12 |
| 13 | 23 | GPIO.23 | ALT4 | 1 | 33 || 34 | | | 0v | | |
| 19 | 24 | GPIO.24 | IN | 0 | 35 || 36 | 0 | IN | GPIO.27 | 27 | 16 |
| 26 | 25 | GPIO.25 | IN | 0 | 37 || 38 | 0 | IN | GPIO.28 | 28 | 20 |
| | | 0v | | | 39 || 40 | 0 | IN | GPIO.29 | 29 | 21 |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |
+-----+-----+---------+------+---+---Pi 4B--+---+------+---------+-----+-----+
You can see the difference between pins 2 and 3 when i2c is working and when it's not. I forgot to mention that pins 2 and 3 are where my i2c is connected.
Is there any wait to make sure that the i2c is activated and is working when booting up ?
I don't know which instructions you followed, but you did way to much there. Now you have a set of conflicting driver modules enabled. All you need to have is
dtparam=i2c_arm=on
Remove everything else you have added to that file.
The modules should be present by default, so no need to copy anything around by hand. Or, next time, just use raspi-config to enable the default I2C bus, it will do the same.

partition by for iterval of 2 seconds

I have a DB with a field of timestamp,
I want to partition it for every 2 seconds (I know how to do it for 1 minute and one second)
this is an example of the DB:
create table data_t(id integer, time_t timestamp without time zone, data_t integer );
insert into data_t(id,time_t,data_t) values(1,'1999-01-08 04:05:06',248),
(2,'1999-01-08 04:05:06.03',45),
(3,'1999-01-08 04:05:06.035',98),
(4,'1999-01-08 04:05:06.9',57),
(5,'1999-01-08 04:05:07',86),
(6,'1999-01-08 04:05:08',84),
(7,'1999-01-08 04:05:08.5',832),
(8,'1999-01-08 04:05:08.7',86),
(9,'1999-01-08 04:05:08.9',863),
(10,'1999-01-08 04:05:9',866),
(11,'1999-01-08 04:05:10',862),
(12,'1999-01-08 04:05:10.5',863),
(13,'1999-01-08 04:05:10.55',826),
(14,'1999-01-08 04:05:11',816),
(15,'1999-01-08 04:05:11.7',186),
(16,'1999-01-08 04:05:12',862),
(17,'1999-01-08 04:05:12.5',826)
;
with t as (
select id,
time_t,
date_trunc('second', data_t.time_t) as time_t_1,
data_t
from data_t
), t1 as(
select *,
extract(hour from time_t_1) as h,
extract(minute from time_t_1) as m,
extract(second from time_t_1) as s
from t ) select *,
row_number() over(partition by h,m,s order by time_t_1) as t_sequence
from t1;
the output of this is:
| id | time_t | time_t_1 | data_t | h | m | s | t_sequence |
|----|--------------------------|----------------------|--------|---|---|----|------------|
| 1 | 1999-01-08T04:05:06Z | 1999-01-08T04:05:06Z | 248 | 4 | 5 | 6 | 1 |
| 2 | 1999-01-08T04:05:06.03Z | 1999-01-08T04:05:06Z | 45 | 4 | 5 | 6 | 2 |
| 3 | 1999-01-08T04:05:06.035Z | 1999-01-08T04:05:06Z | 98 | 4 | 5 | 6 | 3 |
| 4 | 1999-01-08T04:05:06.9Z | 1999-01-08T04:05:06Z | 57 | 4 | 5 | 6 | 4 |
| 5 | 1999-01-08T04:05:07Z | 1999-01-08T04:05:07Z | 86 | 4 | 5 | 7 | 1 |
| 6 | 1999-01-08T04:05:08Z | 1999-01-08T04:05:08Z | 84 | 4 | 5 | 8 | 1 |
| 7 | 1999-01-08T04:05:08.5Z | 1999-01-08T04:05:08Z | 832 | 4 | 5 | 8 | 2 |
| 8 | 1999-01-08T04:05:08.7Z | 1999-01-08T04:05:08Z | 86 | 4 | 5 | 8 | 3 |
| 9 | 1999-01-08T04:05:08.9Z | 1999-01-08T04:05:08Z | 863 | 4 | 5 | 8 | 4 |
| 10 | 1999-01-08T04:05:09Z | 1999-01-08T04:05:09Z | 866 | 4 | 5 | 9 | 1 |
| 11 | 1999-01-08T04:05:10Z | 1999-01-08T04:05:10Z | 862 | 4 | 5 | 10 | 1 |
| 12 | 1999-01-08T04:05:10.5Z | 1999-01-08T04:05:10Z | 863 | 4 | 5 | 10 | 2 |
| 13 | 1999-01-08T04:05:10.55Z | 1999-01-08T04:05:10Z | 826 | 4 | 5 | 10 | 3 |
| 14 | 1999-01-08T04:05:11Z | 1999-01-08T04:05:11Z | 816 | 4 | 5 | 11 | 1 |
| 15 | 1999-01-08T04:05:11.7Z | 1999-01-08T04:05:11Z | 186 | 4 | 5 | 11 | 2 |
| 16 | 1999-01-08T04:05:12Z | 1999-01-08T04:05:12Z | 862 | 4 | 5 | 12 | 1 |
| 17 | 1999-01-08T04:05:12.5Z | 1999-01-08T04:05:12Z | 826 | 4 | 5 | 12 | 2 |
as you can see the t_sequence start over every second but I want it to start over every 2 seconds,
is there a way to do it?
link for SQL fiddle with all the data

Tibco Spotfire - Calculate average only if there are minimum 3 values in a column - see desc

I want to calculate average in Spotfire only when there are minimum 3 values. if there are no values or just 2 values the average should be blank
Raw data:
Product Age Average
1
2
3 10
4 12
5 13 11
6
7 18
8 19
9 20 19
10 21 20
The only way I could really do this is with 3 calculated columns. Insert these calculated columns in this order:
If(Min(If([Age] IS NULL,0,[Age])) over (LastPeriods(3,[Product]))<>0,1) as [BitFlag]
Avg([Age]) over (LastPeriods(3,[Product])) as [TempAvg]
If([BitFlag]=1,[TempAvg]) as [Average]
This will give you the following results. You can ignore / hide the two columns you don't care about.
RESULTS
+---------+-----+---------+------------------+------------------+
| Product | Age | BitFlag | TempAvg | Average |
+---------+-----+---------+------------------+------------------+
| 1 | | | | |
| 2 | | | | |
| 3 | 10 | | 10 | |
| 4 | 12 | | 11 | |
| 5 | 13 | 1 | 11.6666666666667 | 11.6666666666667 |
| 6 | | | 12.5 | |
| 7 | 18 | | 15.5 | |
| 8 | 19 | | 18.5 | |
| 9 | 20 | 1 | 19 | 19 |
| 10 | 21 | 1 | 20 | 20 |
| 11 | | | 20.5 | |
| 12 | 22 | | 21.5 | |
| 13 | 36 | | 29 | |
| 14 | | | 29 | |
| 15 | 11 | | 23.5 | |
| 16 | 23 | | 17 | |
| 17 | 14 | 1 | 16 | 16 |
+---------+-----+---------+------------------+------------------+

org mode - simplify table sum formula of multiple column

Now I need 5 formula for sum of each column, it works fine but I wish it can be simplified to one formula. Is it possible?
|----+----+----+-----+----|
| a | b | c | d | e |
|----+----+----+-----+----|
| 1 | 2 | 3 | 4 | 5 |
| 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 |
|----+----+----+-----+----|
| 34 | 38 | 42 | 160 | 50 |
|----+----+----+-----+----|
#+TBLFM: #>$5=vsum(#2$5..#-1$5)::#>$4=vsum(#2$1..#-1$4)::#>$3=vsum(#2$3..#-1$3)::#>$2=vsum(#2$2..#-1$2)::#>$1=vsum(#2$1..#-1$1)
This should work:
|----+----+----+----+----|
| a | b | c | d | e |
|----+----+----+----+----|
| 1 | 2 | 3 | 4 | 5 |
| 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 |
|----+----+----+----+----|
| 34 | 38 | 42 | 46 | 50 |
|----+----+----+----+----|
#+TBLFM: #>$1..#>$5=vsum(#2$0..#-1$0)
$0 on the RHS is the current column.

How to fill right in org-mode?

Perhaps I missed this in the documentation but can Anyone point Me in the direction of how to fill right a series of columns in emacs's org-mode? I believe I saw how to fill down but do not recall seeing how to fill right.
Edit: For example, I am looking for a way to take:
| 8 | 6 | 7 | 5 | 3 | 0 | 9 |
| :=#1$1*2 | | | | | | |
And turn it into:
| 8 | 6 | 7 | 5 | 3 | 0 | 9 |
| :=#1$1*2 | :=#1$2*2 | :=#1$3*2 | :=#1$4*2 | :=#1$5*2 | :=#1$6*2 | :=#1$7*2 |
Which evaluates to:
| 16 | 12 | 14 | 10 | 6 | 0 | 18 |
Starting with this state:
| 8 | 6 | 7 | 5 | 3 | 0 | 9 |
| | | | | | | |
#+TBLFM: #2=#1*2
You get to this state:
| 8 | 6 | 7 | 5 | 3 | 0 | 9 |
| 16 | 12 | 14 | 10 | 6 | 0 | 18 |
#+TBLFM: #2=#1*2
by pressing C-c C-c while on the line with TBLFM.