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
Related
I have the functional dependency A->B and I need to clean the data by correcting rows that violate this FD. Currently the data is:
A B
1 a
2 b
2 b
2 b
2 c
3 d
3 d
6 c
6 c
6 c
6 c
6 c
6 b
but I need it to be:
A B
1 a
2 b
2 b
2 b
2 b
3 d
3 d
6 c
6 c
6 c
6 c
6 c
6 c
How can I do this within a query? I have an existing query that finds the correct value for B for each A value (for example, 1's correct value is a, 2's is b, 3's is d, 4's is c) but I'm not sure how to clean the data using a standard SELECT query.
Craft an UPDATE or MERGE statement that only corrects those that you know are wrong to what you know is right.
Example update statement for MySql:
UPDATE yourtable t
JOIN
(
SELECT 2 AS A, 'b' AS B
UNION ALL SELECT 3, 'd'
UNION ALL SELECT 6, 'c'
) AS correction
ON t.A = correction.A
SET t.B = correction.B
WHERE t.B != correction.B
db<>fiddle here
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
My understanding of left outer join is,
table1:
id(pk) name(unique_key) address phone
table2:
new_id(pk) name(foreign key) company work-experience
1st table:
1 a x 123
2 b y 234
3 c z 345
2nd table
1 a aaa 2
2 a aab 3
3 b aab 3
if I will do,
select * from table1 left outer join table2 on table1.name=table2.name,
it will give me
1 a x 123 1 a aaa 2
1 a x 123 2 a aab 3
2 b y 345 3 b aab 3
3 c z 345 NULL NULL NULL NULL
Now instead of above result, I want to get all the rows where company is aab. Also , for any entry in 1st table, if there is no corresponding entry in 2nd table then it should give me NULL for all columns in 2nd table.
like this:
1 a x 123 aab 3
2 b y 234 aab 3
3 c z 345 NULL NULL
is the above result possible with left outer join ? If not, How can I get the above result ?
You can simply add the conditions for the second table (right-side table), in the ON part of LEFT JOIN.
SELECT * FROM table1 AS t1
LEFT OUTER JOIN table2 AS t2
ON t1.name = t2.name AND
t2.company = 'aab'
Also, in case of multi-table queries, it is advisable to use Aliasing, for code clarity (enhanced readability), and avoiding unambiguous behaviour.
select t1.name,t1.address,t1.phoneNo,t2.comapnay,t2.workExperiance from table1 as t1
left outer join table2 as t2 on t1.name=t2.name AND
t2.company = 'aab'
id id2 id3 address
1 1 0 A
2 1 0 B
3 2 0 A
4 3 0 A
5 3 0 B
6 4 0 A
Hello every one ... plz solve my query... i have this table in this format. Now i want to get value from this table accordingly uniquely "id2" . It means I want all the values having address "B" along with "A"(but "A" should not have same "id2" as to B). Thus I'll get all the unique values of "id2"....plz guys...do something
currently I'm using the query for getting "B"
SELECT * FROM t1 WHERE address=(SELECT MAX(address) FROM t1)
now also i want all the "A" but it should not have same "id2" with "B"
I want its ans
id id2 id3 address
2 1 0 B
3 2 0 A
5 3 0 B
6 4 0 A
any idea???
SELECT a.*
FROM tableName a
INNER JOIN
(
SELECT id2, MAX(ID) max_ID
FROM tableName
GROUP BY id2
) b ON a.id2 = b.id2 aND
a.ID = b.max_ID
SQLFiddle Demo
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?