I am trying to update data from one column to another using a join statement in SQL.
My two tables are rosters and scores. They share playerid.
I am trying to add data from scores.opp to rosters.opp.
Not sure what I am doing wrong. When I do the select statement below I am able to see the shared playerid, an empty column in rosters, and the column in scores that has the data I am looking to add to rosters.
SELECT a.playerid, a.opp, b.opp
FROM rosters a
JOIN scores b ON a.playerid = b.playerid
When I proceed to do the update I get an error. Here is what my update statement looks like:
UPDATE a
SET a.opp = b.opp
FROM rosters a
JOIN scores b ON a.playerid = b.playerid
I get an error saying, "#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'FROM rosters a
JOIN scores b ON a.playerid = b.playerid' at line 3"
Any help would be appreciated. Thanks!
You can use the standard sql update with exists as follows:
UPDATE ROSTERS A
SET A.OPP = (
SELECT B.OPP FROM SCORES B
WHERE A.PLAYERID = B.PLAYERID)
WHERE EXISTS (
SELECT 1 FROM SCORES B
WHERE A.PLAYERID = B.PLAYERID)
Please make sure that there is only one record in the SCORES table for matching PLAYERID. if there are multiple records available in the SCORES table for a single PLAYERID then you need to use the aggregate function MAX, COUNT, etc.. in SELECT sub-query (like SELECT COUNT(B.OPP) FROM SCORES B -- or MAX)
Related
I want to update column 'location_cost' under 'animals' table basis cost stated under 'location_costs' table. Primary key is location to join both tables. I have tried the following code but it gives me syntax error.
//UPDATE animals.*
SET animals.location_cost = location_costs.costs
FROM animals
LEFT JOIN location_costs
ON animals.location = location_costs.location;//
Error: syntax error at or near "SET"
SET animals.location_cost = location_costs.costs
I am attaching a picture which gives an idea for the tables and columns here:
Tables View
I am not able to decipher the error and would appreciate if someone can help me with this code.
Thank you.
If you want to update only the rows in animals that have a matching location in location_costs then use this syntax:
UPDATE animals
SET location_cost = location_costs.costs
FROM location_costs
WHERE location_costs.location = animals.location;
If you want to update all the rows of animals (even the rows without a a matching location in location_costs will be updated to null), then use a correlated subquery:
UPDATE animals
SET location_cost = (
SELECT location_costs.costs
FROM location_costs
WHERE location_costs.location = animals.location
);
If animalid is a column with unique values, try using an alias as follows:
Update animals
Set location_cost = location_costs.costs
From animals As a Left Join location_costs On (a.location = location_costs.location)
Where animals.animalid = a.animalid;
I have two SQL tables:
matches (columns are hometeam, awayteam, id, gameweek)
teams (columns are teamcode, teamname)
matches.hometeam and matches.awayteam consist of integers that correspond to integers in teams.teamcode. I am trying to get matches.hometeam and matches.awayteam to update to strings that are taken from corresponding strings in teams.teamname. If that is impossible, then I need to create a new table as described.
I have tried the below code, but it produces a syntax error on the penultimate two lines (error 1064 (42000)). I can't figure out why.
UPDATE matches
SET matches.hometeam = teams.teamname
FROM matches
INNER JOIN teams
ON (matches.hometeam = teams.teamcode);
Error 1064 is a MySQL error. If you are using MySQL, the correct syntax is:
UPDATE matches m JOIN
teams t
ON m.hometeam = t.teamcode
SET m.hometeam = t.teamname;
However, this will not really work. What you need to do is add ids:
alter table matches add hometeamcode int;
And then do:
UPDATE matches m JOIN
teams t
ON m.hometeam = t.teamcode
SET m.hometeamcode = t.teamname;
EDIT:
I think I misunderstood the whole situation. Your data model is totally correct. The matches table should have the integer codes, referring to the rows in teams.
You just need to write your query to get the names:
select m.*, th.teamname as hometeamname, ta.teamname as awayteamname
from matches m join
team th
on m.hometeam = th.teamcode join
team ta
on a.hometeam = ta.teamcode;
If you don't want to do the join, then encapsulate the logic in the view.
My code is such:
SELECT COUNT(*)
FROM earned_dollars a
LEFT JOIN product_reference b ON a.product_code = b.product_code
WHERE a.activity_year = '2015'
I'm trying to match two tables based on their product codes. I would expect the same number of results back from this as total records in table a (with a year of 2015). But for some reason I'm getting close to 3 million.
Table a has about 40,000,000 records and table b has 2000. When I run this statement without the join I get 2,500,000 results, so I would expect this even with the left join, but somehow I'm getting 300,000,000. Any ideas? I even refered to the diagram in this post.
it means either your left join is using only part of foreign key, which causes row multiplication, or there are simply duplicate rows in the joined table.
use COUNT(DISTINCT a.product_code)
What is the question are are trying to answer with the tsql?
instead of select count(*) try select a.product_code, b.product_code. That will show you which records match and which don't.
Should also add a where b.product_code is not null. That should exclude the records that don't match.
b is the parent table and a is the child table? try a right join instead.
Or use the table's unique identifier, i.e.
SELECT COUNT(a.earned_dollars_id)
Not sure what your datamodel looks like and how it is structured, but i'm guessing you only care about earned_dollars?
SELECT COUNT(*)
FROM earned_dollars a
WHERE a.activity_year = '2015'
and exists (select 1 from product_reference b ON a.product_code = b.product_code)
I am trying to find subdepartments where they link by description if one is their it should return the id of the one in the table I am using the following query.
SELECT DISTINCT Subdeptcode
,SubDepartment
FROM SkechersPricat
WHERE SubDepartment IN ( SELECT DISTINCT description
FROM SubDept )
AND processed = false
AND SubDeptCode = 0
I need To be able to return the subdeptcode from the sub dept table if one exists if one Doesnt exist create it so I need a query that can account for both condision im using vb.net
Main File looks like the following
Sub Debt table above
For starters lets look at your query
SELECT DISTINCT Subdeptcode
,SubDepartment
FROM SkechersPricat
WHERE SubDepartment IN ( SELECT DISTINCT description
FROM SubDept )
AND processed = false
AND SubDeptCode = 0
you are trying to say that SubDepartment column needs to be in one of the values of description from SubDept table. Unless your SubDepartment matches description exactly it will not return anything.
Proper syntax for your query would be using LEFT OUTER JOIN. By using LEFT OUTER JOIN you are saying to get all results from your left table and only records that match in your right table. Thus you still get all the records from main table even if there is no match in SubDept table.
Your query will be like this
SELECT DISTINCT s.Subdeptcode
,s.SubDepartment
FROM SkechersPricat s
LEFT OUTER JOIN SubDept sd
ON s.SubDepartment = sd.description
WHERE s.processed = false
AND s.SubDeptCode = 0
What I do see in your screenshot that GeminDepartmentID corresponds to DeptCode from SubDept table. I think you are not joining on correct value.
This is the table I'm working with:
I would like to identify only the ReviewIDs that have duplicate deduction IDs for different parameters.
For example, in the image above, ReviewID 114 has two different parameter IDs, but both records have the same deduction ID.
For my purposes, this record (ReviewID 114) has an error. There should not be two or more unique parameter IDs that have the same deduction ID for a single ReviewID.
I would like write a query to identify these types of records, but my SQL skills aren't there yet. Help?
Thanks!
Update 1: I'm using TSQL (SQL Server 2008) if that helps
Update 2: The output that I'm looking for would be the same as the image above, minus any records that do not match the criteria I've described.
Cheers!
SELECT * FROM table t1 INNER JOIN (
SELECT review_id, deduction_id FROM table
GROUP BY review_id, deduction_id
HAVING COUNT(parameter_id) > 1
) t2 ON t1.review_id = t2.review_id AND t1.deduction_id = t2.deduction_id;
http://www.sqlfiddle.com/#!3/d858f/3
If it is possible to have exact duplicates and that is ok, you can modify the HAVING clause to COUNT(DISTINCT parameter_id).
Select ReviewID, deduction_ID from Table
Group By ReviewID, deduction_ID
Having count(ReviewID) > 1
http://www.sqlfiddle.com/#!3/6e113/3 has an example
If I understand the criteria: For each combination of ReviewID and deduction_id you can have only one parameter_id and you want a query that produces a result without the ReviewIDs that break those rules (rather than identifying those rows that do). This will do that:
;WITH review_errors AS (
SELECT ReviewID
FROM test
GROUP BY ReviewID,deduction_ID
HAVING COUNT(DISTINCT parameter_id) > 1
)
SELECT t.*
FROM test t
LEFT JOIN review_errors r
ON t.ReviewID = r.ReviewID
WHERE r.ReviewID IS NULL
To explain: review_errors is a common table expression (think of it as a named sub-query that doesn't clutter up the main query). It selects the ReviewIDs that break the criteria. When you left join on it, it selects all rows from the left table regardless of whether they match the right table and only the rows from the right table that match the left table. Rows that do not match will have nulls in the columns for the right-hand table. By specifying WHERE r.ReviewID IS NULL you eliminate the rows from the left hand table that match the right hand table.
SQL Fiddle