What kind of query do I need? - sql

I have a table called physical_exam_tool_and_body_parts that has:
id physical_exam_tool_id body_part_id
the body_parts table has: name_tid
the physical_exam_tools table has: name_tid
The translation table looks like:
id lang text
I'm trying to query:
SELECT physical_exam_tool_text,
body_part_text
FROM physical_exam_tool_and_body_parts
WHERE translation.lang = 'fr'
I want the names of the body part and physical exam tool for the lang 'fr'. How can I do this. I'm new to joins.
body_parts and physical_exam_tools tables have:
id name_tid
name_tid is the id in the translation table. The translation table has id lang text. So the primary key for translations is a composite key (id,lang).
In physical_exam_body_part_and_tool the id's in that are just the ids (foreign keys) for the body_parts and physical_exam_tools table.

Join to the Translation Table twice where lang = 'fr'.
SELECT t.[text] AS [ExamTool], t2.[text] AS [BodyPart]
FROM physical_exam_tool_and_body_parts as p
inner join physical_exam_tool as pet
on p.physical_exam_tool_id = pet.physical_exam_tool_id
and t.lang = 'fr'
inner join translation as t
on pet.name_tid = t.id
inner join body_parts as b
on p.body_part_id = b.body_part_id
inner join translation as t2
on b.name_tid = t2.id
and t2.lang = 'fr'

Something like this.
SELECT PHYSICAL_EXAM_TOOL_TEXT,
BODY_PART_TEXT
FROM PHYSICAL_EXAM_TOOL_AND_BODY_PARTS A
INNER JOIN BODY_PARTS B
ON A.BODY_PART_ID = B.ID
INNER JOIN TRANSLATION C
ON C.ID = B.NAME_TID
WHERE C.LANG = 'FR'

Related

Use name of controllers to find table name in Select

I want to extract a ID , User_ID value from one of the Companies and Contract tables, depending on the ContorollerName value.
select P.TitleProject, P.StartDateProject, P.EndDateProject,P.ControllerID,P.RecordID,P.IsAllocated,P.ProjectStatus_ID,
CN.ControllerName,CN.PersianName,
PU.ProjectID,PU.UserID,PU.RoleID,
CASE
WHEN CN.ControllerName = 'Company' THEN
Companies.Id,Companies.[User_Id]
WHEN CN.ControllerName = 'Contract' THEN
Contracts.Id,Contracts.[User_Id]
END
from Projects P
left outer join Controllers CN ON P.ControllerID = CN.Id
left outer join ProjectUsers PU ON P.Id = PU.ProjectID
where P.IsAllocated = 1
For example, if ContorollerName is 'Company' , the select command is as follows :
select P.TitleProject, P.StartDateProject, P.EndDateProject,P.ControllerID,P.RecordID,P.IsAllocated,P.ProjectStatus_ID,
CN.ControllerName,CN.PersianName,
PU.ProjectID,PU.UserID,PU.RoleID,
Companies.Id,Companies.[User_Id]
You are on the right track -- using left join. But you need to add the tables to the from clause with the appropriate logic.
The logic for the join is quite unclear. The query looks something like this:
select . . .,
coalesce(c.id, co.id) as id,
coalesce(c.user_id, co.user_id) as user_id
from Projects P left join
Controllers CN
on P.ControllerID = CN.Id left join
ProjectUsers PU
on P.Id = PU.ProjectID left join
companies c
on c.? = ? and -- no idea what the right join conditions are
c.ControllerName = 'Company' left join
contracts co
on co.? = ? and -- no idea what the right join conditions are
co.ControllerName = 'Contract'
where P.IsAllocated = 1

Check and get Parent Tables - Firebird

Anyone can help me a bit?
I use this Query to get all the constraint names, foreign tables, fields with their contraint tables and fields...
select distinct Con.rdb$constraint_name,
Rel.rdb$relation_name TableName ,Rel.rdb$field_name FieldName,
FCon.rdb$Relation_Name ForeignTableName, FIseg.rdb$Field_Name ForeignFieldName
from rdb$relation_fields Rel
inner join rdb$relation_constraints Con on (Con.rdb$relation_name = Rel.rdb$relation_name and Con.rdb$constraint_type like 'FOREIGN%')
inner join rdb$indices IDX on (IDX.rdb$index_name = Con.rdb$index_name)
inner join rdb$index_segments ISeg on (ISeg.rdb$index_name = Idx.rdb$index_name and ISeg.rdb$Field_Name = Rel.rdb$field_name)
inner join rdb$Relation_Constraints FCon on (FCon.rdb$index_name = Idx.rdb$Foreign_Key)
inner join rdb$index_segments FIseg on (FISeg.rdb$index_name = Idx.rdb$Foreign_key and FISeg.rdb$Field_Position = ISeg.rdb$Field_Position)
where Rel.rdb$relation_name not like 'RDB$%' and
FCon.rdb$Relation_Name <> Rel.rdb$relation_name and
FCon.rdb$Relation_Name = :TABLENAME
I would like to check whether the specified table (:TABLENAME) has a parent table or not and I need it's name, foreignfieldname etc like in my first query.
For example:
Applications -> Licenses -> Licenseinfos -> ''
-> Registrations -> ''
If I add Licenseinfos I would like to get "Licenses"
If I add Registrations I would like to get "Licenses"
Thanks for the answers! I use firebird 2.5
The simplest way to do this is using the following query. It identifies both sides of the constraint using the table RDB$REF_CONSTRAINTS, this doesn't need to do anything with the RDB$INDICES table (which would be a complication if you had keys with multiple columns):
select
PK.RDB$RELATION_NAME as PKTABLE_NAME
,FK.RDB$RELATION_NAME as FKTABLE_NAME
from RDB$RELATION_CONSTRAINTS FK
inner join RDB$REF_CONSTRAINTS RC on FK.RDB$CONSTRAINT_NAME = RC.RDB$CONSTRAINT_NAME
inner join RDB$RELATION_CONSTRAINTS PK on PK.RDB$CONSTRAINT_NAME = RC.RDB$CONST_NAME_UQ
where FK.RDB$RELATION_NAME = :TABLENAME

Not able to LEFT OUTER JOIN three tables

I've got a problem.
Currently I have three tables, the first table is the main table and if there is no record in the second table, write it too. But if a record exists in the second table, so from the third table display "dispo" information..
I want to use three SAP table - lagp, lqua and marc.
Me goal is write all stock positions from lagp.
2x LEFT JOIN doesnt work: "Unable to compare with"B~MATNR". A table can be joined with a maximum of one other table usign LEFT OUTER JOIN.
Structure:
TYPES:
BEGIN OF t_work,
lgnum TYPE lgnum,
lgtyp TYPE lgtyp,
lgpla TYPE lgpla,
bdatu TYPE lagp_bdatu,
matnr TYPE matnr,
verme TYPE lqua_verme,
meins TYPE meins,
dispo TYPE dispo,
END OF t_work.
DATA:
lt_work TYPE TABLE OF t_work INITIAL SIZE 0,
ls_work LIKE LINE OF lt_work.
And SQL command:
SELECT a~lgnum a~lgtyp a~lgpla a~bdatu b~matnr b~verme b~meins c~dispo FROM lagp AS a
LEFT JOIN lqua AS b ON a~lgnum = b~lgnum AND a~lgtyp = b~lgtyp AND a~lgpla = b~lgpla
INNER JOIN marc AS c ON b~matnr = c~matnr AND b~werks = c~werks
INTO TABLE lt_work
WHERE a~lgnum IN so_lgnum
AND a~lgtyp IN so_lgtyp
AND a~skzua EQ space
AND a~skzue EQ space.
But as result is only one stock position - http://i.stack.imgur.com/1sEEo.png
Can you tell me, how the SQL code has look?
Thank you
Try:
SELECT a~lgnum, a~lgtyp, a~lgpla, a~bdatu, b~matnr, b~verme, b~meins, c~dispo
FROM lagp AS a
LEFT JOIN ( lqua AS b
JOIN marc AS c ON b~matnr = c~matnr AND b~werks = c~werks )
ON a~lgnum = b~lgnum AND a~lgtyp = b~lgtyp AND a~lgpla = b~lgpla
INTO TABLE #lt_work
WHERE a~lgnum IN #so_lgnum
AND a~lgtyp IN #so_lgtyp
AND a~skzua EQ #space
AND a~skzue EQ #space.
Other option would be to put the join between LQUA and MARC in a view and do an outer join with the view. Or split out the select on MARC and do that while looping at the data found with a select on LAGP and LQUA.
I got that fine working code:
SELECT ma~matnr ma~mtart ma~ernam ma~ersda ma~laeda
de~maktx as maktx_de fr~maktx as maktx_fr it~maktx as maktx_it
FROM mara as ma
LEFT JOIN MAKT as de ON de~matnr = ma~matnr AND de~spras = 'DE'
LEFT JOIN MAKT as fr ON fr~matnr = ma~matnr AND fr~spras = 'FR'
LEFT JOIN MAKT as it ON it~matnr = ma~matnr AND it~spras = 'IT'
INTO CORRESPONDING FIELDS OF TABLE g_it_material
WHERE ma~ernam IN so_ERNAM
AND ma~laeda IN so_LAEDA
AND ma~matnr IN so_MATNR.
and it works fine. What did you say about multi-leftjoins?

Select element where condition is met on all joined elements

I found it hard labeling this question with an appropriate title but maybe thats because i dont know how to accomplish my goal.
I have the following database layout:
table: folders
columns: folderId
table: files
columns: fileId, authorId
table: file_relation
columns: folderId, fileId
table: authors
columns: authorId, surname, firstName
What i try to achieve is listing all folder that only contain files from author X Y (surname = X, firstName = Y). My approach was like this:
SELECT f.folderId FROM `folders` f
INNER JOIN `file_relation` rel ON rel.folderId = f.folderId
INNER JOIN `files` fls ON fls.fileId = rel.fileId
INNER JOIN `authors` a ON a.authorId = fls.authorId
WHERE a.surname = X and a.firstName = Y;
Now that lists me all the folders where there are files from said author. But how do i now restrict them to only get listed if there is no other file?
It's better to use aggregate function instead of subquery. The query will look like this:
SELECT f.folderId,
COUNT(fls.fileId) as filesCount,
SUM(a.surname = 'X' and a.firstName = 'Y') as authorFilesCount
FROM `folders` f
INNER JOIN `file_relation` rel ON rel.folderId = f.folderId
INNER JOIN `files` fls ON fls.fileId = rel.fileId
INNER JOIN `authors` a ON a.authorId = fls.authorId
GROUP BY f.folderId
HAVING filesCount = authorFilesCount;

need replace id_parent on name in SQL Server

I have a query:
SELECT Strtbmovie.id_parent,
Strtbuser.Name as 'Name',
Strtbmovie.Name as Movie,
sum(Strtbmovieatt.CountBeginWatch),
sum(Strtbmovieatt.CountEndWatch),
sum(Strtbmovieatt.CountBeginTest),
sum(Strtbmovieatt.CountEndTest),
max(Strtbmovieatt.CountTest)
FROM Strtbmovie
INNER JOIN Strtbmovieatt
ON Strtbmovie.Idmovie = Strtbmovieatt.Idmovie
INNER JOIN Strtbuser
ON Strtbmovieatt.Iduser = Strtbuser.Iduser
GROUP BY Strtbmovie.Name,
Strtbuser.Name,
Strtbmovie.id_parent
How to replace in this query columns id_parent on name knowing id_parent?
You have to add an extra INNER JOIN with the Strtbmovie table ON Strtbmovie.id_parent = StrtbmovieParent.Idmovie and the group by the actual name as follows:
SELECT StrtbmovieParent.Name as ParentName,
Strtbuser.Name as 'Name',
Strtbmovie.Name as Movie,
sum(Strtbmovieatt.CountBeginWatch),
sum(Strtbmovieatt.CountEndWatch),
sum(Strtbmovieatt.CountBeginTest),
sum(Strtbmovieatt.CountEndTest),
max(Strtbmovieatt.CountTest)
FROM Strtbmovie
INNER JOIN Strtbmovieatt
ON Strtbmovie.Idmovie = Strtbmovieatt.Idmovie
INNER JOIN Strtbuser
ON Strtbmovieatt.Iduser = Strtbuser.Iduser
INNER JOIN Strtbmovie as StrtbmovieParent
ON Strtbmovie.id_parent = StrtbmovieParent.Idmovie
GROUP BY Strtbmovie.Name,
Strtbuser.Name,
StrtbmovieParent.Name