Displaying one column from inner query - sql

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

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.

Query to update 1 column with no condition and another column only if certain condition is met

I have the below SQL Server query to update column type in tblTicket:
UPDATE A set type=B.type
FROM tblTicket A
JOIN (select b.tblTicket_id,a.type
from tblP a
JOIN tblA b ON a.allocation_id = b.id
WHERE b.tblTicket_id =#tblTicket_id Group By b.tblTicket_id,a.type) B
ON A.id=b.tblTicket_id
I also want to update column bid in table tblTicket, but only if a.name = "ML":
UPDATE A set bid = B.id
FROM tblTicket A
JOIN (select b.tblTicket_id,f.id
FROM tblP a
JOIN tblA b ON a.allocation_id = b.id
JOIN FIRM f ON f.firm = a.firm
WHERE b.tblTicket_id =#tblTicket_id and a.name = 'ML' Group By b.tblTicket_id,f.id) B
ON A.id=b.tblTicket_id
Can I combine the 1st query with the 2nd query above ?
Thank you.
Using CASE you can update a column if a certain condition is satisfied, otherwise you can "set" that column with the same value.
A "best practice" tip: avoid using same aliases (even switching from uppercase to lowercase and vice-versa) in your nested queries.
UPDATE A set type=B.type,
bid = case when a.name = 'ML' then B.id else bid end
FROM tblTicket A
JOIN (select b.tblTicket_id,a.type
from tblP a
JOIN tblA b ON a.allocation_id = b.id
WHERE b.tblTicket_id =#tblTicket_id Group By b.tblTicket_id,a.type) B
ON A.id=b.tblTicket_id

DB2 SQL for Join: Duplicate Table Designator B Error

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'

join graph disconnect ORACLE

The query below won't run as I have a join graph disconnect. As far as I understand when you alias a table you have to put it first in the join condition, but when i do so I still get the same error, any suggestions?
select
date1.LABEL_YYYY_MM_DD as Some_Label1,
A.Some_Field as Some_Label2,
round(sum(D.Some_Field3)/count (*),0)as Some_Label3
from Table_A A
inner JOIN Table_B B ON (A.some_key = B.some_key)
inner JOIN date_time date1 ON (A.START_DATE_TIME_KEY = date1.DATE_TIME_KEY)
left outer join Table_C C on(C.some_GUID = A.some_ID)
left outer join Table_D D on(D.a_ID = C.a_ID)
where
(1=1)
and date1.LABEL_YYYY_MM_DD ='2015-03-30'
and D.blah ='1'
group by
date1.LABEL_YYYY_MM_DD,
A.Some_Field
order by
date1.LABEL_YYYY_MM_DD
;
Based on my comment above I should change the first inner join to B.some_key = A.some_key and so on however I still get the disconnect...
according to the Oracle documentation (http://docs.oracle.com/javadb/10.8.3.0/ref/rrefsqlj18922.html) there is space required between ON and the boolean expression

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