SQL: simplify and, or query on multiple columns - mysql

I have a table named employe with following content
id A B C D E F G
1 8 4 6 3 2 5 2
1 7 3 1 2 1 3 7
3 9 2 3 3 2 6 1
4 6 1 2 4 5 5 7
4 6 4 6 3 2 5 2
I do a query like this
employe.where(
A > 7 &
(
(C<3 & D<3 & E<3 & F<3 & G<3)
|(B<3 & D<3 & E<3 & F<3 & G<3)
|(B<3 & C<3 & E<3 & F<3 & G<3)
|(B<3 & C<3 & D<3 & F<3 & G<3)
|(B<3 & C<3 & D<3 & E<3 & G<3)
|(B<3 & C<3 & D<3 & E<3 & F<3)
)
)
Is there any way to simplify the above query? Because I have more than 20 columns in my table and I have do the above query for all columns. It looks ugly and same code in every line. Even if I could something like this would look nice
q1 = [C, D, E, F, G]
q2 = [B, D, E, F, G]
q3 = [B, C, E, F, G]
q4 = [B, C, D, F, G]
q5 = [B, C, D, E, G]
q6 = [B, C, D, E, F]
employe.where(
A > 7 &
(
q1<3 | q2<3 | q4<4 | q5<3 | q6<3
)
)

I don't have MySQL to check, but is it possible to do something like:
SELECT * FROM #employee e
INNER JOIN (SELECT id, A,
CASE WHEN a < 3 THEN 0 ELSE 1 END +
CASE WHEN b < 3 THEN 0 ELSE 1 END +
CASE WHEN c < 3 THEN 0 ELSE 1 END +
CASE WHEN d < 3 THEN 0 ELSE 1 END +
CASE WHEN e < 3 THEN 0 ELSE 1 END +
CASE WHEN f < 3 THEN 0 ELSE 1 END +
CASE WHEN g < 3 THEN 0 ELSE 1 END as cnt from #employee) c
on e.id = c.id
WHERE e.A > 7 AND c.cnt < 3
If I understand you correctly you are trying to find instances of one column where it is greater than 7, where at least four of the other five columns are less than three. My example achieves this (for all columns) by a simple trick of logic. If you count all the instances of A-G that are not less than 3 and check that for any column this is less than 3 where that column is > 7, then you know the others are maximally one not less than 3, since the column itself is one of the 2 that are not less than 3.
Expanding the example for all columns you then get
SELECT * FROM #employee e
INNER JOIN (SELECT id,
CASE WHEN a < 3 THEN 0 ELSE 1 END +
CASE WHEN b < 3 THEN 0 ELSE 1 END +
CASE WHEN c < 3 THEN 0 ELSE 1 END +
CASE WHEN d < 3 THEN 0 ELSE 1 END +
CASE WHEN e < 3 THEN 0 ELSE 1 END +
CASE WHEN f < 3 THEN 0 ELSE 1 END +
CASE WHEN g < 3 THEN 0 ELSE 1 END as cnt from #employee) c
on e.id = c.id
WHERE (e.A > 7 OR e.B > 7 OR e.C > 7 OR e.D > 7 OR e.F > 7 or e.G > 7)
AND c.cnt < 3

If all values (columns) from the list must be less than 3, then the greatest value in this list must be also less than 3, so you can use greatest function, something like:
Where
Greatest( c, d, e, f, g ) < 3
Or
Greatest( b, d, e, f, g ) < 3
Or
Greatest( b, c, d, e, f ) < 3
Or
.....

Related

LOAD DATA INFILE MySQL(MariaDB)

I am trying to load a .csv file into MariaDB but I am struggling with the query.
Here is how the.csv file is formatted:
USER DATE TIME TESTRESULT ERRORCODE
Esa_Test 16.5.2022 12:36:59 Fail 1(MinMaxError)
Esa_Test 16.5.2022 12:38:02 Fail 1(MinMaxError)
Esa_Test 16.5.2022 12:55:40 Fail 1(MinMaxError)
Esa_Test 17.5.2022 16:15:00 Fail 1(MinMaxError)
DPHYD_Ate 18.5.2022 9:50:11 OK 0(NoError)
When I use this query:
LOAD DATA LOW_PRIORITY LOCAL INFILE 'C:\\xampp\\mysql\\data\\test\\log2.csv' IGNORE INTO TABLE `test`.`testova2` IGNORE 4 LINES (`USER`, `DATE`, `TIME`, `TESTRESULT`, `ERRORCODE`);
The data is loaded successfully but with spaces like this:
USER;DATE;TIME;TESTRESULT;ERRORCODE
E s a _ T e s t ; 1 6 . 5 . 2 0 2 2 ; 1 2 : 3 6 : 5 9 ; F a i l ; 1 ( M i n M a x E r r o r )
E s a _ T e s t ; 1 6 . 5 . 2 0 2 2 ; 1 2 : 3 8 : 0 2 ; F a i l ; 1 ( M i n M a x E r r o r )
E s a _ T e s t ; 1 6 . 5 . 2 0 2 2 ; 1 2 : 5 5 : 4 0 ; F a i l ; 1 ( M i n M a x E r r o r )
E s a _ T e s t ; 1 7 . 5 . 2 0 2 2 ; 1 6 : 1 5 : 0 0 ; F a i l ; 1 ( M i n M a x E r r o r )
D P H Y D _ A t e ; 1 8 . 5 . 2 0 2 2 ; 9 : 5 0 : 1 1 ; O K ; 0 ( N o E r r o r )
I tried to define some "limits" via FIELDS TERMINATED BY '\t' LINES TERMINATED BY '|' in the query but not working. The original file is with encoding UTF16LE according to notepad++
Please help me to build the proper query for my case in order to insert the data correctly..
If someone looking at this:
I manage to find a solution.
The problem was the encoding.
The solution was to make a short Python script to change the encoding before the upload in MySQL the code is:
import codecs
with codecs.open("input.csv","r",encoding="utf_16") as fin:
with codecs.open("output.csv","w",encoding="utf_8") as fout:
fout.write(fin.read())
I automated this and now everything is working.

MySQL: count appearance of sequences

i have table with:
id
mode
1
B
2
B
3
A
4
A
5
A
6
A
7
B
8
B
9
C
10
C
11
C
12
B
13
A
14
A
15
A
16
B
17
C
18
B
19
C
20
B
21
B
i would like to count following sequences:
"start": xA -> xB -> xC
"stop": xC -> xB ->xA
so that final result for this table would be:
START = 2 (ID: 3-11, 13-17)
STOP = 1 (ID: 9-15)
Point is that i need to count only right mode changes, no matter how many times mode is recorded.
Can anybody help? (tnx!)
ok, i got this solved with:
select
sum(case when prev= "A" and nxt="C" then 1 else 0 end) as start,
sum(case when prev= "C" and nxt="A" then 1 else 0 end) as stop
from (
select id, prev, mode, nxt
from (
select
id,
LAG(mode)OVER (
partition by 1
order by id
) prev,
mode,
LEAD(mode)OVER (
partition by 1
order by id
) nxt
from prva
order by id
) sub
where mode <> nxt and mode="B"
) data

Select range between two columns

I need query to select value between to columns but one columns have same value
For example :
12 1
12 2
12 3 ------
12 4
12 5
13 1
13 2
13 3
13 4 ------
13 5
i need range to 12 | 3 to 13 | 4
select * from table1 where
((a >= '12' and B >= 2) and (a <= '13' and b <=5))
so the 13 1 is missing :( i have no idea for this query
I think the logic is:
where (a > 12 and a < 13) or
(a = 12 and b >= 3) or
(a = 13 and b <= 4)
Of course, you can also express this more simply using tuples:
where (a, b) >= (12, 3) and
(a, b) <= (13, 4)
this will surely work:
select * from Table1 where
(a=12 and b>=3) or(a=13 and b<=4);
check this up :http://sqlfiddle.com/#!9/7c94ab/6
I think that works:
select * from Test
where (a = 12 and b > 2) or (a = 13 and b < 5);

MySQL adding value from previous row

I have a table with a single column. The column is like this:
1
2
3
4
5
...
I want to create a query that will display another column that will have the previous value added to it. So:
1 1 ( 0 + 1 )
2 3 ( 1 + 2 )
3 5 ( 2 + 3 )
4 7 ( 3 + 4 )
5 9 ( 4 + 5 )
9 14 (5 + 9)
45 54 ( 9 + 45)
How would I construct a query to accomplish that?
Essentially, I just want the difference between ROW[X], and ROW[X-1].
SELECT a.val, (#runtot := a.val + #runtot) AS rt, ( #runtot := a.val ) ne
FROM Table1 a,(SELECT #runtot:=0) c
This seems to be working. I tried reinit the variable at each stage.
Try it out.
SQLFiddle Demo
TRY
SELECT a.int, (#runtot := a.int + a.int - 1) AS rt
FROM test a,(SELECT #runtot:=0) c
ie
1 1 ( 0 + 1 )
2 3 ( 1 + 2 )
3 5 ( 2 + 3 )
4 7 ( 3 + 4 )
5 9 ( 4 + 5 )
this will result the output you shown

Improvement of VBA code in Access to do something like a pivot table

I have a table of data in MS Access 2007. There are 6 fields per record, thousands of records. I want to make a sort of pivot table like object. That is, if any two rows happens to be the same in the first 4 fields, then they will end up grouped together into one row. The column headers in this pivot table will be the values from the 5th field, and the value in the pivot table will be the 6th field, a dollar amount. Think of the 5th field as letters A, B, C, D, E, F, G. So, the table I start with might have a row with A in the 5th field and $3.48 in the 6th field. Another row may match in the first 4 fields, have B in the 5th field and $8.59 in the 6th field. Another may match in the first 4 fields, have E in the 5th field and $45.20 in the 6th field. I want all these rows to be turned into one row (in a new table) that starts with the first 4 fields where they match, then lists $3.48, $8.59, $0.00, $0.00, $45.20, $0.00, $0.00, corresponding to column headers A, B, C, D, E, F, G (since no records contained C, D, F, G, their corresponding values are $0.00), and then ends with one more field that totals up the money in that row.
Currently, I have some VBA code that does this, written by someone else a few years ago. It is extremely slow and I am hoping for a better way. I asked a previous question (but not very clearly so I was advised to create a new question), where I was asking if there was a better way to do this in VBA. My question asked about reading and writing large amounts of data all at once in Access through VBA, which I know is a good practice in Excel. That is, I was hoping to take my original table and just assign the entire thing to an array all at once (as in Excel, instead of cell by cell), then work with that array in VBA and create some new array and then write that entire array all at once to a new table (instead of record by record, field by field). From the answers in that question, it seems like that is not really a possibility in Access, but my best bet might be to use some sort of query. I tried the Query Wizard and found the Cross Tab query which is close to what I describe above. But, there appears to be a max of 3 fields used in the Row Heading, whereas here I have 4. And, instead of putting $0.00 when a value is not specified (like C, D, F, G in my example above), it just leaves a blank.
Update (in response to Remou's comment to give sample data): Here is some sample data.
ID a b c d e f
7 1 2 3 5 A 5
8 1 2 3 5 B 10
9 1 2 3 5 C 15
10 1 2 3 5 D 20
11 1 2 3 5 E 25
12 1 2 4 4 A 16
13 1 2 4 4 B 26
14 1 3 3 7 D 11
15 1 3 3 7 B 11
The result should be:
a b c d an bn cn dn en Total
1 2 3 5 5 10 15 20 25 75
1 2 4 4 16 26 0 0 0 42
1 3 3 7 0 11 0 11 0 22
But, when I copy and paste the SQL given by Remou, the only output I get is
a b c d an bn cn dn en
1 2 3 5 5 10 15 20 25
This is, I think, what you want, but it would be better to consider database design, because this is a spreadsheet-like solution.
SELECT t0.a,
t0.b,
t0.c,
t0.d,
Iif(Isnull([a1]), 0, [a1]) AS an,
Iif(Isnull([b1]), 0, [b1]) AS bn,
Iif(Isnull([c1]), 0, [c1]) AS cn,
Iif(Isnull([d1]), 0, [d1]) AS dn,
Iif(Isnull([e1]), 0, [e1]) AS en
FROM (((((SELECT DISTINCT t.a,
t.b,
t.c,
t.d
FROM table3 t) AS t0
LEFT JOIN (SELECT t.a,
t.b,
t.c,
t.d,
t.f AS a1
FROM table3 t
WHERE t.e = "A") AS a0
ON ( t0.d = a0.d )
AND ( t0.c = a0.c )
AND ( t0.b = a0.b )
AND ( t0.a = a0.a ))
LEFT JOIN (SELECT t.a,
t.b,
t.c,
t.d,
t.f AS b1
FROM table3 t
WHERE t.e = "B") AS b0
ON ( t0.d = b0.d )
AND ( t0.c = b0.c )
AND ( t0.b = b0.b )
AND ( t0.a = b0.a ))
LEFT JOIN (SELECT t.a,
t.b,
t.c,
t.d,
t.f AS c1
FROM table3 t
WHERE t.e = "C") AS c0
ON ( t0.d = c0.d )
AND ( t0.c = c0.c )
AND ( t0.b = c0.b )
AND ( t0.a = c0.a ))
LEFT JOIN (SELECT t.a,
t.b,
t.c,
t.d,
t.f AS d1
FROM table3 t
WHERE t.e = "D") AS d0
ON ( t0.d = d0.d )
AND ( t0.c = d0.c )
AND ( t0.b = d0.b )
AND ( t0.a = d0.a ))
LEFT JOIN (SELECT t.a,
t.b,
t.c,
t.d,
t.f AS e1
FROM table3 t
WHERE t.e = "E") AS e0
ON ( t0.d = e0.d )
AND ( t0.c = e0.c )
AND ( t0.b = e0.b )
AND ( t0.a = e0.a );
Table3
ID a b c d e f
1 1 2 3 4 a €10.00
2 1 2 3 4 b €10.00
3 1 2 3 4 c €10.00
4 1 2 3 4 d €10.00
5 1 2 3 4 e €10.00
6 1 2 3 5 a €10.00
7 1 2 3 5 b
8 1 2 3 5 c €10.00
9 1 2 3 5 d €10.00
10 1 2 3 5 e €10.00
Result
There are two rows, because there are only two different sets in the first four columns.
a b c d an bn cn dn en
1 2 3 4 €10.00 €10.00 €10.00 €10.00 €10.00
1 2 3 5 €10.00 €0.00 €10.00 €10.00 €10.00
The way the sql above is supposed to work, is that it selects each of the four definition columns and the currency column from the table where the sort column has a particular sort letter and labels the currency column with the sort letter, each of these sub queries are then assembled, however, you can take a sub query and look at the results. The last one is the part between the parentheses:
INNER JOIN (SELECT t.a,
t.b,
t.c,
t.d,
t.f AS e1
FROM table3 t
WHERE t.e = "E") AS e0