MySQL Locate Duplicates between two table with similar column - mysql

Using this question's answer. I'm trying to find duplicate records between two tables by the column names matrix_unique_id and Matrix_Unique_ID in each table and then display the full address. The Full address columns are formatted differently from each other in each table so I cannot use that as a comparison. I'm getting an "unknown column fort_property_res.matrix_unique_id" error but everything looks okay?
So two questions:
Will this query find duplicates correctly?
Why the unknown column error?
SQL query:
SELECT matrix_unique_id, full_address
FROM fort_property_res
INNER JOIN (
SELECT Matrix_Unique_ID, FullAddress
FROM sunshinemls_property_res
GROUP BY FullAddress
HAVING count(fort_property_res.matrix_unique_id) > 1
) dup ON fort_property_res.matrix_unique = sunshinemls_property_res.Matrix_Unique_ID

The solution you're trying to copy is a totally different case. You have two tables and (it looks like) a convenient matrix_unique_id to join on, so this is much easier:
SELECT fort.matrix_unique_id, fort.full_address AS fortAddress, sun.FullAddress AS sunAddress
FROM fort_property_res fort, sunshinemls_property_res sun
WHERE fort.matrix_unique_id = sun.Matrix_Unique_ID

Related

SELECT statement inside a CASE statement in SNOWFLAKE

I have a query where i have "TEST"."TABLE" LEFT JOINED to PUBLIC."SchemaKey". Now in my final select statement i have a case statement where i check if c."Type" = 'FOREIGN' then i want to grab a value from another table but the table name value i am using in that select statement is coming from the left joined table column value. I've tried multiple ways to get to work but i keep getting an error, although if i hard code the table name it seems to work. i need the table name to come from c."FullParentTableName". Is what i am trying to achieve possible in snowflake and is there a way to make this work ? any help would be appreciated !
SELECT
c."ParentColumn",
c."FullParentTableName",
a."new_value",
a."column_name"
CASE WHEN c."Type" = 'FOREIGN' THEN (SELECT "Name" FROM TABLE(c."FullParentTableName") WHERE "Id" = 'SOME_ID') ELSE null END "TestColumn" -- Need assistance on this line...
FROM "TEST"."TABLE" a
LEFT JOIN (
select s."Type", s."ParentSchema", s."ParentTable", s."ParentColumn", concat(s."ParentSchema",'.','"',s."ParentTable",'"') "FullParentTableName",s."ChildSchema", s."ChildTable", trim(s."ChildColumn",'"') "ChildColumn"
from PUBLIC."SchemaKey" as s
where s."Type" = 'FOREIGN'
and s."ChildTable" = 'SOMETABLENAME'
and "ChildSchema" = 'SOMESCHEMANAME'
) c
on a."column_name" = c."ChildColumn"
Thanks !
In Snowflake you cannot dynamically use the partial results as tables.
You can use a single bound value via identifier to bind a value to table name
But you could write a Snowflake Scripting but it would need to explicitly join the N tables. Thus if you N is fixed, you should just join those.

How to sum all elements in an indexed table

I tried to make a query to my database with this structure: Data Base Structure
I did this query:
SELECT partido.acronimoPartido, SUM(votosacta.numVotos) FROM partido, votosacta WHERE votosacta.partido_idpartido=1 AND partido.idpartido=1
This query does work like I want but displays only the SUM of 'votos' for idpartido=1
I want to be able to sum numVotos from 'votosacta' table for each member of my 'partido' table indexed in 'votosacta' but I seem to not be able to get the right sintax.
I tried something like this:
SELECT partido.acronimoPartido, SUM(votosacta.numVotos) FROM partido, votosacta WHERE votosacta.partido_idpartido = partido.idpartido
You need a group by clause:
select p.acronimoPartido,
SUM(v.numVotos)
from partido p
join votosacta v on v.partido_idpartido = p.idpartido
group by p.acronimoPartido
Also, use explicit join syntax instead of old comma based syntax and use aliases to make your queries concise and readable.

(MySQL) Why are there no rows showing after a query?

I have a quick question about why rows are not showing up after a query. Here's a sample query I'm using based on 3 tables I have in my database.
select ff9characters.name, ff9apchart.apneeded
from ff9characters
join ff9apchart
on ff9apchart.charID=ff9characters.id
join ff9abilities
on ff9apchart.abilityid=ff9abilities.id
where ff9abilities.ability = 'Accuracy+'
group by ff9characters.name
order by ff9characters.id;
Nothing shows up at all. I would like to add that the ability column in ff9abilities is filled with data of the text datatype and has 'Accuracy+' listed.
If I change the WHERE statement to:
where ff9abilities.ability like 'Acc%'
All of the data shows up. I got 4 rows returned (which was my desired result).
Should I have changed the datatype for the ability column from text to varchar instead so that it reads all the data?
Could be you have improper white space try with trim and (lower)
select ff9characters.name, ff9apchart.apneeded
from ff9characters
join ff9apchart
on ff9apchart.charID=ff9characters.id
join ff9abilities
on ff9apchart.abilityid=ff9abilities.id
where trim(ff9abilities.ability) = 'Accuracy+'
group by ff9characters.name
order by ff9characters.id;

Group rows but keep values where not null

I am trying to group rows in MySQL but end up with a wrong result.
My DB looks like this:
I'm using this query:
SELECT
r_id, va_id,va_klasse,va_periode,
1va_mer,1va_hjem,1va_mot,1va_bil,1va_fit,1va_hand,1va_med,1va_fra,
2va_mer,2va_hjem,2va_trae,2va_bil,2va_sty,2va_mus,2va_med,2va_fra,
3va_mer,3va_hjem,3va_mot,3va_bil,3va_pima,3va_nat,3va_med,3va_fra,
va_lock, va_update
FROM o6hxd_valgfag
WHERE va_klasse IN('7A','7B','7C','8A','8B','8C','9A','9B','9C')
GROUP BY va_id
ORDER BY va_klasse,va_name
This produces a wrong result, where one row is returned with only the first three numbers 123 and not the ones from row two and three.
What I would like is a result where the numbers 123, 321 and 132 are gathered in one line.
I can explain more detailed if this isn't sufficient.
If across those fields there should only be ever one value, you should really have them all in the same record and go about fixing it to insert and update the same record.
Ie I am aware that you database isn't designed correctly
However
To dig you out, you could give this a crack, I suppose.
SELECT
r_id, va_id,va_klasse,va_periode,
MAX(1va_mer),MAX(1va_hjem),MAX(1va_mot),MAX(1va_bil),MAX(1va_fit),MAX(1va_hand),MAX(1va_med),MAX(1va_fra),
MAX(2va_mer),MAX(2va_hjem),MAX(2va_trae),MAX(2va_bil),MAX(2va_sty),MAX(2va_mus),MAX(2va_med),MAX(2va_fra),
MAX(3va_mer),MAX(3va_hjem),MAX(3va_mot),MAX(3va_bil),MAX(3va_pima),MAX(3va_nat),MAX(3va_med),MAX(3va_fra),
va_lock, va_update
FROM o6hxd_valgfag
WHERE va_klasse IN('7A','7B','7C','8A','8B','8C','9A','9B','9C')
GROUP BY va_id
ORDER BY va_klasse,va_name
Your query will not work as intended. Think about this use-case:
what if for row1 (r_id =9), the fields 2va_sty, 2va_mus, 2va_med are not empty and has values?
In such case what should your desired output be? It certainly cannot be the numbers 123, 321 and 132 gathered in one line. Group by is usually used if you want to use aggregate functions executed against a certain field value, in your case va_id.
Not a solution to your problem but i think a better query would be like this (because of the not named columns in the group by clause https://dev.mysql.com/doc/refman/5.5/en/group-by-handling.html):
SELECT
aa.r_id, aa.va_id, aa.va_klasse, aa.va_periode,
aa.1va_mer, aa.1va_hjem, aa.1va_mot, aa.1va_bil, aa.1va_fit, aa.1va_hand, aa.1va_med, aa.1va_fra,
aa.2va_mer, aa.2va_hjem, aa.2va_trae, aa.2va_bil, aa.2va_sty,2va_mus, aa.2va_med, aa.2va_fra,
aa.3va_mer, aa.3va_hjem, aa.3va_mot, aa.3va_bil, aa.3va_pima, aa.3va_nat, aa.3va_med, aa.3va_fra,
aa.va_lock, aa.va_update
FROM o6hxd_valgfag AS aa
INNER JOIN (
SELECT va_id
FROM o6hxd_valgfag
GROUP BY va_id
) AS _aa
ON aa.va_id = _aa.va_id
WHERE aa.va_klasse IN ('7A','7B','7C','8A','8B','8C','9A','9B','9C')
ORDER BY aa.va_klasse, aa.va_name;

How to write update query using 2 tables and conditions

I am trying to update/revise the department names in a table called "DEPART_NAMES_AS_SUBMITTED" using another table called "DEPART_NAMES_REQUIRED." I would like this update to occur only if the line numbers in "DEPART_NAMES_AS_SUBMITTED" are within the line number range [LOW] [HIGH] in the second table called "DEPART_NAMES_REQUIRED." If the line number is less/more than the [LOW] [HIGH] range the department name should remain the same. I have unsuccessfully tried numerous SQLs including the following:
UPDATE DEPT_NAMES_SUBMITTED INNER JOIN DEPT_NAMES_REQUIRED ON(DEPT_NAMES_SUBMITTED.LINE_NUMBER = DEPT_NAMES_REQUIRED.HIGH) AND (DEPT_NAMES_SUBMITTED.LINE_NUMBER = DEPT_NAMES_REQUIRED.LOW) SET DEPT_NAMES_SUBMITTED.DEPART_NAME = [DEPT_NAMES_REQUIRED].[DEPART_NAME]
WHERE (((DEPT_NAMES_REQUIRED.LINE_NUMBER) Between [low] And [high]));
Thank you for taking the time to read and answer this question.
I think your query is fine if you remove the square braces and put in the right logic:
UPDATE DEPT_NAMES_SUBMITTED ns INNER JOIN
DEPT_NAMES_REQUIRED hr
ON ns.LINE_NUMBER <= nr.HIGH AND
ns.LINE_NUMBER >= nr.LOW
SET ns.DEPART_NAME = nr.DEPART_NAME;
Notice that table aliases make the query easier to write and to read.