DB2 SQL for Join: Duplicate Table Designator B Error - sql

I have a ColdFusion query like this:
SELECT a.Description, b.MCDL01
FROM ( SELECT GMDL01 as Description from F0901_LB where GMMCU = '950ALDA77') a, F0006_LA b
Inner Join b on a.GMMCU = b.MCMCU
Basically, the value of GMMCU in F0901_LB table exists as MCMCU in F0006_LA table. And I need to grab all matching MCDL01 from the F0006_LA table. But my join above gives error: Duplicate table designator B not valid.
BTW, I am not fully sure an Inner Join is needed but trying it to see what happens.
Any ideas?
Thanks.

You have two different syntax for joins here you want this (preferred):
SELECT a.Description, b.MCDL01
FROM ( SELECT GMDL01 as Description, GMMCU
from F0901_LB
where GMMCU = '950ALDA77') a
Inner Join F0006_LA b on a.GMMCU = b.MCMCU
this is the old way of doing it which you might have seen, but is not the best way to do it:
SELECT a.Description, b.MCDL01
FROM ( SELECT GMDL01 as Description, GMMCU
from F0901_LB
where GMMCU = '950ALDA77') a, F0006_LA b
where a.GMMCU = b.MCMCU

Just do it?
SELECT a.MCDL01, b.GMDL01 Description
FROM F0006_LA a inner join F0901_LB b on a.MCMCU = b.GMMCU and b.GMMCU = '950ALDA77'

Related

SQL Query Select from 1 table and return data based on 2 columns

I'm working on a SQL query where I have data stored in table r. Based on the columns of give or receive, I need to store the data in a temp table to then store in a new table.
Essentially, the data is pulling from 3 tables and the end goal is to get the binary code for an image and store to display in .net
I'm trying to figure out the multi select at this time
So give or receive in r equals Username in S, display all the data that pertains and get the image if employee equals employee
I've tried a good amount of code and I feel like an OR would do the trick but it doesn't seem to.
Thanks for any help you may provide.
SELECT
r.id, r.give, r.[receive], r.[type], r.[description], r.isApproved,
r.photoGive, r.photoReceive
INTO
#temp2
FROM
Intranet.dbo.Recgonize r
SELECT s.Employee, s.Username
INTO #temp3
FROM Vision7.dbo.SEUser s
SELECT p.Photo, p.Employee
INTO #temp4
FROM Vision7.dbo.EMPhoto p
SELECT *
FROM #temp2 AS a
INNER JOIN #temp3 b ON a.give = b.Username
INNER JOIN #temp4 c ON b.Employee = c.Employee
DROP TABLE #temp2
DROP TABLE #temp3
DROP TABLE #temp4
Why are you using temporary tables for this? These just make code harder to debug and maintain, more expensive to run, and more complicated to understand.
JOINs will work without temporary tables. But you do have to additional logic to get the give and receive values in separate columns, so more JOINs are necessary:
SELECT r.id, r.give, r.[receive], r.[type], r.[description],
r.isApproved, r.photoGive, r.photoReceive,
pg.Photo as give_photo, pg.Employee as give_employee,
pr.Photo as receive_photo, pr.Employee as receive_employee
FROM Intranet.dbo.Recognize r LEFT JOIN
Vision7.dbo.SEUser sg
ON r.give = sg.Username LEFT JOIN
Vision7.dbo.SEUser sr
ON r.receive = sr.Username LEFT JOIN
Vision7.dbo.EMPhoto pg
ON sg.Employee = pg.Employee LEFT JOIN
Vision7.dbo.EMPhoto pr
ON sr.Employee = pr.Employee
Try with a single script as below-
SELECT a.id,
a.give,
a.[receive],
a.[type],
a.[description],
a.isApproved,
a.photoGive,
a.photoReceive,
b.Employee,
b.Username,
c.Photo,
c.Employee
FROM Intranet.dbo.Recgonize A
INNER JOIN Vision7.dbo.SEUser B ON a.give = b.Username
INNER JOIN Vision7.dbo.EMPhoto C ON b.Employee = c.Employee;
You may want try following:
-- Include only required columns select rather than "*"
SELECT *
FROM #temp2 AS a
INNER JOIN #temp3 b ON a.give = b.Username
INNER JOIN #temp3 b2 ON a.receive = b2.Username
INNER JOIN #temp4 c ON b.Employee = c.Employee
INNER JOIN #temp4 c2 ON b2.Employee = c2.Employee
or
-- Include only required columns in select rather than "*"
SELECT *
FROM
(select r.id, r.give as UserName, r.[type], r.[description], r.isApproved, r.photoGive from #temp2
union
select r.id, r.[receive], r.[type], r.[description], r.isApproved, r.photoReceive from #temp2
) AS a
INNER JOIN #temp3 b ON a.UserName = b.Username
INNER JOIN #temp4 c ON b.Employee = c.Employee
If temp tables are NOT used for necessary need by application, same logic can work with actual tables, just replace #Temp2, #Temp3, #Temp4 with appropriate table names.

Displaying one column from inner query

Please help me solve this:
SELECT a.prs_code,
a.cc,
b.description
FROM idp_inpadoc_prs_cc a,
idp_inpadoc_cat_desc b
WHERE a.ID = b.ID
AND a.prs_code IN (SELECT prs_code
FROM tls221_inpadoc_prs c,
tls201_appln d
WHERE c.appln_id = d.appln_id
AND c.appln_id IN ( '1', '2' ));
In this query, along with prs_code, cc, description, I also want to display the corresponding appln_id. How can I do this? Kindly help. Thanks! :)
so when you start looking at transforming this to joins this should do the same as your query because appln_id is on both the tls221_inadoc_prs and the tsl201_appln tables so you don't actually need the later table if the tls221_inadoc_prs.appln_id is a foreign key to tls201_appln.appln_id meaning that if the value is in tls221_inadoc_prs then it also must be in tls201_appln
SELECT a.prs_code,
a.cc,
b.description
,c.appln_id
FROM
idp_inpadoc_prs_cc a
INNER JOIN idp_inpadoc_cat_desc b
ON a.ID = b.ID
INNER JOIN tls221_inpadoc_prs c
ON a.prs_code = c.prs_code
AND d.appln_id IN (1,2)
If it is not a foreign key you can simply add another join as well:
SELECT a.prs_code,
a.cc,
b.description
,c.appln_id
FROM
idp_inpadoc_prs_cc a
INNER JOIN idp_inpadoc_cat_desc b
ON a.ID = b.ID
INNER JOIN tls221_inpadoc_prs c
ON a.prs_code = c.prs_code
AND c.appln_id IN ( '1', '2' )
INNER JOIN tls201_appln d
ON c.appln_id = d.appln_id

Error message - Every derived table must have its own alias

I have this SQL Syntax but it's not working and receive this error:
"#1248 - Every derived table must have its own alias".
Could you help me?
SELECT *
FROM produse_comenzi
JOIN comenzi ON comenzi.id_comanda = produse_comenzi.id_comanda
JOIN (SELECT DISTINCT numar_factura FROM facturi)
ON facturi.id_comanda = comenzi.id_comanda
In the second join you are using a subquery but you haven't given the result an alias, i.e. something to identify the result by
SELECT *
FROM produse_comenzi
JOIN comenzi
ON comenzi.id_comanda = produse_comenzi.id_comanda
JOIN (SELECT DISTINCT numar_factura FROM facturi) -- has no alias
ON facturi.id_comanda = comenzi.id_comanda
you should do
SELECT *
FROM produse_comenzi
JOIN comenzi
ON comenzi.id_comanda = produse_comenzi.id_comanda
JOIN (SELECT DISTINCT numar_factura, id_comanda FROM facturi) AS facturi
ON facturi.id_comanda = comenzi.id_comanda
You must add an alias to each subquery that's being treated as a table:
SELECT *
FROM produse_comenzi
JOIN comenzi ON comenzi.id_comanda = produse_comenzi.id_comanda
JOIN (SELECT DISTINCT numar_factura FROM facturi) x
ON x.id_comanda = comenzi.id_comanda
Here I have named the result set x and referred to that in the join condition. You can change "x" to whatever you like.
This should fix it:
(there is a need in SQL to distinguish between different Resultset from selects)
SELECT *
FROM produse_comenzi AS table_1
JOIN comenzi AS table_2
ON table_2.id_comanda = table_1.id_comanda
JOIN (SELECT DISTINCT numar_factura FROM facturi AS table_3)
ON table_3.id_comanda = table_2.id_comanda

Nice way to query for cross table

I have 3 tables :
A(k1,A) B(k1,k2,B) and C(k2,C).
I want to filter all A that satisfy C.k2 condition. in this example, I must filter go through table B : filter all B that have same k1 attribute with A , and filter all C k2 attribute with B (that I have filtered before).
I have an ugly way to do this :
select * from A where k1 in (select * .....) // it looks ugly and hard to trace
I have though about using join function, but don't really know how to do this. Please tell me a best way for this query.
Thanks :)
Try this Query.
select * from A
join b on a.k1 = b.k1
join c on c.k2 = b.k2
Explanation for JOIN
It sounds pretty easy:
select * from A
join B on B.k1 = A.k1
join C on C.k2 = B.k2
If I'm reading your table structure correctly, the join logic would be like this:
SELECT *
FROM A
JOIN B
ON A.k1 = B.k1
JOIN C
ON B.k2 = C.k2
You could of course then specify in the SELECT which table you want values from, ie:
SELECT A.*,C.*
Or Limit results with WHERE ie:
WHERE C.C = 'something'
Using join to retrieve data from two or more tables. see Join Fundamentals
SELECT A.k1,B.k2
FROM A
JOIN B ON A.k1 = B.k1
JOIN C ON B.k2 = C.k2

Joining two tables on a key and then left outer joining a table on a number of criteria

I'm attempting to join 3 tables together in a single query. The first two have a key so each entry has a matching entry. This joined table will then be joined by a third table that could produce multiple entries for each entry from the first table (the joined ones).
select * from
(select a.bidentifier, a.bsession, a.symbol, b.jidentifier, b.JSession
from trade_monthly a, trade_monthly_second b
where
a.bidentifier = b.jidentifier AND
a.bsession = b.JSession)
left outer join
trade c
on c.symbol = a.symbol
order by a.bidentifier, a.bsession, a.symbol, b.jidentifier, b.JSession, c.symbol
There will be more criteria (not just c.symbol = a.symbol) on the left outer join but for now this should be useful. How can I nest the queries this way? I'm gettin gan SQL command not properly ended error.
Any help is appreciated.
Thanks
For what I know every derived table must be given a name; so try something like this:
SELECT * FROM
(SELECT a.bidentifier, ....
...
a.bsession = b.JSession) t
LEFT JOIN trade c
ON c.symbol = t.symbol
ORDER BY t.bidentifier, ...
Anyway I think you could use a simpler query:
SELECT a.bidentifier, a.bsession, a.symbol, b.jidentifier, b.JSession, c.*
FROM trade_monthly a
INNER JOIN trade_monthly_second b
ON a.bidentifier = b.jidentifier
AND a.bsession = b.JSession
LEFT JOIN trade c
ON c.symbol = a.symbol
ORDER BY a.bidentifier, a.bsession, a.symbol, b.jidentifier, b.JSession, c.symbol
Try this:
SELECT
`trade_monthly`.`bidentifier` AS `bidentifier`,
`trade_monthly`.`bsession` AS `bsession`,
`trade_monthly`.`symbol` AS `symbol`,
`trade_monthly_second`.`jidentifier` AS `jidentifier`,
`trade_monthly_second`.`jsession` AS `jsession`
FROM
(
(
`trade_monthly`
JOIN `trade_monthly_second` ON(
(
(
`trade_monthly`.`bidentifier` = `trade_monthly_second`.`jidentifier`
)
AND(
`trade_monthly`.`bsession` = `trade_monthly_second`.`jsession`
)
)
)
)
JOIN `trade` ON(
(
`trade`.`symbol` = `trade_monthly`.`symbol`
)
)
)
ORDER BY
`trade_monthly`.`bidentifier`,
`trade_monthly`.`bsession`,
`trade_monthly`.`symbol`,
`trade_monthly_second`.`jidentifier`,
`trade_monthly_second`.`jsession`,
`trade`.`symbol`
Why don't you just create a view of the two inner joined tables. Then you can build a query that joins this view to the trade table using the left outer join matching criteria.
In my opinion, views are one of the most overlooked solutions to a lot of complex queries.