sql update table from a join query - sql

hi I have table as below named enrolment, I need to update the null averagemark from the data from another table so that the average of all assignments done by a student will populate the field. Can I anyone advise how to do this using Isql as the dbms?
enrolment-
student_id- course code - averagemark
1 a1 - 0
2 b2 - 0
3 c3 - 0
4 d4 - 0
assignment-
student_id- course code - assignment number mark
1 a1 1 - 50
1 b2 2 - 55
2 a1 1 - 60
2 b2 2 - 65

This example is based on Microsoft SQL Server. Copying the sample from How can I do an UPDATE statement with JOIN in SQL?:
update u
set u.assid = s.assid
from ud u
inner join sale s on
u.id = s.udid
Try something like this (providing the name of the column in the Student table is AverageMark)
UPDATE s
SET s.AverageMark = AVG(mark)
FROM Student s LEFT OUTER JOIN enrolment e
on e.Student_Id = e.Student_Id
LEFT OUTER JOIN assignment a
ON e.student_id = a.student_id
AND e.course_code = a.course_code
GROUP BY e.student_id, e.course_code

Related

record doesn't show up if there's no corresponding data

I have a few tables I'm trying to join however, I'm missing some rows if there's no data, but would like to have them be displayed
tblCountry
sID sCountry
1 Algeria
2 Armenia
3 Belgium
tblRefData
RefID IDnum sID
1 7 1
2 8 2
3 9 3
tblMData
IDnum IDa
7 123
8 123
Here's what my query looks like:
Select tblCountry.sCountry, count(tblMData.Ida) as CountIDa
From tblRefData
inner join tblMData on tblRefData.IDnum = tblMData.IDnum
inner join tblCountry on tblRefData.sID = tblCountry.sID
GroupBy tblCountry.sCountry
What my desired end result is:
sCountry CountIDa
Algeria 1
Armenia 1
Belgium 0
What I'm currently getting is
sCountry CountIDa
Algeria 1
Armenia 1
So if country does not have corresponding data in tblMData, that country does not show up in my result. Any ideas?
You should use left join if the on condition columns values don't match
Select
tblCountry.sCountry,
case
when (count(tblMData.Ida) is null) then 0 end as CountIDa
From
tblRefData
left join
tblMData on tblRefData.IDnum = tblMData.IDnum
left join
tblCountry on tblRefData.sID = tblCountry.sID
Group By
tblCountry.sCountry
You could change the 'INNER JOIN' to a 'LEFT JOIN' to bring back data when there is no corresponding data. Then you could you the ISNULL() function to set the NULL value to a zero.
You want a left join, but have to rearrange the from clause:
Select c.sCountry, count(m.Ida) as CountIDa
From tblCountry c left join
tblRefData r
on r.sID = c.sID left join
tblMData m
on r.IDnum = m.IDnum
GroupBy c.sCountry;
Notice that the table aliases make the query easier to write and to read.

SQL Access 2003 create column if exisit value

I've three table
Product
- ID_Product
UP
- ID_Product
- UP_SUM
DOWN
- ID_Product
- DOWN_SUM
The query on this three tables create a column Total_SUM = [UP_SUM]-[DOWN_SUM]
The problem is that if there is no value in DOWN_SUM there is no result in Total_SUM.
EX.
UP
1 - 2
2 - 4
3 - 2
DOWN
1 - 1
3 - 1
TOTAL_SUM
(1) 1
(2) 4 -> value missing
(3) 1
In the fact i don't get the value for the ID 2.
How can i use statement if not isNULL to get all values in TOTAL_SUM? Actually the SQL Query is:
SELECT
Product.ID_Product,
UP.UP_SUM,
DOWN.DOWN_SUM,
[UP_SUM]-[DOWN_SUM] AS TOTAL_SUM,
FROM (PRODUCT INNER JOIN UP ON Product.ID_Product = UP.ID_Product)
INNER JOIN DOWN ON Product.ID_Product = DOWN.ID_Product;
use the iif condition that will replace null with a value, and use left outer join since normal join is filtering those rows without a match. A left join will keep them with the values NULL
IIF(ISNULL(DOWN.DOWN_SUM),0,DOWN.DOWN_SUM)
SELECT
Product.ID_Product,
IIF(ISNULL(UP.UP_SUM),0,UP.UP_SUM),
IIF(ISNULL(DOWN.DOWN_SUM),0,DOWN.DOWN_SUM),
IIF(ISNULL(UP.UP_SUM),0,UP.UP_SUM)-IIF(ISNULL(DOWN.DOWN_SUM),0,DOWN.DOWN_SUM) AS TOTAL_SUM,
FROM (PRODUCT LEFT OUTER JOIN UP ON Product.ID_Product = UP.ID_Product)
LEFT OUTER JOIN DOWN ON Product.ID_Product = DOWN.ID_Product;

How to Update Using Join (Linked Server)

I want to match the data between 2 databases.
I have 2 Databases. Aa and Bb, and I want to compare Aa to Bb. Database Bb is in linked server
I have join code like this
SELECT
B.Employee_Name, B.Employee_NIP, B.DomainName, A.NAMA, A.NIP,
A.StatusEmployee, A.ActiveStatus
FROM
[SERVER-B].Bb.dbo.employee_hierar AS B RIGHT OUTER JOIN
Bb AS B ON B.NIP = A.Employee_NIP
and I want update A.StatusEmployee from Y to N if there is NULL data on B.Employee_Name and B.Employee_NIP
note:
SQLServer
Please Advice
You can use a join. Something like:
update a
set StatusEmployee = 'N'
from bb a LEFT JOIN
[SERVER-B].Bb.dbo.employee_hierar b
on B.NIP = A.Employee_NIP
where b.EmployeeName is null and b.Employee_NIP is null and
a.StatusEmployee = 'Y';
This is same to update multi tables in one database. Standard sql should be written like this:
UPDATE A
SET StatusEmployee = "N"
WHERE NOT EXISTS
( SELECT * FROM B WHERE B.NIP = A.Employee_NIP )

Update a table by using a join [duplicate]

This question already has answers here:
Update a table using JOIN in SQL Server?
(13 answers)
Closed 8 years ago.
I have two tables ta, tb. ta columns - cId, c1, c2. c1 and c2 contain nulls and need to be filled with data. tb columns - cId, c3, c4. The data for c1 and c2 will come from c3 and c4 respectively.
So, I tried to do a simple inner join first. Both tables were aliased as al_ta and al_tb respectively. Then, I put an update statement -
UPDATE ta SET
al_ta.c1 = al_tb.c3,
al_ta.c2 = al_tb.c4
FROM ta AS al_ta
INNER JOIN tb AS al_tb
ON al_tb.cId = al_tb.cId
This does not work and I get an error - The multi-part identifier al_ta.c1 could not be bound. How do I make this work ?
Sample tables -
ta
cId c1 c2
1 NULL NULL
2 NULL NULL
3 NULL NULL
tb
cId c3 c4
1 11 111
2 22 222
3 33 333
4 44 444
When referencing the columns, you need to use the alias, not the base table name, if you've abstracted the table names away in the JOIN. Guessing at what your join might look like, you probably meant to write it this way:
UPDATE ta SET
ta.c1 = tb.c3,
ta.c2 = tb.c4
FROM dbo.some_long_table_name_a AS ta
INNER JOIN dbo.some_long_table_name_b AS tb
ON ta.cId = tb.cId
WHERE ta.c1 IS NULL OR ta.c2 IS NULL;
I don't understand the purposes of saying:
FROM ta AS al_ta
Why would you bother using an alias here that is actually harder to write than the original table name?
Please Try it
update ta set
ta.c1 = b.c3,
ta.c2 = b.c4
from ta a join tb b on a.cid = b.cid

SQL query get record table A based on parentid Table B

[SOLVED: thanks to bluefeet and Brian I got to build this working query (turned out I also needed a fourth table / view)]"
SELECT A.SalesLineID, A.ArticleResultID, B.ID, C.ID, D.Value
FROM VIEWA AS A
INNER JOIN TABLEB AS B ON A.ArticleResultID = B.ArticleResultID
AND B.Name = N'Collect' AND DAPR.Value = 1
INNER JOIN TABLEC AS C ON B.ArticleResultID = C.ID
AND C.Name='Assemble'
AND C.Value = 1
INNER JOIN TABLED AS D ON D.ArticleResultID = C.ParentId
AND D.Name = 'IndexY'
WHERE (A.SalesID = #SalesID)
[UPDATE: I've made a mistake assuming IndexY/Color and ProdId where fields Table A is some kind of parameter / property table with only 5 columns ID - NAME - VALUE - ARTICLERESULTID - PRODID. IndexY is a value of the Name field..]
I'm having trouble building the right sql query to get this trick done:
I have the following 2 tables:
Table A
ID Name Value ArticleResultID ProdID Color
1 Operation Collect 110 10 BLACK
2 IndexY 10 110 10 -
3 Operation Collect 101 11 WHITE
Table B
ID ParentID Name Value
101 110 Assemble 1
101 100 Assemble 0
Steps:
Find record in A with Name = Operation and Value = Collect and ProdId = 11 AS ORG_A
Find record in B With B.ID = ORG_A.ArticleResultId AND B.NAME = 'Assemble'AND B.VALUE = 1 AS B
Find record in A With A.ArticleResultID = B.ParentID as NEW_A
In the above scenario thats ORG_A.ArticleResultID = 11 --> B.ParentID = 110 --> NEW_A.ARTICLERESULTID = 110 --> (IndexY - Value - 10)
Much appreciated if someone can tell me how to build this query..
Best regards,
Mike D
[OLD DESCRIPTION:]
I'm having trouble building the right sql query to get this trick done:
I have the following 2 tables:
Table A
Name Value ArticleResultID IndexY Color ProdID
Operation Collect 110 10 - 0
Operation Collect 101 _ Black 100
Table B
ID ParentID Name Value Dx Dy
101 110 Assemble 1 1000 500
101 100 Assemble 0 400 300
I want to fetch all records from A where NAME equals 'operation', VALUE equals 'Collect' and PRODID = '100' but I also want (here's my problem) the IndexY value of the record in Table A with the PARENTID in Table B which joins on TABLE B.ID = A.ArticleResultID AND Name = 'Assemble' and VALUE = '1'
In the above scenario thats ParentID 110 which gives me the record in Table A with ArticleResultID 110 with IndexY (10).
Much appreciated if someone can tell me how to build this query..
Best regards,
Mike D
Since your requirements are not the clearest. How about this:
SELECT a1.*, a2.indexy as additionalIndexY
FROM ta a1
INNER JOIN tb b
ON a1.articleresultid = b.id
INNER JOIN ta a2
ON b.parentid = a2.articleresultid
WHERE a1.name = 'Operation'
AND a1.prodid = 100
AND b.name = 'Assemble'
AND b.value = 1
Here is a sqlfiddle with a working version
If you just want the record with the 110 articleresultid, then:
SELECT a2.*
FROM ta a1
INNER JOIN tb b
ON a1.articleresultid = b.id
INNER JOIN join ta a2
ON b.parentid = a2.articleresultid
WHERE a1.name = 'Operation'
AND a1.prodid = 100
AND b.name = 'Assemble'
AND b.value = 1
see the sqlfiddle for the second example
Does this work for you?
SELECT a.*,c.IndexY
FROM [TableA] a
JOIN [TableB] b ON a.ArticleResultId = b.id
AND b.Name ='Assemble'
AND b.Value = 1
JOIN [TableA] c ON b.ParentId = c.ArticleResultID
WHERE a.name = 'Operation'
AND a.Value = 'Collect'
AND a.ProdId = 100