Create view with delta for each column in SQL - mysql

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 ...

Related

I want to return the number of rows it's been since a value occurred

Alright, I'm starting over with a new bit of code that feels more efficient and shows that I can get some of what I want but not the final bit. Here is the edited code:
SELECT
uq1.UserID,
uq1.QuestionID,
r.LastAttempt,
r.Score,
r.DateSeen,
s.LastBad,
s.Score AS ScoreUnder,
s.DateSeen AS DateScoreUnder
FROM
users_questions uq1
LEFT JOIN (
SELECT
uq2.QuestionID,
uq2.UserID,
MAX(UsersQuestionsID) LastAttempt,
uq2.Score,
uq2.DateSeen
FROM
users_questions uq2
GROUP BY
uq2.UserID,
uq2.QuestionID
) AS r ON r.QuestionID = uq1.QuestionID AND r.UserID = uq1.UserID
LEFT JOIN (
SELECT
uq3.QuestionID,
uq3.UserID,
MAX(UsersQuestionsID) LastBad,
uq3.Score,
uq3.DateSeen
FROM
users_questions uq3
WHERE
uq3.Score <= 250
GROUP BY
uq3.UserID,
uq3.QuestionID
) AS s ON s.QuestionID = uq1.QuestionID AND s.UserID = uq1.UserID
GROUP BY
uq1.UserID,
uq1.QuestionID
It seems to be more efficient and produce some of what I want. The thing I need to find is the number of iterations between the record that it displays on the last LEFT JOIN and the end of the table for each question and user combo.
My test data is this:
UsersQuestionsID
Score
DateSeen
UserID
QuestionID
1
877
2022-06-25 19:51:18
14
98
2
765
2022-06-25 15:52:42
14
99
3
345
2022-06-25 15:54:22
14
99
4
754
2022-06-25 15:54:22
14
107
5
222
2022-06-25 16:11:40
13
73
6
525
2022-06-25 16:11:40
13
55
7
23
2022-06-25 16:11:40
13
130
8
888
2022-06-25 16:11:40
13
104
9
234
2022-07-01 12:41:44
14
94
10
564
2022-07-01 12:41:44
14
106
11
0
2022-07-01 13:59:37
14
99
12
267
2022-07-01 16:57:40
14
99
13
345
2022-07-02 15:17:03
13
99
14
49
2022-07-02 17:44:19
14
99
15
222
2022-07-02 18:10:17
14
99
16
49
2022-07-02 18:10:43
14
94
17
1000
2022-07-02 18:11:18
14
106
18
0
2022-07-06 14:18:51
14
94

MYSQL filter our same rows that are next to each other

I have this table, similar to the one below.
Table show player points:
s main player points
d sub main points;
date when it is calculated.
I want to be able to filter rows that are same as s and d staying next to each other. Date should be as the last last one that are the same.
For example, here we should skip ri - 13 as it is the same as ri -12. Also skip ri - 15,19,20,21,22,23 and so on. But rows 28, 29,30,31 should not be skipped and grouped.
I'm asking because GROUP BY for my case do not work. Any ideas?
Table example:
ri date s d
1 2016-05-23 4 355
2 2016-05-16 4 352
3 2016-05-09 4 349
4 2016-05-02 4 352
5 2016-04-25 4 358
6 2016-04-18 4 359
7 2016-04-11 4 200
8 2016-04-04 4 201
9 2016-03-21 4 198
10 2016-03-07 4 199
11 2016-02-29 4 201
12 2016-02-22 4 203
13 2016-02-15 4 203
14 2016-02-08 4 200
15 2016-02-01 4 200
16 2016-01-18 4 201
17 2016-01-11 4 198
18 2016-01-04 4 183
19 2015-12-28 4 183
20 2015-12-21 4 183
21 2015-12-14 4 183
22 2015-12-07 4 183
23 2015-11-30 4 183
24 2015-11-23 4 182
25 2015-11-16 4 149
26 2015-11-09 4 148
27 2015-11-02 4 145
28 2015-10-26 4 109
29 2015-10-19 4 110
30 2015-10-12 4 109
31 2015-10-05 4 110
32 2015-09-28 4 106
33 2015-09-21 4 108
34 2015-09-14 4 109
35 2015-08-31 5 108
36 2015-08-24 5 108
37 2015-08-17 5 136
38 2015-08-10 5 136
39 2015-08-03 4 123
40 2015-07-27 4 122
41 2015-07-20 4 125
42 2015-07-13 4 126
43 2015-06-29 4 130
44 2015-06-22 4 128
45 2015-06-15 4 126
46 2015-06-08 4 120
47 2015-05-25 9 120
48 2015-05-18 9 122
49 2015-05-11 9 121
50 2015-05-04 9 119
51 2015-04-27 9 122
52 2015-04-20 10 124
53 2015-04-13 9 173
54 2015-04-06 9 172
55 2015-03-23 8 174
56 2015-03-09 7 89
57 2015-03-02 7 89
58 2015-02-23 7 92
59 2015-02-16 7 96
60 2015-02-09 8 93
61 2015-02-02 9 88
62 2015-01-19 4 89
63 2015-01-12 4 89
64 2015-01-05 4 94
Coulb be you need a join ..
select a.*, b.*
from my_table as a
inner join my_table as b on a.ri != b.ri
where (a.d - b.d) = 0;
This can be done using not exists. This would select the first of many rows which have the same s and d.
select *
from tablename t1
where not exists (select 1 from tablename t2
where t1.ri = t2.ri+1 and t1.s = t2.s and t1.d = t2.d)

Percentage by Row Group

I have a matrix with rows grouped by Dept (Department). I am trying to get the actual hours / required hours percentage in a column for each row group, but I can only get the total %, not the % by group. Ex:
I should get this:
Total Employee Req Hrs Rep Hrs % Billable hrs % NonBill Hrs % Time Off %
Dept A Total 672 680 101 575 85 140 21 8 1
Emp1 168 170 101 150 89 50 29 0 0
Emp2 168 165 98 120 71 20 12 8 4
Emp3 168 175 104 155 92 20 12 0 0
Emp4 168 170 101 150 89 50 29 0 0
Dept B Total 420 428 102 365 87 80 19 4 .1
Emp5 168 170 101 150 89 50 29 0 0
Emp6 84 84 98 60 71 10 12 4 4
Emp7 168 175 104 155 92 20 12 0 0
G Total 1092 1108 101 940 86 190 17 12 1
But I get this:
Total Employee Req Hrs Rep Hrs % Billable hrs % NonBill Hrs % Time Off %
Dept A Total 1684 1675 101 1250 86 225 17 12 1
Emp1 168 170 101 150 89 50 29 0 0
Emp2 168 165 98 120 71 20 12 8 4
Emp3 168 175 104 155 92 20 12 0 0
Emp4 168 170 101 150 89 50 29 0 0
Dept B Total 1092 1108 101 1250 86 225 17 12 1
Emp5 168 170 101 150 89 50 29 0 0
Emp6 84 84 98 60 71 10 12 4 4
Emp7 168 175 104 155 92 20 12 0 0
G Total 1092 1108 101 940 86 190 17 12 1
The totals are correct but the % is wrong.
I have several Datasets because the report only runs the department you are in, except for the VPs who can see all departments.
I Insert the percentage columns into the matrix and have tried several expressions with no results including:
=Fields!ActHrs.Value/Fields!ReqHrs.Value
=Sum(Fields!ActHrs.Value, "Ut_Query")/Sum(Fields!ReqHrs.Value, "Ut_Query")
=Sum(Fields!ActHrs.Value, "Ut_Query","Dept")/Sum(Fields!ReqHrs.Value,
"Ut_Query","Dept")
=Sum(Fields!ActHrs.Value,"Dept", "Ut_Query")/Sum(Fields!ReqHrs.Value,
"Dept","Ut_Query")
Plus more I can't even remember.
I tried creating new groups, and even a new matrix.
There must be a simple way to get the percentage by group but I have not found an answer on any of the interned boards.
OK, I figured this out, but it doesn't make much sense. If I try:
=Textbox29/TextBox28 I get error messages about undefined variables.
If I go the the textbox properties and rename the textboxes to Act and Req and use:
=Act/Req I get the right answer.

How to get row with the maximum value by grouping

I have a table where population number is given for each day at every hour. How can I get the row with max population number? Here is an example of the table
date hour population
2015-07-11 10 205
2015-07-11 11 390
2015-07-11 12 579
2015-07-11 13 679
2015-07-11 14 699
2015-07-11 15 890
2015-07-11 16 816
2015-07-11 17 970
2015-07-11 18 835
2015-07-11 19 827
2015-07-11 20 753
2015-07-11 21 638
2015-07-11 22 327
2015-07-12 9 33
2015-07-12 10 151
2015-07-12 11 227
2015-07-12 12 419
2015-07-12 13 561
2015-07-12 14 683
2015-07-12 15 799
2015-07-12 16 830
2015-07-12 17 876
2015-07-12 18 844
2015-07-12 19 819
2015-07-12 20 626
2015-07-12 21 526
2015-07-12 22 235
Try using MAX() to get maximum value of a column per group :
SELECT date, MAX(population)
FROM foo
GROUP BY date
EDIT :
If you want to have the hour that corresponds to your max population value, you can go with :
SELECT foo.*
FROM foo
INNER JOIN
(SELECT date, MAX(population) as MaxPop
FROM foo
GROUP BY date) max
ON foo.date = max.date
AND foo.population = max.MaxPop
Hope it helps.

Selecting duplicate entries from a table and getting all the fields from the duplicate rows

I have a table like this.
id day1 day2 day3
1 411 523 223
2 413 554 245
3 417 511 209
4 420 515 232
5 422 522 212
6 483 567 212
7 456 512 256
8 433 578 209
9 438 532 234
10 418 555 223
11 460 510 263
12 453 509 245
13 441 524 233
14 430 543 261
15 456 582 222
16 444 524 241
17 478 511 211
18 421 583 222
I want to select all the IDs that have duplicate values in day2.
I'm doing
select day2,count(*) from resultater group by day having count(*)>1;
Is it possible to list all the IDs within the groups?
select day2,count(*), group_concat(id)
from resultater
group by day
having count(*)>1;
should do the trick.