Postgresql - Using AVG on a resulting column in HAVING clause - postgresql

I'm fairly new to Postgresql (and to SQL itself) so forgive me if I'm missing something (yes, I did try to use the search as well).
I'm trying to use the average of a resulting column from a query in the HAVING clause. Is it possible? Or is there a better solution?
I would like the column with Alias 'Sent_traffic' to display only those rows with a value greater than or equal to the average of that column.
The following query is what I would like to do (doesn't work because of the HAVING clause):
select p.name, SUM(f.bytes_ab) as Sent_traffic
from pcap_flow f
inner join ports p
ON f.source_ip = p.source_ip AND f.source_port=p.source_port AND p.destination_ip=f.destination_ip AND p.destination_port=f.destination_port
WHERE p.logged_at BETWEEN f.first_packet AND f.last_packet
AND p.logged_at BETWEEN '2016-03-15' AND '2016-04-01'
AND p.name!=''
GROUP BY p.name
HAVING SUM(f.bytes_ba) >= AVG(Sent_traffic)
ORDER BY Sent DESC
LIMIT 10;
What does work is the the same query without the AVG(Sent) in the HAVING clause:
select p.name, SUM(f.bytes_ab) as Sent_traffic
from pcap_flow f
inner join ports p
ON f.source_ip = p.source_ip AND f.source_port=p.source_port AND p.destination_ip=f.destination_ip AND p.destination_port=f.destination_port
WHERE p.logged_at BETWEEN f.first_packet AND f.last_packet
AND p.logged_at BETWEEN '2016-03-15' AND '2016-04-01'
AND p.name!=''
GROUP BY p.name
HAVING SUM(f.bytes_ba) >= 999999
ORDER BY Sent DESC
LIMIT 10;
A snapshot of the tables is as follows:
Table pcap_flow:
pcap_flow table in PgAdmin
Table ports:
ports table in PgAdmin
pcap_flow table:
id | pcap_id | flow_code | source_ip | source_port | destination_ip | destination_port | protocol | first_packet | last_packet | status | total_time | total_packets | idle_time_ab | idle_time_ba | bytes_ab | bytes_ba
-------+---------+-----------+---------------------------+-------------+---------------------------+------------------+----------+----------------------------+----------------------------+--------------------------+--------------+---------------+--------------+--------------+----------+----------
1 | 42 | a2b | 192.168.0.22 | 50191 | 128.93.101.81 | 443 | T | 2016-03-25 09:43:46.184039 | 2016-03-25 09:43:55.950184 | reset | 9.766144 | 10 | 9.7053 | 9.7065 | 510 | 3601
2 | 42 | c2d | 192.168.0.22 | 50127 | 74.125.133.189 | 443 | T | 2016-03-25 09:43:46.212468 | 2016-03-25 09:44:04.860872 | reset (syns 0) (fins 1) | 18.648403 | 6 | | | 0 | 0
3 | 42 | e2f | 192.168.0.22 | 50194 | 192.168.0.254 | 80 | T | 2016-03-25 09:43:46.302105 | 2016-03-25 09:43:49.615557 | yes | 3.313451 | 10 | 1.7933 | 2.0006 | 336 | 1421
4 | 42 | g2h | 192.168.0.22 | 50196 | 104.16.27.216 | 80 | T | 2016-03-25 09:43:46.335128 | 2016-03-25 09:43:46.454677 | yes | 0.119549 | 5 | 0.1172 | 0.1162 | 236 | 1705
5 | 42 | i2j | 192.168.0.254 | 443 | 192.168.0.22 | 50190 | T | 2016-03-25 09:43:46.420872 | 2016-03-25 09:43:46.422176 | no (syns 0) (fins 2) | 0.001304 | 6 | 0.0012 | 0.0002 | 2063 | 74
6 | 42 | k2l | 192.168.0.22 | 50197 | 192.168.0.254 | 443 | T | 2016-03-25 09:43:46.457142 | 2016-03-25 09:43:57.94442 | yes | 11.487277 | 26 | 2.001 | 2.001 | 3678 | 18859
7 | 42 | m2n | 192.168.0.22 | 50170 | 192.168.0.254 | 443 | T | 2016-03-25 09:43:46.509135 | 2016-03-25 09:43:46.51023 | reset (syns 0) (fins 2) | 0.001095 | 2 | 0.001 | 0.0001 | 0 | 37
8 | 42 | o2p | 192.168.0.22 | 50161 | 54.149.211.23 | 443 | T | 2016-03-25 09:43:46.510764 | 2016-03-25 09:43:46.512014 | reset (syns 0) (fins 2) | 0.00125 | 3 | 0.0011 | 0 | 37 | 37
9 | 42 | q2r | 192.168.0.22 | 50198 | 192.168.0.254 | 443 | T | 2016-03-25 09:43:46.511504 | 2016-03-25 09:43:46.744645 | reset | 0.233141 | 7 | 0.138 | 0.0981 | 385 | 4342
10 | 42 | s2t | 192.168.0.22 | 50199 | 192.168.0.254 | 443 | T | 2016-03-25 09:43:46.511667 | 2016-03-25 09:43:46.962999 | reset | 0.451332 | 8 | 0.2478 | 0.2018 | 385 | 4342
11 | 42 | u2v | 192.168.0.22 | 50200 | 104.16.27.216 | 80 | T | 2016-03-25 09:43:46.600772 | 2016-03-25 09:43:46.776045 | yes | 0.175273 | 5 | 0.166 | 0.1648 | 463 | 1604
12 | 42 | w2x | 192.168.0.22 | 50201 | 104.16.27.216 | 80 | T | 2016-03-25 09:43:46.606515 | 2016-03-25 09:43:46.760278 | yes | 0.153763 | 6 | 0.1517 | 0.1504 | 463 | 1604
13 | 42 | y2z | 192.168.0.22 | 50163 | 172.217.16.78 | 443 | T | 2016-03-25 09:43:46.744559 | 2016-03-25 09:43:46.746244 | reset (syns 0) (fins 2) | 0.001685 | 3 | 0.0014 | 0.0001 | 37 | 37
14 | 42 | aa2ab | 192.168.0.22 | 50202 | 192.168.0.254 | 443 | T | 2016-03-25 09:43:46.763646 | 2016-03-25 09:43:48.673286 | reset | 1.90964 | 8 | 1.5839 | 1.789 | 397 | 4342
15 | 42 | ac2ad | 192.168.0.22 | 50203 | 104.16.27.216 | 80 | T | 2016-03-25 09:43:46.854744 | 2016-03-25 09:43:46.998117 | yes | 0.143373 | 5 | 0.1414 | 0.1401 | 463 | 1604
ports table:
id | session_id | pid | name | protocol | source_ip | destination_ip | source_port | destination_port | state | logged_at
--------+------------+-------+-------------------------+----------+---------------------------+---------------------------+-------------+------------------+-------+-------------------------
1 | 1 | 676 | svchost.exe | 17 | 0.0.0.0 | | 68 | | 2 | 2016-03-16 09:41:04.716
2 | 1 | 4 | | 17 | 192.168.0.22 | | 137 | | 2 | 2016-03-16 09:41:04.716
3 | 1 | 4 | | 17 | 192.168.0.22 | | 138 | | 2 | 2016-03-16 09:41:04.716
4 | 1 | 3408 | svchost.exe | 17 | 127.0.0.1 | | 1900 | | 2 | 2016-03-16 09:41:04.716
5 | 1 | 3408 | svchost.exe | 17 | 192.168.0.22 | | 1900 | | 2 | 2016-03-16 09:41:04.716
6 | 1 | 3092 | uoipservice.exe | 17 | 192.168.0.22 | | 1900 | | 2 | 2016-03-16 09:41:04.716
7 | 1 | 2208 | mdnsresponder.exe | 17 | 192.168.0.22 | | 5353 | | 2 | 2016-03-16 09:41:04.716
8 | 1 | 1032 | svchost.exe | 17 | 0.0.0.0 | | 5355 | | 2 | 2016-03-16 09:41:04.716
9 | 1 | 2208 | mdnsresponder.exe | 17 | 0.0.0.0 | | 49152 | | 2 | 2016-03-16 09:41:04.716
10 | 1 | 3092 | uoipservice.exe | 17 | 192.168.0.22 | | 51128 | | 2 | 2016-03-16 09:41:04.716
11 | 1 | 3092 | uoipservice.exe | 17 | 192.168.0.22 | | 51129 | | 2 | 2016-03-16 09:41:04.716
12 | 1 | 3408 | svchost.exe | 17 | 192.168.0.22 | | 61182 | | 2 | 2016-03-16 09:41:04.716
13 | 1 | 3408 | svchost.exe | 17 | 127.0.0.1 | | 61183 | | 2 | 2016-03-16 09:41:04.716
14 | 1 | 676 | svchost.exe | 17 | fe80::1d77:665c:b2e5:3be3 | | 546 | | 2 | 2016-03-16 09:41:04.716
15 | 1 | 3408 | svchost.exe | 17 | ::1 | | 1900 | | 2 | 2016-03-16 09:41:04.716
16 | 1 | 3408 | svchost.exe | 17 | fe80::1d77:665c:b2e5:3be3 | | 1900 | | 2 | 2016-03-16 09:41:04.716
17 | 1 | 2208 | mdnsresponder.exe | 17 | ::1 | | 5353 | | 2 | 2016-03-16 09:41:04.716
18 | 1 | 1032 | svchost.exe | 17 | :: | | 5355 | | 2 | 2016-03-16 09:41:04.716
19 | 1 | 2208 | mdnsresponder.exe | 17 | :: | | 49153 | | 2 | 2016-03-16 09:41:04.716
20 | 1 | 3408 | svchost.exe | 17 | fe80::1d77:665c:b2e5:3be3 | | 61180 | | 2 | 2016-03-16 09:41:04.716
21 | 1 | 3408 | svchost.exe | 17 | ::1 | | 61181 | | 2 | 2016-03-16 09:41:04.716
22 | 1 | 972 | svchost.exe | 6 | 0.0.0.0 | 0.0.0.0 | 135 | 0 | 2 | 2016-03-16 09:41:04.716
23 | 1 | 4 | | 6 | 192.168.0.22 | 0.0.0.0 | 139 | 0 | 2 | 2016-03-16 09:41:04.716
24 | 1 | 1728 | devmonsrv.exe | 6 | 127.0.0.1 | 0.0.0.0 | 515 | 0 | 2 | 2016-03-16 09:41:04.716
25 | 1 | 2208 | mdnsresponder.exe | 6 | 127.0.0.1 | 0.0.0.0 | 5354 | 0 | 2 | 2016-03-16 09:41:04.716
26 | 1 | 2564 | hostviewcli.exe | 6 | 127.0.0.1 | 0.0.0.0 | 40123 | 0 | 2 | 2016-03-16 09:41:04.716
27 | 1 | 716 | wininit.exe | 6 | 0.0.0.0 | 0.0.0.0 | 49152 | 0 | 2 | 2016-03-16 09:41:04.716
28 | 1 | 676 | svchost.exe | 6 | 0.0.0.0 | 0.0.0.0 | 49153 | 0 | 2 | 2016-03-16 09:41:04.716
29 | 1 | 1076 | svchost.exe | 6 | 0.0.0.0 | 0.0.0.0 | 49154 | 0 | 2 | 2016-03-16 09:41:04.716
30 | 1 | 772 | services.exe | 6 | 0.0.0.0 | 0.0.0.0 | 49155 | 0 | 2 | 2016-03-16 09:41:04.716
31 | 1 | 4872 | vpnui.exe | 6 | 127.0.0.1 | 127.0.0.1 | 49156 | 62522 | 5 | 2016-03-16 09:41:04.716
32 | 1 | 788 | lsass.exe | 6 | 0.0.0.0 | 0.0.0.0 | 49157 | 0 | 2 | 2016-03-16 09:41:04.716
33 | 1 | 3092 | uoipservice.exe | 6 | 192.168.0.22 | 0.0.0.0 | 49164 | 0 | 2 | 2016-03-16 09:41:04.716

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.

How to group MVA field for faceted in sphinx

I have an index where some data's has duplicate, all fields are similar except for latitude,longitude and id (field id is not realy ID, just generated row_number() OVER () AS id).
it's example:
mysql> select id,vacancy_id,prof_area_ids,latitude,longitude from jobVacancy;
+------+------------+---------------+----------+-----------+
| id | vacancy_id | prof_area_ids | latitude | longitude |
+------+------------+---------------+----------+-----------+
| 1 | 917 | 11,199,202 | 0.973178 | 0.743566 |
| 2 | 916 | 17,283,288 | 0.973178 | 0.743566 |
| 3 | 915 | 17,288 | 0.973178 | 0.743566 |
| 4 | 914 | 30,482 | 0.973178 | 0.743566 |
| 5 | 919 | 15,243 | 0.825153 | 0.692837 |
| 6 | 919 | 15,243 | 0.825162 | 0.692828 |
| 7 | 918 | 8,154 | 0.825153 | 0.692837 |
| 8 | 918 | 8,154 | 0.825162 | 0.692828 |
| 9 | 920 | 17,283,288 | 0.958914 | 1.282161 |
| 10 | 920 | 17,283,288 | 0.958915 | 1.282215 |
| 11 | 924 | 12,208 | 0.97333 | 0.658246 |
| 12 | 924 | 12,208 | 0.973336 | 0.658237 |
| 13 | 923 | 21,365 | 0.97333 | 0.658246 |
| 14 | 923 | 21,365 | 0.973336 | 0.658237 |
| 15 | 922 | 20,359 | 0.97333 | 0.658246 |
| 16 | 922 | 20,359 | 0.973336 | 0.658237 |
| 17 | 921 | 19,346 | 0.97333 | 0.658246 |
| 18 | 921 | 19,346 | 0.973336 | 0.658237 |
| 19 | 926 | 12,17,208,292 | 0.88396 | 2.389868 |
| 20 | 925 | 12,208 | 0.88396 | 2.389868 |
+------+------------+---------------+----------+-----------+
20 rows in set (0.00 sec)
Now I want to group data by vacancy_id
mysql> select id,vacancy_id,prof_area_ids,latitude,longitude from jobVacancy group by vacancy_id;
+------+------------+---------------+----------+-----------+
| id | vacancy_id | prof_area_ids | latitude | longitude |
+------+------------+---------------+----------+-----------+
| 1 | 917 | 11,199,202 | 0.973178 | 0.743566 |
| 2 | 916 | 17,283,288 | 0.973178 | 0.743566 |
| 3 | 915 | 17,288 | 0.973178 | 0.743566 |
| 4 | 914 | 30,482 | 0.973178 | 0.743566 |
| 5 | 919 | 15,243 | 0.825153 | 0.692837 |
| 7 | 918 | 8,154 | 0.825153 | 0.692837 |
| 9 | 920 | 17,283,288 | 0.958914 | 1.282161 |
| 11 | 924 | 12,208 | 0.97333 | 0.658246 |
| 13 | 923 | 21,365 | 0.97333 | 0.658246 |
| 15 | 922 | 20,359 | 0.97333 | 0.658246 |
| 17 | 921 | 19,346 | 0.97333 | 0.658246 |
| 19 | 926 | 12,17,208,292 | 0.88396 | 2.389868 |
| 20 | 925 | 12,208 | 0.88396 | 2.389868 |
| 21 | 961 | 4,105 | 0.959217 | 1.280721 |
| 23 | 960 | 8,155 | 0.959217 | 1.280721 |
| 25 | 959 | 12,208 | 0.959217 | 1.280721 |
| 27 | 928 | 1,60 | 0.963734 | 1.070297 |
| 29 | 927 | 32,513 | 0.963734 | 1.070297 |
| 31 | 929 | 6,140 | 0.786553 | 0.678649 |
| 33 | 932 | 1,40,46 | 0.824627 | 0.694182 |
+------+------------+---------------+----------+-----------+
20 rows in set (0.00 sec)
Result is awesome! But problem begins when I want to get all grouped data with faceted
mysql> select id,vacancy_id,prof_area_ids,latitude,longitude from jobVacancy where prof_area_ids=199 group by vacancy_id facet prof_area_ids;
+------+------------+-----------------+----------+-----------+
| id | vacancy_id | prof_area_ids | latitude | longitude |
+------+------------+-----------------+----------+-----------+
| 1 | 917 | 11,199,202 | 0.973178 | 0.743566 |
| 191 | 1004 | 11,196,199 | 0.925335 | 2.768874 |
| 313 | 1072 | 1,11,60,197,199 | 0.963968 | 1.070624 |
| 318 | 1136 | 11,196,199 | 0.96071 | 1.448998 |
| 374 | 1097 | 11,199 | 0.785255 | 0.678504 |
+------+------------+-----------------+----------+-----------+
5 rows in set (0.00 sec)
+---------------+----------+
| prof_area_ids | count(*) |
+---------------+----------+
| 202 | 1 |
| 199 | 12 |
| 11 | 12 |
| 196 | 5 |
| 197 | 3 |
| 60 | 3 |
| 1 | 3 |
+---------------+----------+
7 rows in set (0.02 sec)
Faceted result is incorrect. Because in fact data's count where prof_area_ids=199 must be 5 and not 12. So how I can group field for faceted?
Additionaly
I fount here http://sphinxsearch.com/blog/2013/06/21/faceted-search-with-sphinx/ but just written "If you have a MVA facet, you need to use the GROUPBY() function which returns the actual value on which the grouping was made." and without examle.
mysql> select id,vacancy_id,prof_area_ids,latitude,longitude,GROUPBY() as selected,COUNT(*) from jobVacancy where prof_area_ids=199 group by vacancy_id facet prof_area_ids;
+------+------------+-----------------+----------+-----------+----------+----------+
| id | vacancy_id | prof_area_ids | latitude | longitude | selected | count(*) |
+------+------------+-----------------+----------+-----------+----------+----------+
| 1 | 917 | 11,199,202 | 0.973178 | 0.743566 | 917 | 1 |
| 191 | 1004 | 11,196,199 | 0.925335 | 2.768874 | 1004 | 2 |
| 313 | 1072 | 1,11,60,197,199 | 0.963968 | 1.070624 | 1072 | 3 |
| 318 | 1136 | 11,196,199 | 0.96071 | 1.448998 | 1136 | 3 |
| 374 | 1097 | 11,199 | 0.785255 | 0.678504 | 1097 | 3 |
+------+------------+-----------------+----------+-----------+----------+----------+
5 rows in set (0.00 sec)
+---------------+----------+
| prof_area_ids | count(*) |
+---------------+----------+
| 202 | 1 |
| 199 | 12 |
| 11 | 12 |
| 196 | 5 |
| 197 | 3 |
| 60 | 3 |
| 1 | 3 |
+---------------+----------+
7 rows in set (0.02 sec)
Also faceted result is wrong.
Seems, wanting effectively COUNT(DISTINCT vacancy_id) on the FACET rather than the default COUNT(*), but alas it turns out
... FACET prof_area_ids,COUNT(DISTINCT vacancy_id) AS vacancies BY prof_area_ids
doesnt work. The bit before BY only supports attributes, not custom functions.
... will just have to write it out the long way, with full queries...
select id,vacancy_id,prof_area_ids,latitude,longitude from jobVacancy
where prof_area_ids=199 group by vacancy_id;
SELECT GROUPBY() AS prof_area_id, COUNT(DISTINCT vacancy_id) FROM jobVacancy
WHERE prof_area_ids=199 GROUP BY prof_area_id;
Same results, just slightly more verbose. ie rather than using FACET shorthand, write it
out in full, as multiple seperate queries.
Faceted result is incorrect. Because in fact data's count where prof_area_ids=199 must be 5 and not 12. So how I can group field for faceted?
It looks like you misunderstand how FACET works. It seems to me, that you think it takes as a base the main query's result, but it actually just does another grouping. E.g. here:
mysql> select g, t from idx_mva where t = 11 group by g facet t;
+------+----------+
| g | t |
+------+----------+
| 1 | 11,12 |
| 2 | 11,13,15 |
| 3 | 9,11 |
| 5 | 11,12,15 |
+------+----------+
4 rows in set (0.00 sec)
+------+----------+
| t | count(*) |
+------+----------+
| 12 | 2 |
| 11 | 6 |
| 15 | 4 |
| 13 | 1 |
| 9 | 1 |
| 3 | 1 |
+------+----------+
6 rows in set (0.00 sec)
for t=11 you can see that as in your case it's found 3 times in the 1st query's result, but the count for that is 6 in the FACET's query result. This is because it actually occurs 6 times in the index:
mysql> select * from idx_mva where t = 11;
+------+------+----------+
| id | g | t |
+------+------+----------+
| 2 | 1 | 11,12 |
| 3 | 1 | 11,15 |
| 3 | 2 | 11,13,15 |
| 6 | 3 | 9,11 |
| 8 | 5 | 11,12,15 |
| 11 | 2 | 3,11,15 |
+------+------+----------+
6 rows in set, 1 warning (0.00 sec)
and it happens 3 times in the 1st case only because the t's value is returned only once for each of the groups. You can use group_concat() to see more values from the same group:
mysql> select g, group_concat(to_string(t)) from idx_mva where t = 11 group by g facet t;
+------+----------------------------+
| g | group_concat(to_string(t)) |
+------+----------------------------+
| 1 | 11,12,11,15 |
| 2 | 11,13,15,3,11,15 |
| 3 | 9,11 |
| 5 | 11,12,15 |
+------+----------------------------+
4 rows in set (0.00 sec)
+------+----------+
| t | count(*) |
+------+----------+
| 12 | 2 |
| 11 | 6 |
| 15 | 4 |
| 13 | 1 |
| 9 | 1 |
| 3 | 1 |
+------+----------+
6 rows in set (0.00 sec)
If you want to learn more about faceting here's an interactive course about that - https://play.manticoresearch.com/faceting/

Check previous and next record

I'm trying to compare different costs from different periods. But I dont no how I can compare the single record with the record before and after. What I need is a yes or no in my dataset when the costs from a records is the same as record before and record after.
My dataset looks like this:
+--------+-----------+----------+------------+-------+-----------+
| Client | Provision | CAK Year | CAK Period | Costs | Serial Nr |
+--------+-----------+----------+------------+-------+-----------+
| 1 | 210 | 2017 | 13 | 150 | 1 |
+--------+-----------+----------+------------+-------+-----------+
| 1 | 210 | 2018 | 1 | 200 | 2 |
+--------+-----------+----------+------------+-------+-----------+
| 1 | 210 | 2018 | 2 | 170 | 3 |
+--------+-----------+----------+------------+-------+-----------+
| 1 | 210 | 2018 | 3 | 150 | 4 |
+--------+-----------+----------+------------+-------+-----------+
| 1 | 210 | 2018 | 4 | 150 | 5 |
+--------+-----------+----------+------------+-------+-----------+
| 1 | 210 | 2018 | 5 | 150 | 6 |
+--------+-----------+----------+------------+-------+-----------+
| 1 | 689 | 2018 | 1 | 345 | 1 |
+--------+-----------+----------+------------+-------+-----------+
| 1 | 689 | 2018 | 2 | 345 | 1 |
+--------+-----------+----------+------------+-------+-----------+
| 1 | 689 | 2018 | 3 | 345 | 1 |
+--------+-----------+----------+------------+-------+-----------+
What i've tried so far:
CASE
WHEN Provision = Provision
AND Costs = LEAD(Costs, 1, 0) OVER(ORDER BY CAK Year, CAK Period)
AND Costs = LAG(Costs, 1, 0) OVER(ORDER BY CAK Year, CAK Period)
THEN 'Yes
ELSE 'No'
END
My expected result:
+--------+-----------+----------+------------+-------+-----------+--------+
| Client | Provision | CAK Year | CAK Period | Costs | Serial Nr | Result |
+--------+-----------+----------+------------+-------+-----------+--------+
| 1 | 210 | 2017 | 13 | 150 | 1 | No
+--------+-----------+----------+------------+-------+-----------+--------+
| 1 | 210 | 2018 | 1 | 200 | 2 | No
+--------+-----------+----------+------------+-------+-----------+--------+
| 1 | 210 | 2018 | 2 | 170 | 3 | No
+--------+-----------+----------+------------+-------+-----------+--------+
| 1 | 210 | 2018 | 3 | 150 | 4 | No
+--------+-----------+----------+------------+-------+-----------+--------+
| 1 | 210 | 2018 | 4 | 150 | 5 | Yes
+--------+-----------+----------+------------+-------+-----------+--------+
| 1 | 210 | 2018 | 5 | 150 | 6 | No
+--------+-----------+----------+------------+-------+-----------+--------+
| 1 | 689 | 2018 | 1 | 345 | 1 | No
+--------+-----------+----------+------------+-------+-----------+--------+
| 1 | 689 | 2018 | 2 | 345 | 1 | Yes
+--------+-----------+----------+------------+-------+-----------+--------+
| 1 | 689 | 2018 | 3 | 345 | 1 | No
+--------+-----------+----------+------------+-------+-----------+--------+
You guys can help me further because I don't get the expected result?
You need to add in partition by Provision otherwise your lag and lead ordering will run across all Provision values:
declare #d table(Client int,Provision int,CAKYear int, CAKPeriod int, Costs int, SerialNr int);
insert into #d values
(1,210,2017,13,150,1)
,(1,210,2018,1,200,2)
,(1,210,2018,2,170,3)
,(1,210,2018,3,150,4)
,(1,210,2018,4,150,5)
,(1,210,2018,5,150,6)
,(1,689,2018,1,345,1)
,(1,689,2018,2,345,1)
,(1,689,2018,3,345,1);
select *
,case when Provision = Provision
and Costs = lead(Costs, 1, 0) over(partition by Provision order by CAKYear, CAKPeriod)
and Costs = lag(Costs, 1, 0) over(partition by Provision order by CAKYear, CAKPeriod)
then 'Yes'
else 'No'
end as Result
from #d
order by Provision
,CAKYear
,CAKPeriod;
Output
+--------+-----------+---------+-----------+-------+----------+--------+
| Client | Provision | CAKYear | CAKPeriod | Costs | SerialNr | Result |
+--------+-----------+---------+-----------+-------+----------+--------+
| 1 | 210 | 2017 | 13 | 150 | 1 | No |
| 1 | 210 | 2018 | 1 | 200 | 2 | No |
| 1 | 210 | 2018 | 2 | 170 | 3 | No |
| 1 | 210 | 2018 | 3 | 150 | 4 | No |
| 1 | 210 | 2018 | 4 | 150 | 5 | Yes |
| 1 | 210 | 2018 | 5 | 150 | 6 | No |
| 1 | 689 | 2018 | 1 | 345 | 1 | No |
| 1 | 689 | 2018 | 2 | 345 | 1 | Yes |
| 1 | 689 | 2018 | 3 | 345 | 1 | No |
+--------+-----------+---------+-----------+-------+----------+--------+

I have a query that groups usage by user by day how would I add a running total to this query?

I have the following query:
SELECT
usersq1.id AS user_id, name, completed_at,
COUNT(usersq1.id) AS trips,
SUM(cost_amount_cents) AS daily_cost_amount_cents
FROM usersq1
LEFT OUTER JOIN tripsq1
ON usersq1.id = user_id
GROUP by usersq1.id, name, completed_at
ORDER by user_id, name, completed_at;
Which returns the following:
user_id | name | completed_at | trips | daily_cost_amount_cents
---------+---------------------+--------------+-------+-------------------------
1001 | Makeda Mosser | 2017-06-01 | 2 | 125
1001 | Makeda Mosser | 2017-06-02 | 1 | 125
1001 | Makeda Mosser | 2017-06-03 | 2 | 350
1001 | Makeda Mosser | 2017-06-04 | 2 | 200
1001 | Makeda Mosser | 2017-06-06 | 1 | 100
1001 | Makeda Mosser | 2017-06-07 | 1 | 125
1001 | Makeda Mosser | 2017-06-08 | 1 | 150
1002 | Libbie Luby | 2017-06-02 | 2 | 125
1002 | Libbie Luby | 2017-06-09 | 1 | 175
1003 | Linn Loughran | 2017-06-03 | 1 | 75
1004 | Natacha Ned | 2017-06-04 | 1 | 100
1005 | Lorrine Lunt | 2017-06-05 | 1 | 125
1006 | Tami Tineo | 2017-10-06 | 1 | 150
1007 | Delisa Deen | 2017-10-07 | 1 | 175
1008 | Mimi Miltenberger | 2017-10-08 | 1 | 200
1009 | Seth Sneller | 2017-10-09 | 1 | 25
1010 | Rickie Rossi | 2017-10-10 | 1 | 50
1011 | Jenise Jeanbaptiste | 2017-06-01 | 1 | 200
1011 | Jenise Jeanbaptiste | 2017-07-01 | 1 | 75
1012 | Genia Glatz | 2017-06-02 | 1 | 25
1012 | Genia Glatz | 2017-07-02 | 1 | 50
1013 | Onita Oddo | 2017-06-03 | 1 | 50
1014 | Dario Dreyer | 2017-06-04 | 1 | 75
1014 | Dario Dreyer | 2017-06-24 | 5 | 750
1015 | Toby Trent | | 1 |
I would like to produce another cumulative sum column which keeps a running total of daily_cost_amount_cents per user. The expected outlook I would like is something like this:
+---------+---------------------+------------+-------+-------------------------+-----------+
| user_id | name | created_at | trips | daily_cost_amount_cents | cum_cents |
+---------+---------------------+------------+-------+-------------------------+-----------+
| 1001 | Makeda Mosser | 6/1/17 | 2 | 125 | 125 |
| 1001 | Makeda Mosser | 6/2/17 | 1 | 125 | 250 |
| 1001 | Makeda Mosser | 6/3/17 | 2 | 350 | 600 |
| 1001 | Makeda Mosser | 6/4/17 | 2 | 200 | 800 |
| 1001 | Makeda Mosser | 6/6/17 | 1 | 100 | 900 |
| 1001 | Makeda Mosser | 6/7/17 | 1 | 125 | 1025 |
| 1001 | Makeda Mosser | 6/8/17 | 1 | 150 | 1175 |
| 1002 | Libbie Luby | 6/2/17 | 2 | 125 | 125 |
| 1002 | Libbie Luby | 6/9/17 | 1 | 175 | 300 |
| 1003 | Linn Loughran | 6/3/17 | 1 | 75 | 75 |
| 1004 | Natacha Ned | 6/4/17 | 1 | 100 | 100 |
| 1005 | Lorrine Lunt | 6/5/17 | 1 | 125 | 125 |
| 1006 | Tami Tineo | 10/6/17 | 1 | 150 | 150 |
| 1007 | Delisa Deen | 10/7/17 | 1 | 175 | 175 |
| 1008 | Mimi Miltenberger | 10/8/17 | 1 | 200 | 200 |
| 1009 | Seth Sneller | 10/9/17 | 1 | 25 | 25 |
| 1010 | Rickie Rossi | 10/10/17 | 1 | 50 | 50 |
| 1011 | Jenise Jeanbaptiste | 6/1/17 | 1 | 200 | 200 |
| 1011 | Jenise Jeanbaptiste | 7/1/17 | 1 | 75 | 275 |
| 1012 | Genia Glatz | 6/2/17 | 1 | 25 | 25 |
| 1012 | Genia Glatz | 7/2/17 | 1 | 50 | 75 |
| 1013 | Onita Oddo | 6/3/17 | 1 | 50 | 50 |
| 1014 | Dario Dreyer | 6/4/17 | 1 | 75 | 75 |
| 1014 | Dario Dreyer | 6/24/17 | 5 | 750 | 750 |
| 1015 | Toby Trent | | 0 | | |
+---------+---------------------+------------+-------+-------------------------+-----------+
I am pretty sure that I need to use a window function to do this but can't seem to do it while preserving the grouping by user_id and created_by
The problem is that in the presence of a GROUP BY clause, the window functions iterate over each group rather than multiple grouped rows. Put your query into a WITH clause and you can easily do the windowing you want:
WITH t AS (
SELECT usersq1.id AS user_id,
name,
completed_at,
COUNT(completed_at) AS trips, -- To correctly handle 0 trips
SUM(cost_amount_cents) AS daily_cost_amount_cents
FROM usersq1
LEFT OUTER JOIN tripsq1 ON usersq1.id = user_id
GROUP BY usersq1.id, name, completed_at
ORDER BY user_id, name, completed_at
) SELECT user_id,
name,
completed_at AS created_at,
trips,
daily_cost_amount_cents,
SUM(daily_cost_amount_cents) OVER (PARTITION BY user_id
ORDER BY user_id, completed_at)
FROM t;

Divison with more than one result from postgresql query

I am using postgresql and I have a table called accidents (state, total accidents) and another table called population. I want to get the top 3 state names with high total accidents and then get the population of those 3 states divided by total accidents in postgresql? How to write the query in the following way?
Explanation:
Population Table
rank| state | population
---+-----------------------------+------------
1 | Uttar Pradesh | 199581477
2 | Maharashtra | 112372972
3 | Bihar | 103804630
4 | West Bengal | 91347736
5 | Madhya Pradesh | 72597565
6 | Tamil Nadu | 72138958
7 | Rajasthan | 68621012
8 | Karnataka | 61130704
9 | Gujarat | 60383628
10 | Andhra Pradesh | 49665533
11 | Odisha | 41947358
12 | Telangana | 35193978
13 | Kerala | 33387677
14 | Jharkhand | 32966238
15 | Assam | 31169272
16 | Punjab | 27704236
17 | Haryana | 25753081
18 | Chhattisgarh | 25540196
19 | Jammu and Kashmir | 12548926
20 | Uttarakhand | 10116752
21 | Himachal Pradesh | 6856509
22 | Tripura | 3671032
23 | Meghalaya | 2964007
24 | Manipur*β* | 2721756
25 | Nagaland | 1980602
26 | Goa | 1457723
27 | Arunachal Pradesh | 1382611
28 | Mizoram | 1091014
29 | Sikkim | 607688
30 | Delhi | 16753235
31 | Puducherry | 1244464
32 | Chandigarh | 1054686
33 | Andaman and Nicobar Islands | 379944
34 | Dadra and Nagar Haveli | 342853
35 | Daman and Diu | 242911
36 | Lakshadweep | 64429
accident table:
state | eqto8 | eqto10 | mrthn10 | ntknwn | total
-----------------------------+-------+--------+---------+--------+--------
Andhra Pradesh | 6425 | 8657 | 8144 | 19298 | 42524
Arunachal Pradesh | 88 | 76 | 87 | 0 | 251
Assam | 0 | 0 | 0 | 6535 | 6535
Bihar | 2660 | 3938 | 3722 | 0 | 10320
Chhattisgarh | 2888 | 7052 | 3571 | 0 | 13511
Goa | 616 | 1512 | 2184 | 0 | 4312
Gujarat | 4864 | 7864 | 7132 | 8089 | 27949
Haryana | 3365 | 2588 | 4112 | 0 | 10065
Himachal Pradesh | 276 | 626 | 977 | 1020 | 2899
Jammu and Kashmir | 1557 | 618 | 434 | 4100 | 6709
Jharkhand | 1128 | 701 | 1037 | 2845 | 5711
Karnataka | 11167 | 14715 | 18566 | 0 | 44448
Kerala | 5580 | 13271 | 17323 | 0 | 36174
Madhya Pradesh | 15630 | 16226 | 19354 | 0 | 51210
Maharashtra | 4117 | 5350 | 10538 | 46311 | 66316
Manipur | 147 | 453 | 171 | 0 | 771
Meghalaya | 210 | 154 | 119 | 0 | 483
Mizoram | 27 | 58 | 25 | 0 | 110
Nagaland | 11 | 13 | 18 | 0 | 42
Odisha | 1881 | 3120 | 4284 | 0 | 9285
Punjab | 1378 | 2231 | 1825 | 907 | 6341
Rajasthan | 5534 | 5895 | 5475 | 6065 | 22969
Sikkim | 6 | 144 | 8 | 0 | 158
Tamil Nadu | 8424 | 18826 | 29871 | 10636 | 67757
Tripura | 290 | 376 | 222 | 0 | 888
Uttarakhand | 318 | 305 | 456 | 393 | 1472
Uttar Pradesh | 8520 | 10457 | 10995 | 0 | 29972
West Bengal | 1494 | 1311 | 974 | 8511 | 12290
Andaman and Nicobar Islands | 18 | 104 | 114 | 0 | 236
Chandigarh | 112 | 39 | 210 | 58 | 419
Dadra and Nagar Haveli | 40 | 20 | 17 | 8 | 85
Daman and Diu | 11 | 6 | 8 | 25 | 50
Delhi | 0 | 0 | 0 | 6937 | 6937
Lakshadweep | 0 | 0 | 0 | 3 | 3
Puducherry | 154 | 668 | 359 | 0 | 1181
All India | 88936 | 127374 | 152332 | 121741 | 490383
So that result should be
21.57
81.03
107.44
explanation:
Highest accidents states Tamilnadu, Maharashtra, Madhyapradesh.
Tamilnadu population/accidents = 21213/983 = 21.57 (Assumed values)
Maharasthra population/accidents = 10000/123 = 81.03
Madhyapradesh population/accidents = 34812/324 = 107.44
My query is:
SELECT POPULATION/
(SELECT TOTAL
FROM accidents
WHERE STATE NOT LIKE 'All %'
ORDER BY TOTAL DESC
LIMIT 3)
aVG FROM population
WHERE STATE IN
(SELECT STATE
FROM accidents
WHERE STATE NOT LIKE 'All %'
ORDER BY TOTAL DESC
LIMIT 3);
throwing ERROR: more than one row returned by a subquery used as an expression.
How to modify the query to get the required result or any other way to get the result in postgresql?
This ought to do it.
SELECT a.state, population.population/a.total FROM
(SELECT total, state FROM accidents WHERE state <> 'All India' ORDER BY total DESC LIMIT 3 ) AS a
INNER JOIN population on a.state = population.state