Select rows in SQL that follow certain conditions - mysql

I want to select rows that follow the following conditions in SQL.
Table Name:
Codes_and_Numbers
I have the following dataset:
Code_Name Num_1 Num_2
A 10 12
A 10 10
A 10 10
B 17 17
B 17 17
B 17 17
B 17 17
C 21 25
C 21 23
I want to select the rows where Num_1 and Num_2 are not equal, however if on another row with the same Code_Name, Num_1 and Num_2 are equal then I don't want to select any of the rows under that Code_Name.
In the dataset above, this would mean only the 2 rows for C would be selected. As A has two rows with equal Num_1 and Num_2 and all of B rows are equal.

You can use NOT EXISTS:
SELECT Code_Name, Num_1, Num_2
FROM Codes_and_Numbers AS t1
WHERE t1.Num_1 <> t1.Num_2 AND
NOT EXISTS (SELECT 1
FROM Codes_and_Numbers AS t2
WHERE t1.Code_Name = t2.Code_Name AND
t2.Num_1 = t2.num_2)

Related

how to create a table like this by mysql

I have a table like this:
ID Payment year
A 10 1
A 15 2
A 12 3
B 11 2
B 15 4
C 25 1
C 17 3
I'm looking for a query that returns row for each ID for the its last year. The year column is ordered increasing for each ID.
I need a result like this:
ID Payment year
A 12 3
B 15 4
C 17 3
I have this query so far:
select ID, Payment, Year from payment_table
where year = (select max(year) from ?????????);
I don't know what shall I write instead of ????????
It would be appreciated If anybody gives me some idea.
Use subquery :
select t.*
from table t
where year = (select max(t1.year) from table t1 where t1.id = t.id);

INFORMATION_SCHEMA columns in where clause query in MySql?

I have two requirements
1) Use table columns names (INFORMATION_SCHEMA.COLUMNS) NOT DATA in where clause query.
2) Transform the final output columns
Table-A:
id column-A column-B column-C ... column-N
1 11 12 13 20
2 21 22 23 20
Table-B:
id name label
1 column A CA
2 column B CB
3 column C CC
RAW MySQL query
select a.* from table A a inner join table B b
where b.name IN (SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='Table-A' AND
COLUMN_NAME NOT IN ('id');
ALSO, I want to transform final output of Table-A and that should be:
id CA CB CC
1 11 12 13
2 21 22 23
I don't want column-A column-B column-C in my final query result. Can someone please help me in this?

Select only rows with matching pair

I have a MySQL table as below
ID SHEET_NUMBER DAYS RESULT
1 55201 9 10
2 55209 28 25.5
3 55209 28 27.9
4 38558 7 12
5 38552 5 19
6 38559 5 5
I want to select only rows with firstly matching sheet numbers & only if there if there is a matching pair of 28 days specimens
so if there is only one 28 day it will select nothing but if there is at least 2x 28 day it will get both rows
I'm totally lost, i know i should be using group by.. but i'm unsure of its use.
thankyou
Can you try the following query:
SELECT *
FROM test
WHERE sheet_number IN (
SELECT sheet_number
FROM test
WHERE days = 28
GROUP BY sheet_number
HAVING COUNT(*) >= 2
);
Here's the SQL Fiddle.
First, write a query that finds sheet_number with two or more rows with days value of 28.
SELECT d.sheet_number
FROM my_table_below d
WHERE d.days = 28
GROUP BY d.sheet_number
HAVING COUNT(1) > 1
With that query, we can use that as an inline view, and join back to the original table to find the matching rows:
SELECT t.*
FROM ( SELECT d.sheet_number
FROM my_table_below d
WHERE d.days = 28
GROUP BY d.sheet_number
HAVING COUNT(1) > 1
) m
JOIN my_table_below t
ON t.sheet_number = m.sheet_number
AND t.days = 28
ORDER BY t.sheet_number, t.id
Omit the condition t.days = 28 on the outer query, if the specification is to return all of the rows for the sheet_number, not just the rows with days=28. (The specification is a bit unclear.)

Mysql query to return result from mapping table in order of occurence

I have one table A_B mapping. Now I want those A which are associated with single B only.
A B
12 16
12 22
12 23
12 26
23 16
24 26
Suppose if I will search for A whose are associated with B = 16, I will get 12 and 23 A.
But I want only 23 as it is only associated with B=16.
Second choice can be first 23 then 12 will occur.
So first priority will be to single association items, then multiple associations will occur.
1.
select A
from test4 T1
where B=16
and not exists(select 1 from test4 T2
where T2.A=T1.A and T2.B<>T1.B)
2.
select A
from test4 T1
where B=16
order by exists(select 1 from test4 T2
where T2.A=T1.A and T2.B<>T1.B)
Did you mean this ?
Select A FROM A_B where B IN (Select B FROM A_B where A=12) and A<>'12'

how can you eliminate permutations between columns in MySQL

i have table columns one (idprocess) point to columns two (idporcess1) and point to columns tree (idprocess2).
id idprocess idporcess1 idprocess2
1 15 16 17 <== A
2 15 16 19 <== B
3 15 20 23
4 14 16 17
6 16 15 80 <== C
7 17 15 49 <== D
8 23 16 20 <== E
I need a SQL query that returns this: row c and row D, so with number idprocess(16) and idprocess(17 )
because row c : idprocess(16) references again ipdprocess1(15)
because row c : idprocess(17 ) references agin ipdprocess1(15)
please help
i want only to eleminate circular referencial in tree
If you are happy to find rows where the first two columns are permutated, this will do the job:
SELECT *
FROM my_tbl t
WHERE EXISTS (SELECT 1 FROM my_tbl t1 WHERE t1.idprocess = t.idprocess1 AND t1.idprocess1 = t.idprocess)
ORDER BY t.id;
Alternative interpretation:
If you want all rows where idprocess1 has been listed in idprocess before (before = smaller id), then you can:
SELECT *
FROM my_tbl t
WHERE EXISTS (SELECT 1 FROM my_tbl t1 WHERE t1.id < t.id AND t1.idprocess = t.idprocess1)
ORDER BY t.id;
You wouldn't call that "permutation", though.
The question is a bit ambiguous but I tried to understand it on my own and prepared the following query:
SELECT *
FROM TEMP
where C2 IN ( Select C2 FROM TEMP group by C2 having count(C2) > 1 )
OR C3 IN ( Select C3 FROM TEMP group by C3 having count(C3) > 1 )