Is there a smarter way to do this SQL so I do not have many subselects?
SELECT
emp1,
emp3,
mtrl,
kat,
(select f_desc from tbllangdesc where tbllangdesc.id = tblPart.id and tbllangdesc.rownr = 3 and tbllangdesc.langcode = 'DK') LangDescDK3,
(select f_desc from tbllangdesc where tbllangdesc.id = tblPart.id and tbllangdesc.rownr = 4 and tbllangdesc.langcode = 'DK') LangDescDK4,
(select f_desc from tbllangdesc where tbllangdesc.id = tblPart.id and tbllangdesc.rownr = 3 and tbllangdesc.langcode = 'EN') LangDescEN3,
(select f_desc from tbllangdesc where tbllangdesc.id = tblPart.id and tbllangdesc.rownr = 3 and tbllangdesc.langcode = 'DE') LangDescDE3,
(select f_desc from tbllangdesc where tbllangdesc.id = tblPart.id and tbllangdesc.rownr = 3 and tbllangdesc.langcode = 'FR') LangDescFR3,
(select f_desc from tbllangdesc where tbllangdesc.id = tblPart.id and tbllangdesc.rownr = 3 and tbllangdesc.langcode = 'FI') LangDescFI3,
(select f_desc from tbllangdesc where tbllangdesc.id = tblPart.id and tbllangdesc.rownr = 3 and tbllangdesc.langcode = 'ZH') LangDescZH3,
(select f_desc from tbllangdesc where tbllangdesc.id = tblPart.id and tbllangdesc.rownr = 3 and tbllangdesc.langcode = 'LI') LangDescLI3,
(select f_desc from tbllangdesc where tbllangdesc.id = tblPart.id and tbllangdesc.rownr = 3 and tbllangdesc.langcode = 'NO') LangDescNO3,
(select f_desc from tbllangdesc where tbllangdesc.id = tblPart.id and tbllangdesc.rownr = 3 and tbllangdesc.langcode = 'PL') LangDescPL3,
(select f_desc from tbllangdesc where tbllangdesc.id = tblPart.id and tbllangdesc.rownr = 3 and tbllangdesc.langcode = 'PT') LangDescPT3,
(select f_desc from tbllangdesc where tbllangdesc.id = tblPart.id and tbllangdesc.rownr = 3 and tbllangdesc.langcode = 'RU') LangDescRU3
FROM
tblEmp,
tblPart
WHERE
tblEmp.id = tblPart.id
asked
06 Mar '16, 09:37
Rolle
558●49●51●61
accept rate:
0%
Just to understand:
Do you need all these language-dependent columns in the same query, or do you later filter only those you need for the current connection/current language, what ever? (I'm asking since it is a common requirement to return a description in the "one and only appropriate language" for the current client but that seems not to be the case here...)
And do you require two columns for "DK" and only one for the other languages?
The languages which is typed in all this subselects is all Possible languages that can be used. Used languages codes is in tables tblLang. This can I type with
select distinct lang_lang from tblLang
. This will return all used language codes like IT, DKK etc.. Wich would be the best. Instead of use many subselects I hope I can use it in a smarter way. DKK is on row 1,3 and 4. All other languages is on row 1 and 3.I still do not really understand what you are trying to achieve - do you need a result set with all these languages, or just one with the chosen/used language? And if several languages, do you need these to be returned in the same row as in your sample, or could they be returned in different rows?
It would be probably easier for others to make suggestions if you could please explain what result set you would like to have, so please just show us what the resulting data should look like...
DROP TABLE IF EXISTS tbllangdesc;
Should be doing in a different way (smarter way with smaller code) than subselect I done now
While I still do not understand what exactly you are really trying to achieve (or what you will do with that particular result set), your sample points out that you need to turn values of rows of some table (like 'EN' and '1') into column names of a result set (such as "LangDescEN1").
If so, I agree with Breck that you could try to use the (quite complex) PIVOT clause if you are using v17, or could stay with your current solution, although that may mean you will have to adapt the query if there are more languages/rownrs to handle...
I'm still not sure whether you could also try to use a derived table for all those language IDs and join that with other data (and that way get a solution able to cope easily with more language entries) - but as stated, I do not really understand what it's all good for.
What I am asking for is a kind of Pivot for this. I think there is a more dynamic solution with pivot and less code is needed. We use SA16. I wrote wrong in my previous post ... I get all used unique language codes by