SQL problems, JOINS - sql

Im having some problem with my SQL code. My assignment is to present some information about teachers(Lärare.personnummer) who don't have teach the course "Java2"(Kurstilfälle.kurs). The code is right but my problem is that there is one teacher who don't have teach any course(kurs). So the information about her is not in the result. I want to get this last persons information in my result.
My code--> http://imgur.com/QT3u4TL
Database--> https://ilearn2.dsv.su.se/mod/resource/view.php?id=21941
SELECT DISTINCT Person.personnummer, Person.namn, tjänsterum, telefon
FROM Kurstillfälle,
Person,
Lärare
WHERE Person.personnummer = Lärare.personnummer
AND Kurstillfälle.lärare = Person.personnummer
AND Lärare.personnummer NOT IN (SELECT Kurstillfälle.lärare
FROM Kurstillfälle WHERE kurs = 'Java2')
Maybe someone can help me with this. Thanks.

With new style JOIN, return a teacher who NOT EXISTS as teacher for Java2:
SELECT p.personnummer, p.namn, tjänsterum, telefon
FROM Person p
INNER JOIN Lärare l ON p.personnummer = l.personnummer
WHERE NOT EXISTS (SELECT 1 FROM Kurstillfälle
WHERE kurs = 'Java2'
AND lärare = p.personnummer)
Edit: I don't know Access syntax, but try INNER JOIN instead of just JOIN!

Using Joins this should get what you want
SELECT DISTINCT p.personnummer, p.namn, tjänsterum, telefon
FROM Kurstillfälle k
INNER JOIN Person p ON k.larare = p.personnummer
INNER JOIN Lärare l ON p.personnummer = l.personnummer
WHERE k.kurs !='Java2'

Related

LEFT JOIN DUPLICATING VALUES (QUERY WITH WHERE IN)

I need to get rid of duplicated values return from a LEFT JOIN that I did, I'm such a beginner in SQL so I do hope you guys help me out!
SELECT *
,tm.TEAM_NAME
,up.USER_NAME OWNER_NAME
FROM NTTUS_INIT_TM_TASK_HEADER th
LEFT JOIN NTTUS_TEAM tm
ON th.TEAM_ID = tm.TEAM_ID
LEFT JOIN NTTUS_USER_PROFILE up
ON th.OWNER_ID = up.USER_ID
WHERE 1 = 1
AND INIT_ID IN(${arr.ids})
Actually from your description, it's hard to understand your table. But try this. If this doesn't work let me know
SELECT *
,tm.TEAM_NAME
,up.USER_NAME OWNER_NAME
FROM
NTTUS_INIT_TM_TASK_HEADER as th
LEFT JOIN
NTTUS_TEAM as tm ON th.TEAM_ID = tm.TEAM_ID
LEFT JOIN
NTTUS_USER_PROFILE as up ON th.OWNER_ID = up.USER_ID
WHERE
INIT_ID IN(${arr.ids})
Group By
th.*
,tm.TEAM_NAME
,up.USER_NAME OWNER_NAME
Having count(*) = 1

sql inner join over multiple tables

I've given this relational scheme and following task:
Inner Join: Return a list of professors, which gives
'lehrveranstaltung' of the 'fachbereich' with the name 'informatik'.
* print 'vorname', 'ho_name', 'lv_name'
* output should sort surnames in ascending order and if they're the same in descending order
* identical lines should online shown once
now I came up with following query:
select distinct
v.vorname,
h.ho_name,
l.lv_name
--print wanted, only once
from
vorname v,
hochschulangehoeriger h,
lehrveranstaltung l
-- from these tables
inner join fachbereich f on f.fb_name = 'Informatik'
-- only the 'informatik' events
inner join prof_haelt_lv on l.lv_nr = pl.lv_nr
-- make sure 'lehrveranstaltung' is from a professor
inner join mitarbeiter mit on pl.pers_Nr = mit.pers_Nr
-- make sure dude is a prof
where
mit.ho_nr = h.ho_nr
and
mit.ho_nr = v.ho_nr -- give only names from prof
order by
2 asc,
3 desc; -- order rules
I think this works for me (can't test it properly). But when I look at it I'll wish that I came up for a bether solution since this looks kinda ugly and wrong for me.
Is there a bether way of doing this? (Have to use inner join)
Based on the table you have, you may use the following SQL statement
SELECT DISTINCT v.vorname,
h.ho_name,
l.lv_name
FROM vorname v
INNER JOIN hochschulangehoeriger h
ON v.ho_nr = h.ho_nr
INNER JOIN mitarbeiter m
ON m.ho_nr = h.ho_nr
INNER JOIN fachbereich f
ON f.fb_nr = m.fb_nr
AND f.fb_name = 'Informatik'
INNER JOIN lehrveranstaltung l
ON l.fb_nr = f.nb_nr
INNER JOIN professor p
ON p.pers_nr = m.pers_nr
INNER JOIN prof_haelt_lv pl
ON pl.pers_nr = p.pers_nr
AND pl.lv_nr = l.lv_nr
ORDER BY 2,
3 DESC;
Also, these section on your SQL, this has no connection to any table in your SQL
inner join fachbereich f on f.fb_name = 'Informatik'
-- only the 'informatik' events
you forgot the alias for prof_haelt_lv
inner join prof_haelt_lv on l.lv_nr = pl.lv_nr
-- make sure 'lehrveranstaltung' is from a professor

View data from one table but based on other table (SQL Server)

I want to display all information of table program but it must based on other table (which is Line), I try to use join but it will show ALL information from ALL TABLE.
Can someone help me to create the query or just tell me what should i do.
TABLE LINE
------------
LineName
TABLE PACKAGE
-------------
PackageNo
PackageName
Line
TABLE FAMILY
------------
FamilyCode
FamilyName
TestQuant
TABLE PROGRAM
-------------
FamilyName
TestType
FolderPath
TestProgram
Remark
CreateTime
And this is what i have done
SELECT * FROM Program AS D
JOIN Family AS Q ON D.FamilyName = Q.FamilyName
JOIN Process AS V ON Q.TestQuant = V.PackageNo
JOIN Line AS R ON R.LineName = V.Line
WHERE V.Line = 'LINE1'
WHAT I HAVE CHANGE
SELECT DISTINCT D.FamilyName, D.TestType, D.FolderPath, D.TestProgram, D.Remark,
D.CreateTime
FROM Program D
INNER JOIN Family Q ON D.FamilyName = Q.FamilyName
INNER JOIN Process V ON Q.TestQuant = V.PackageNo
INNER JOIN Line R ON R.LineName = V.Line
WHERE V.Line = 'LINE1'
If you need ONLY the data from Program table, try this:
SELECT D.FamilyName, D.TestType, D.FolderPath, D.TestProgram, D.Remark, D.CreateTime
FROM Program D
JOIN Family Q ON D.FamilyName = Q.FamilyName
JOIN Process V ON Q.TestQuant = V.PackageNo
JOIN Line R ON R.LineName = V.Line
WHERE V.Line = 'LINE1'
It sounds like you may need a left or right outer join. Here is an example question that has some very good explanations of JOINs in the answers, the venn diagrams really helped me understand:
SQL JOIN and different types of JOINs
An example of how you might construct a join (taken from Techonthenet)
SELECT columns
FROM table1
RIGHT [OUTER] JOIN table2
ON table1.column = table2.column;
Hope this helps, SQL JOIN can be a bit tricky to understand.

Finding Non Entries within another data table (MS Access SQL)

I know there ust be a few hundred of this similar post, but I have tried all the other ways in MS Access and still cannot get it to work.
So my working code is as follows
SELECT FVR.*, V.[Week Commencing], F.Date, V.Date
FROM FVR
INNER JOIN (F
INNER JOIN V ON (F.[Week Commencing] = V.[Week Commencing]) AND (F.GUID = V.GUID))
ON (FVR.GUID = V.GUID) AND (FVR.GUID = F.GUID)
My desired effect would be to show the Dates of the "F" table that have no entries in the "V"Table.
Sorry for being crpytic on the tables but it is for work. I thoght i had a good idead on how to do most of this.
any help would be amazing as I have been pulling my hair over this for a while now.
Cheers and thanks in advance.
Editing this to add in the full code as it will make more sense.
I basically have am unable to produce the Data range from F(Forecast) that Does not match in V(Visits) am trying to bring up a list of forecasted dates that have not been visited using the Week Commencing and GUID from both tables, The FVR table is just a table that holds the regional data matching up to the GUID. #Hogan I tried your way and ended up with syntax errors, I almost got somewhere and then lost it again. I thought I had a bit more knowledge of SQL than this.
Full code is as follows
SELECT FVR.*, [Visits].[Week Commencing], [Forecast].[Forecast Date], [Visits].Date
FROM ForecastVisitRegion
INNER JOIN ([Forecast] INNER JOIN [Visits] ON ([Forecast].[Week Commencing] = [Visits].[Week Commencing])
AND ([Forecast].GUID = [Visits].GUID)) ON (FVR.GUID = [Visits].GUID)
AND (FVR.GUID = [External - Forecast].GUID)
Thanks again
Stephen Edwards
You need to use left joins:
SELECT FVR.*, V.[Week Commencing], NZ(V.Date,F.Date) as virtual_date
FROM FVR
LEFT JOIN F ON FVR.GUID = F.GUID
LEFT JOIN V ON FVR.GUID = V.GUID F.[Week Commencing] = V.[Week Commencing]
Not sure I understand why FVR is coming into the mix but you need a left Join.
Select F.*
from F
left join V on F.[Week Commencing] = V.[Week Commencing] AND F.GUID = V.GUID
where V.GUID is null
The left join ensures all the records (matched or not) from F are included in the result set. Then the where V.GUID is null removes the records where no match was found in V leaving you with the F records with no match.
Another approach would be to use the NOT EXISTS statement in the WHERE Clause
Select F.*
from F
where not exists (select * from V where F.[Week Commencing] = V.[Week Commencing] AND F.GUID = V.GUID)

SQL View statement with 2 INNER JOINS

Can anyone help me with this issue I tried many times but still haven't found the solution
Here is the original View that I have in my database but now I made changes in the database and the view need to be changed also.
Here is the view as it was:
SELECT
[tableN].*,
[tabB].[att1] AS tabB_email, [tabB].[name] AS tabB_name,
[tabC].[name] AS tabC_name
FROM
[tabC]
INNER JOIN
([tableN]
INNER JOIN [tabB] ON [tableN].[sender_id] = [tabB].[ID])
ON [tabC].[ID] = [tableN].[recipient_id]
Here is what is the difficult point for me. Now I don't have this 2 tables tabB and tabC
They are now in one table tabX and have an identifier field roleId. I manage to get all the columns except the last one [tabC].[name] AS tabC_name
Any ideas?
Try like this
SELECT [tableN].*, [tabX].[att1] AS tabB_email, [tabX].[name] AS tabB_name,
t1.[name] AS tabC_name
FROM [tabX] as t INNER JOIN ([tableN] INNER JOIN [tabX]
ON [tableN].[sender_id] = [tabX].[roleid])
ON t.[roleid] = [tableN].[recipient_id]
SELECT [tableN].*, [Tabx] .[att1] AS tabB_email, [Tabx] .[name] AS tabB_name
FROM [Tabx] A
INNER JOIN [TABLEN] B
ON A.ROLEID=B.RECIPIENT_ID
SELECT [TableN].*, Bd.[email] AS bd_email, Bd.[showname] AS bd_name,
Pc.[showname] AS pc_name
FROM
[TABLE_X] AS Pc
INNER JOIN ([TableN]
INNER JOIN [TABLE_X] AS Bd
ON [TableN].[sender_id] = Bd.[ID] AND Bd.roleID = 1)
ON Pc.[ID] = [TableN].[recipient_id] AND Pc.roleID = 2
I finally find the the code that is working as needed