Get latest Date Record mysql - mysql

timestamp_read timestamp entity_id status currently_in
10/26/16 15:31 1477495880 1758 0 36 West
11/1/16 19:08 1478027336 1758 0 36 West
11/28/16 19:42 1480362171 1758 0 36 West
12/17/16 16:50 1481993441 1758 0 36 West
1/10/17 21:17 1484083045 1758 1 36 West
11/18/16 19:56 1479499012 1841 0 Service
12/7/16 16:33 1481128427 1841 0 Attention
11/4/16 15:05 1478271946 1902 0 36 West
11/28/16 16:47 1480351626 1902 0 36 West
12/16/16 23:49 1481932191 1902 0 36 West
1/10/17 21:32 1484083954 1902 1 36 West
11/1/16 18:54 1478026491 1904 0 36 West
11/28/16 17:28 1480354089 1904 0 36 West
12/17/16 18:09 1481998170 1904 0 36 West
12/21/16 14:53 1482332016 1904 0 55 West
1/6/17 19:34 1483731252 1904 0 Show
1/11/17 16:01 1484150479 1904 0 55 West
1/17/17 17:31 1484674280 1904 1 36 West
I have to audit a LOG on not the best designed table.
I need the latest record from column 'entity' based on a unixtime column 'timestamp'
Any help would be much appreciated

select * from scan_log order by timestamp desc limit 1
Is that what you want? You haven't described any foreign keys or relations.

Related

mysql return 360 degrees returned with strengths

I have a table of wind directions and strengths over a 24 hour period, sample data at the bottom of this question.
only directions that have strengths are stored in the database, I'm currently using the following SQL:
SELECT winddirection, avespeed
FROM wp_weather_data
WHERE ID%10 = 0
what I would like to return is an entry for every degree (0 value for any degree not in the db) and where there are multiple entries for a given degree to only return the highest value. Oh, and they need to be in ascending order of degrees.
Is this possible?
This is so I can plot a wind distribution chart on a polar chat plugin in WordPress.
sample data returned from the above sql:
294 2
271 3
269 2
285 3
289 2
123 1
130 1
144 1
160 0
168 0
161 0
135 0
138 0
331 0
115 0
136 0
161 0
267 0
114 0
265 0
204 0
248 1
206 0
199 1
250 2
244 3
257 3
272 5
267 5
208 3
221 3
223 4
253 6
233 5

Create view with delta for each column in SQL

I have a table named pwrDay containing electric index counters (always growing).
jour
pwrconsohp
pwrconsohc
pwrprod
pwrprodmax
2021-09-26
35 736 527
18 073 331
12 629 677
0
2021-09-27
35 754 125
18 073 331
12 637 154
0
2021-09-28
35 780 113
18 073 331
12 646 963
0
2021-09-29
35 807 081
18 073 331
12 657 084
0
2021-09-30
35 833 193
18 073 331
12 668 804
0
2021-10-01
35 861 259
18 073 331
12 682 444
0
2021-10-02
35 888 342
18 073 331
12 693 908
0
2021-10-03
35 917 218
18 073 331
12 704 696
0
2021-10-04
35 944 869
18 073 331
12 706 056
0
2021-10-05
35 972 043
18 073 331
12 708 309
0
I need to extract the difference between previous and current row (maybe create a view?) The following query works for most days, but it's wrong every first day of month (or if I miss a control day):
SELECT pwr.jour,
(pwr.pwrconsoHP-ifnull(oldpwr.pwrconsoHP, 0)) as deltaconsoHP,
(pwr.pwrconsoHC-ifnull(oldpwr.pwrconsoHC, 0)) as deltaconsoHC,
(pwr.pwrProd-ifnull(oldpwr.pwrProd, 0)) as deltaProd
FROM pwrDay pwr
LEFT OUTER JOIN pwrDay oldpwr ON
(day(pwr.jour)-day(oldpwr.jour)=1 AND MONTH(pwr.jour)=MONTH(oldpwr.jour))
ORDER BY jour;
I also tried this query:
SELECT pwr.jour,
(pwr.pwrconsoHP-LAG(pwr.pwrconsoHP, 0)) as deltaconsoHP,
(pwr.pwrconsoHC-LAG(pwr.pwrconsoHC, 0)) as deltaconsoHC,
(pwr.pwrProd-LAG(pwr.pwrProd, 0)) as deltaProd
FROM pwrDay pwr
ORDER BY jour;
However, it doesn't run at all. I get this error message:
Erreur SQL (1305) : FUNCTION velbus.LAG does not exist
How can I write this query?
SELECT pwr.jour,
(pwr.pwrconsoHP-LAG(pwr.pwrconsoHP, 0) OVER(order by jour)) as deltaconsoHP,
(pwr.pwrconsoHC-LAG(pwr.pwrconsoHC, 0) OVER(order by jour)) as deltaconsoHC,
(pwr.pwrProd-LAG(pwr.pwrProd, 0) OVER(order by jour)) as deltaProd
FROM pwrDay pwr
ORDER BY jour;
give it a try ...

BeautifulSoup parsed only one Column instead of entire Wikipedia table in Python

I am trying to parse 1st table located here using BeautifulSoup in Python. It parsed my First column but for some reason It didn't parsed entire table. Any help is appreciated!
Note: I am trying to parse entire table and convert into pandas dataframe
My Code:
import requests
from bs4 import BeautifulSoup
WIKI_URL = requests.get("https://en.wikipedia.org/wiki/NCAA_Division_I_FBS_football_win-loss_records").text
soup = BeautifulSoup(WIKI_URL, features="lxml")
print(soup.prettify())
my_table = soup.find('table',{'class':'wikitable sortable'})
links=my_table.findAll('a')
print(links)
It only parsed one column because you did a findall for only the items in the first column. To parse the entire table you'd have to do a findall for the table rows <tr> and then a findall within each row for the table divides <td>. Right now you are just doing a findall for the links and then printing the links.
my_table = soup.find('table',{'class':'wikitable sortable'})
for row in mytable.findAll('tr'):
print(','.join([td.get_text(strip=True) for td in row.findAll('td')]))
NOTE: Accept B.Adler's solution as it is good work and sound advice. This solution is simply so you can see some alternatives as you are learning.
Whenever I see <table> tags, I'll usually check out pandas first to see if I can find what I need from the tables that way. pd.read_html() will return a list of dataframes, and you can work/manipulate those to extract what you need.
import pandas as pd
WIKI_URL = "https://en.wikipedia.org/wiki/NCAA_Division_I_FBS_football_win-loss_records"
tables = pd.read_html(WIKI_URL)
You can also look through the dataframes to see which has the data you want.
I just used dataframe in index position 2 for this one, which is the first table you were looking for
table = tables[2]
Output:
print (table)
0 1 ... 6 7
0 Team Won ... Total Games Conference
1 Michigan 953 ... 1331 Big Ten
2 Ohio State 1 911 ... 1289 Big Ten
3 Notre Dame 2 897 ... 1263 Independent
4 Boise State 448 ... 618 Mountain West
5 Alabama 3 905 ... 1277 SEC
6 Oklahoma 896 ... 1274 Big 12
7 Texas 908 ... 1311 Big 12
8 USC 4 839 ... 1239 Pac-12
9 Nebraska 897 ... 1325 Big Ten
10 Penn State 887 ... 1319 Big Ten
11 Tennessee 838 ... 1281 SEC
12 Florida State 5 544 ... 818 ACC
13 Georgia 819 ... 1296 SEC
14 LSU 797 ... 1259 SEC
15 Appalachian State 617 ... 981 Sun Belt
16 Georgia Southern 387 ... 616 Sun Belt
17 Miami (FL) 630 ... 1009 ACC
18 Auburn 759 ... 1242 SEC
19 Florida 724 ... 1182 SEC
20 Old Dominion 76 ... 121 C-USA
21 Coastal Carolina 112 ... 180 Sun Belt
22 Washington 735 ... 1234 Pac-12
23 Clemson 744 ... 1248 ACC
24 Virginia Tech 743 ... 1262 ACC
25 Arizona State 614 ... 1032 Pac-12
26 Texas A&M 741 ... 1270 SEC
27 Michigan State 701 ... 1204 Big Ten
28 West Virginia 750 ... 1292 Big 12
29 Miami (OH) 690 ... 1195 MAC
.. ... ... ... ... ...
101 Memphis 482 ... 1026 The American
102 Kansas 582 ... 1271 Big 12
103 Wyoming 526 ... 1122 Mountain West
104 Louisiana 510 ... 1098 Sun Belt
105 Colorado State 520 ... 1124 Mountain West
106 Connecticut 508 ... 1107 The American
107 SMU 489 ... 1083 The American
108 Oregon State 530 ... 1173 Pac-12
109 UTSA 38 ... 82 C-USA
110 Kansas State 526 ... 1207 Big 12
111 New Mexico 483 ... 1103 Mountain West
112 Temple 468 ... 1094 The American
113 Iowa State 524 ... 1214 Big 12
114 Tulane 520 ... 1197 The American
115 Northwestern 535 ... 1240 Big Ten
116 UAB 126 ... 284 C-USA
117 Rice 470 ... 1108 C-USA
118 Eastern Michigan 453 ... 1089 MAC
119 Louisiana-Monroe 304 ... 727 Sun Belt
120 Florida Atlantic 87 ... 205 C-USA
121 Indiana 479 ... 1195 Big Ten
122 Buffalo 370 ... 922 MAC
123 Wake Forest 450 ... 1136 ACC
124 New Mexico State 430 ... 1090 Independent
125 UTEP 390 ... 1005 C-USA
126 UNLV11 228 ... 574 Mountain West
127 Kent State 341 ... 922 MAC
128 FIU 64 ... 191 C-USA
129 Charlotte 20 ... 65 C-USA
130 Georgia State 27 ... 94 Sun Belt
[131 rows x 8 columns]

SQL combine one column with another table and add new column

Sorry for the unclear title. But i want to make column editor_article which has name of editor. Only article with same value among id_article and parent_id have name of editor_article and editor_article has '0' value if id_section = 29 and if parent_id != 0. Editor_article got from editor column join with t_kolom.id_editor.
tbl_name:t_article
id_section id_article parent_id editor
29 441 0 2
33 1093 18 2
33 18 0 0
29 3144 0 8
30 3136 0 0
31 3130 0 0
31 3140 3130 22
31 3141 3130 335
30 3142 3136 546
tbl_name:t_kolom
id_editor name
1 john
2 gerrard
3 lukas
8 anthony
22 jimmy
335 eric
546 tyas
And the expected output:
id_section id_article parent_id editor editor_article
29 441 0 2 0
33 1093 18 2 0
33 18 0 0 gerrard
29 3144 0 8 0
30 3136 0 0 tyas
31 3130 0 0 jimmy,eric
31 3140 3130 22 0
31 3141 3130 335 0
30 3142 3136 546 0

Insert php multiple data into mysql

I have 1000+ customers. I require customer report.
Here debit = potato + onion + ginger. Credit is commission.Balance will be updated every time. It will be balance - debit and balance + credit alternatively.
Grocery data report is as: Data is filled through php form with mysql_fetch_array query. Here few customers are as sample. and few data fields.
id cus_id cus_name potato onion ginger debit credit balance
1 12 munna 10 25 28 63 0 37
2 16 anil 24 56 84 164 0 136
3 34 palash 17 47 51 115 0 85
4 45 dimpy 35 64 39 138 0 112
Table grocery before and after entering new data:
id cus_id cus_name potato onion ginger debit credit balance
1 12 munna 10 25 28 63 0 37
2 16 anil 24 56 84 164 0 136
3 34 palash 17 47 51 115 0 85
4 45 dimpy 35 64 39 138 0 112
5 12 munna 0 0 0 0 6 43
6 16 anil 0 0 0 0 16 152
7 34 palash 0 0 0 0 12 97
8 45 dimpy 0 0 0 0 14 126
My problem is :
I am unable to update balance column, cus_name wise and cus_id wise and insert all data into mysql database. Suggest me with mysql query.