MySQL distinct values query - mysql

I need help with a relatively simple query. For a table:
A | B | C
----------
2 1 6
2 2 5
3 3 4
4 4 3
5 5 2
6 6 1
I need to have an output like so:
A | B | C
----------
2 1 6
3 3 4
4 4 3
5 5 2
6 6 1
So that each value in A is distinct, but I also get the corresponding values in B and C. I know "select distinct(A) from table" but that only returns the values 2,3,4,5,6 and I need the values in columns B and C, too. Please help. I have a deadline fast approaching. This question is stupid and trivial, but one must walk before they can run. Thanks so much.

Try this:
SELECT T1.A, T1.B, MIN(T1.C) AS C
FROM yourtable T1
JOIN (
SELECT A, MIN(B) AS B
FROM yourtable
GROUP BY A
) T2
ON T1.A = T2.A AND T1.B = T2.B
GROUP BY T1.A, T1.B

SELECT DISTINCT(A), B, C
FROM table
Is there a specific logic behind which distinct A rows you want to select when considering columns B and C?

Related

SQL Union and Merge Subset Tables

T1
SCHOOL STUDENT TEACHER
1 1 A
1 2 B
1 3 B
1 4 B
T2
SCHOOL STUDENT TEACHER
2 7 A
2 6 A
2 8 B
2 9 B
T3
SCHOOL TEACHER ID
1 A FOX
1 B CAT
2 A DOG
2 B MOUSE
T4
SCHOOL STUDENT TEACHER ID
1 1 A FOX
1 2 B CAT
1 3 B CAT
1 4 B CAT
2 7 A DOG
2 6 A DOG
2 8 B MOUSE
2 9 B MOUSE
I have T1 T2 T3 where I wish to UNION T1 and T2 and afterwards JOIN T3 such as:
SELECT * FROM T1
UNION
(SELECT * FROM T2)
JOIN
(SELECT SCHOOL, TEACHER, ID
WHERE SCHOOL != 10
FROM T3)
BUT I receive error "UNEXPECTED JOIN"
JOIN is a part of a SELECT statement while UNION are not.
So, you need to make UNION as a part of a SELECT statement
select * from (
SELECT * FROM T1
UNION
SELECT * FROM T2) q1
JOIN
(SELECT SCHOOL, TEACHER, ID
FROM T3
WHERE SCHOOL != 10) q2
on /* here goes JOIN condition you need, like q1.school = q2.school*/
Please note:
there is another syntax error in your example: FROM goes always after SELECT and before WHERE
UNION will append result and eliminate duplicate rows which can run slower than UNION ALL. The latter will not check whether there are duplicate rows. So, if you're sure there won't be any duplicates in the result or if it's irrelevant whether you get duplicates or not, you may use UNION ALL

Identify missing conversions with an SQL query

I need to find a way to find what conversions are missing.
I have three tables.
Table 1: type, which are the different types.
id
name
1
typeA
2
typeB
3
typeC
Table 2: section, which are the different sections.
id
name
1
section1
2
section2
3
section3
4
section4
Table 3: conversions, which contains all the combinations to go from one type to another for each of the sections.
id
section_id
type_convert_from
type_convert_to
1
1
1
2
2
2
1
2
3
3
1
2
4
4
1
2
5
1
1
3
6
2
1
3
7
3
1
3
8
4
1
3
9
1
2
1
10
2
2
1
11
3
2
1
12
4
2
1
For example some are missing from the table above, how can I identify them with a SQL query? Thanks!
Try this. The cross join of table type with itself generates all possible combinations of type id's. I've excluded combinations in the cross join where id_from = id_to (ie you're not interested in conversions from a type to itself)
select * from conversions C
right join (
select T1.id as id_from, T2.id as id_to
from type T1 cross join type T2
where T1.id <> T2.id
) X on X.id_from = C.type_convert_from and X.id_to = C.type_convert_to
where C.type_convert_from is null
If you want to check missing type conversions by section, extend the cross join by adding the section table to include section.id as follows. It will list missing type conversions within each section.
select X.section_id, X.id_from, X.id_to from conversions C
right join (
select S.id as section_id, T1.id as id_from, T2.id as id_to
from types T1 cross join types T2 cross join section S
where T1.id <> T2.id
) X
on X.id_from = C.type_convert_from and X.id_to = C.type_convert_to
and C.section_id = X.section_id
where C.type_convert_from is null

SQL to 1 hot vector

Need Help On an SQL problem
I have a table like table1
id labels.1 labels.2 labels.3 labels.4 .... table1
1 a b c null
2 b d a null
I want my tuples to be 1-hot vector like
table2
id a b c d ...
1 1 1 1 0
2 1 1 0 1
My solution:
Covert table1 to table3, and with t3, I can use if x IN label to decide whether a column should be 1 or 0, but I was stuck at the converting, I think I should use unpivot, but I don't know how, any ideas, or any smarter solutions
table3
id label
1 a
1 b
1 c
2 a
2 b
2 d

Select with double duplicates

Here is an example
a b
--------
1 10
1 10
2 20
2 20
3 20
3 20
4 NULL
5 NULL
I want this in a mySQL query :
a b
------------
1 10
2 or 3 20
4 NULL
5 NULL
In other words : the set of elements where (there is no duplicates on a and (there is no duplicates on b or b is NULL)).
I try the
SELECT DISTINCT(a), b, but I have 2 rows with b = 20
SELECT a, DISTINCT(b), but I have duplicates on column a and the 2 NULL values are merged.
GROUP BY a or GROUP BY b, equal as the 2 previous queries.
Does anyone have an idea for it ?
SELECT MIN(a) a, b
FROM table1
WHERE b IS NOT NULL
GROUP BY b
UNION
SELECT a, b
FROM table1
WHERE b IS NULL
SQLFiddle Demo
give this a try,
SELECT MIN(a) a, b
FROM table1
GROUP BY COALESCE(b, RAND())
SQLFiddle Demo
I guess this would be faster
SELECT MIN(a) a, b
FROM table1
GROUP by IF( b is null, a, b);
SQLFIDDLE

Fetch DISTINCT records with JOIN of two tables

I have two tables:
builders
b_id fk_c_id
1 1
2 1
3 1
4 1
5 1
6 2
7 2
subbuilders
fk_b_id sb_id
1 2
1 3
1 4
2 5
6 7
and i want Distinct b_id which are not exist in subbuilders table and must have same fk_c_id
I create:
SELECT DISTINCT b.id FROM pms_builder_to_subbuilders bsb
LEFT JOIN pms_builders b ON b.id = bsb.sb_id WHERE b.fk_c_id = '1'
but it show Distinct records from subbuilders.
You can get the desired results with the following query:
SELECT DISTINCT b.b_id FROM builders b LEFT JOIN subbuilders sb ON sb.fk_b_id = b.b_id WHERE b.fk_c_id = '1' AND ISNULL(fk_b_id);
i think you want this query:
SELECT DISTINCT b_ID
FROM builders
WHERE b_ID NOT IN
(SELECT DISTINCT fk_b_id FROM subbuilders)
but it returns
b_ID
========
3
4
5
7
but i didn't understand your statement: must have same fk_c_id. What do you mean by that?
b_id = fk_c_id
if that's the case then there will be no rows returned because only record 1 has the same b_ID and fk_c_id but exists in table subbuilders