Joining a table with itself vs using Group By MySQL - mysql

Here is a simplified version of my table:
It contains the columns
employee_id column2 column3 x y
and they are all NUMERIC
I am having trouble writing a complicated MySQL query. I want to get all the employeeid and column3 values for all employees that have the same value in column2 as another employee and have DIFFERENT values for x and y PAIR from every other employee. For example, if the following 4 rows were in table:
2 100 123.456 5 7
1 234 123.456 5 7
3 100 456.789 5 10
4 100 123.456 5 7
The rows 2 100 123.456 5 7 and 3 100 456.789 5 10 should be obtained because they have different employee ids (2 vs 3), the same value for column2 (100 and 100), and different x, y pair: (Employee 2 has x = 5 and y = 7, which is distinct from x = 5 and y = 10).
How can I compare the documents of table with other documents within itself?

SELECT e1.*
FROM employee e1
JOIN employee e2
ON e1.employee_id <> e2.employee_id
AND e1.column2 = e2.column2
AND e1.X <> e2.X
AND e1.Y <> e2.Y
But I guess your last condition should be
AND (e1.X <> e2.X OR e1.Y <> e2.Y)

Related

How to select 1:n related records which are minimum 2 per type in MySQL

I have the following database tables.
my_left_table
left_id name
1 A
2 B
3 C
my_right_tabe
right_id thing left_id_fk status
1 D 1 new
2 E 1 new
3 F 2 old
4 G 3 old
5 H 3 new
6 I 3 new
7 J 1 old
8 K 2 old
9 L 2 new
10 M 3 old
11 N 3 old
12 O 1 new
My desired result is as follow.
my_left_table
left_id name
3 C
How do I select the left records which its right records have AT LEAST 2 status is new AND 2 status is old. For example, left_id 1 is not the target because three of its right records have the status new but only one record has the status old.
So far I have is.
SELECT *, COUNT(my_right_tabe.left_id_fk) AS count_left_id_fk
FROM my_left_table
INNER JOIN my_right_tabe
ON my_left_table.id = my_right_tabe.left_id_fk
GROUP BY my_right_tabe.left_id_fk
Use the HAVING clause in MySQL
Like the following
SELECT my_left_table.left_id, my_left_table.name
FROM my_left_table
INNER JOIN my_right_tabe
ON my_left_table.left_id = my_right_tabe.left_id_fk
GROUP BY my_right_tabe.left_id_fk
HAVING SUM(my_right_tabe.status="new") >= 2 AND
SUM(my_right_tabe.status="old") >= 2
You can achieve desired results by first grouping the values and then check its total. If its >= 2 pull that record.
Here is the query
SELECT z.*
FROM
(
SELECT a.left_id, name, status, IF(COUNT(*) >=2, 1, 0) AS status_calc
FROM my_left_table a JOIN my_right_table b
ON a.left_id = b.left_id_fk
GROUP BY left_id, status
) z
GROUP BY z.left_id
HAVING SUM(status_calc) = 2;
Working Demo

Is the query acting like it should? [duplicate]

Here is a simplified version of my table:
It contains the columns
employee_id column2 column3 x y
and they are all NUMERIC
I am having trouble writing a complicated MySQL query. I want to get all the employeeid and column3 values for all employees that have the same value in column2 as another employee and have DIFFERENT values for x and y PAIR from every other employee. For example, if the following 4 rows were in table:
2 100 123.456 5 7
1 234 123.456 5 7
3 100 456.789 5 10
4 100 123.456 5 7
The rows 2 100 123.456 5 7 and 3 100 456.789 5 10 should be obtained because they have different employee ids (2 vs 3), the same value for column2 (100 and 100), and different x, y pair: (Employee 2 has x = 5 and y = 7, which is distinct from x = 5 and y = 10).
How can I compare the documents of table with other documents within itself?
SELECT e1.*
FROM employee e1
JOIN employee e2
ON e1.employee_id <> e2.employee_id
AND e1.column2 = e2.column2
AND e1.X <> e2.X
AND e1.Y <> e2.Y
But I guess your last condition should be
AND (e1.X <> e2.X OR e1.Y <> e2.Y)

select columns from one table using count function of another table in sql

run_software
runID Release
1 X
2 X
3 Y
4 Z
5 Y
6 X
7 Y
8 Z
9 X
10 Z
testcase
testID runID Result
T_1 1 PASS
T_2 1 FAIL
T_3 1 PASS
T_4 2 PASS
T_5 2 FAIL
T_6 3 PASS
T_7 4 FAIL
T_8 3 PASS
T_9 3 FAIL
T_10 5 PASS
T_11 5 FAIL
T_12 3 PASS
1) From run_software table we can understand X software run on runID's 1, 2,6,9
2) Take runID - 1 and come to testcase table.
Here we have 7 testID's with runID 1.
From these 7 testID's we need to measure the TC count and percentage of PASS/FAIL using group by Result and runID.
AIM: Ultimate aim is to find the latest 3 Release's and its runID's with PASS percentage by considering max testcase count.
Eg. if 'X' release executed on runID's 1, 2, 3, 4 with each 10, 12, 9, 21 testcases respectively, we should consider runID 4 for release 'X' to measure the 'PASS %'
Desired OutPut:
considering PASS% is > 60
Release runID Result PASS %
X 1 PASS 66.66
Y 3 PASS 75
Z 4 FAIL 0
To be understanding
Release 'X' has runID's - 1, 2 , 6, 9 with 3, 2, 0 , 0 TestID's respecively
Hence, X finalized runID '1' with 66.66 as PASS% (2 PASS & 1 FAIL)
Your question is not very clear actually, but it should be something like that I guess:
SELECT r.Release, r.runID, (CASE WHEN subq.PassPerc >= 60 THEN 'PASS' ELSE 'FAIL' END) AS Result, subq.PASSPerc
FROM run_software AS r
INNER JOIN (SELECT p.runID, (100 * p.passed / t.total) AS PASSPerc
FROM (SELECT runID, COUNT(*) AS passed FROM testcase WHERE Result = 'PASS' GROUP BY runID) AS p
INNER JOIN (SELECT runID, COUNT(*) AS total FROM testcase GROUP BY runID) AS t ON t.runID = p.runID
GROUP BY p.runID, p.passed, t.total) AS subq
ON subq.runID = r.runID
GROUP BY r.Release, r.runID, subq.PASSPerc

How to select next 4 consecutive rows from my resultant row?

I want to know, how can i select next 4 consecutive rows along with the resultant row in MySQL?
To explain:-
i have following rows in table abc
name | amount
a - 1000
b - 2000
c - 3000
d - 4000
e - 5000
f - 6000
Now,
if my selection criteria is name = a , then resultant rows returned should be
a - 1000
b - 2000
c - 3000
d - 4000
e - 5000
In other words i want a query which will return result of query - "select * from abc where name = 'a'" plus next 4 consecutive rows to that row?
SELECT abc.*
FROM abc JOIN (SELECT amount FROM abc WHERE name = 'a') t
WHERE abc.amount >= t.amount
ORDER BY abc.amount
LIMIT 5
try
for mssql: select top 5 * from abc where name > 'a' order by name
for mysql: select * from abc where name > 'a' order by name limit 5

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