Join Query to Display info from 2 tables - sql

this is the query to join 2 tables but I keep getting an error message near the JOIN
select CHKNBR, pidamt, copied_from_batdat, BATDAT, SEQNBR from UMCDTL
where copied_from_batdat between '10/11/2013' and '11/4/2013'
and batseq between '001' and '100' and SEQNBR between '01' and '100'
join
UMCFIL on BATDAT=SEQNBR and seqnbr=batadat
The output needs to display the
Original Claim Number
Original Paid Amount
Original Check Number
Thanks

If you want to use 'JOIN' specifically, then it needs to be inside of your 'FROM' clause.
Select...
From
table1 JOIN table2 ON Table1.key = Table2.key
Where...
If you want to use your 'WHERE' clause to join the tables, then relate the key fields there instead:
Select...
FROM
table1, table2
WHERE
table1.key = table2.key
...
Here's a tutorial on using the basic joins if want to understand it a little better: http://www.w3schools.com/sql/sql_join.asp

Try something like:
SELECT CHKNBR, pidamt, copied_from_batdat, BATDAT, SEQNBR
FROM UMCDTL JOIN UMCFIL ON BATDAT=SEQNBR AND seqnbr=batadat
WHERE copied_from_batdat between '10/11/2013' AND '11/4/2013'
AND batseq BETWEEN 1 AND 100
AND SEQNBR BETWEEN 1 AND 100

Related

SQL - Union all / Inner join (with where clauses)

I'm trying to port a lot of old AS400 queries to SQL as I'm developing a GUI for reporting & updating records from the libraries in AS/400.
With that, I've encountered a query that has quite a few steps and the joining of tables is throwing me a little.
The below is a query that does what I want for retrieving data from two of the three physical files on the AS400.
select substr(clntpm,1,2) as clntwf, substr(pal#pm,1,10) as pal#wf,
substr(clsspm,1,2) as clsswf, clsqpm * 1 as clsqwf
from warpall
where locnpm <> 'ASSEMBLED PALLET'
and commpm <> 'ASSEMBLED PALLET'
and clsqpm <> 0
union all
select substr(clntpq,1,2) as clntwf, substr(pal#pq,1,10) as pal#wf,
substr(clsspq,1,2) as clsswf, clsqpq * 1 as clsqwf
from warpalq
where clsqpq <> 0
Now I want to add a third table... As I understand, I want to do an inner join on the result of the above union.
select * from (
<old query>
) t9
inner join t3 on <field> where t3.field = t9.field
Is what I tried, but I'm clearly getting the syntax wrong as I get an error about the where clause being unexpected.
Can anyone shed some light please?
try this
select
* from (<old query>) as t9
inner join
t3
on
t3.field = t9.field

4 Tables Joining in SQL

I have some preps/views which im using in my final select clause in the following way :
select .......
from gr
full outer join go
on gr.ART_CONC=go.ART_CONC and
gr.pt=go.pt and
gr.p_act=go.p_act and
gr.month_id=go.month_id and
gr.art_desc=go.art_desc
Now i want to also include the following in my select clause, but im confused how the syntax and join would really go
sim ab join prep1
on ab.fp_num3 = article_num AND
substr(ab.fp_num2,-2,2) = substr(pt10,-2,2)
where fp_data_kind='SEC PFE_ND_GO'
and fp_data_valid_from between to_date(:par_date_from, 'YYYY.MM.DD HH24:MI:SS') and to_date(:par_date_to, 'YYYY.MM.DD HH24:MI:SS')
Note : sim, prep1,gr and go are 4 tables/preps that already exist.
Thanks in Advance !
In this, table1/2/3/4 are your tables. join_val's are the values you are joining on. I did not fully understand your question or what you wanted so this one way to go about doing 4 joins.
select t1.something1
,t1.something2
,t1.something3
,t2.something1
,t2.something2
,t2.something3
,t3.something1
,t3.something2
,t3.something3
,t4.something1
,t4.something2
,t4.something3
from table1 t1
full outer join table2 t2
on t1.join_val = t2.join_val
......
full outer join table3 t3
on t1.join_val = t3.join_val
......
full outer join table4 t4
on t.join_val = t3.join_val

How to exclude records based on second table (using JOIN)

I think this is simple, but not so today...
I have two tables The first has a column with data from the second.
T1 has Column1 value = "Created"
T2 has Column1 Value = "Created" and Column2 Value = "OPEN"
So the idea is to return every row from table 1 where column 2 of table 2 = "OPEN"
This si the current SELECT
SELECT tblCustIncidents.IncidentID, tblCustIncidents.EntryDateTime, tblCustIncidents.Title, tblCustIncidents.StatusType, tblCustIncidents.Summary, tblMaintStatusTypes.StatusDescr
FROM tblCustIncidents LEFT JOIN
tblMaintStatusTypes
ON tblCustIncidents.StatusType = tblMaintStatusTypes.StatusType;
I thought I could just use WHERE but I am not clear on the location of the WHERE or even if it will work... I've been looking more than a little while. I'll keep looking and hope someone can point me tot he right method/answer/post etc. Thanks.
In the screenshot of the returned data the last column is in both tables. The second table has a column indictaing if the item is open or closed. SO I would want all records in the snippet NOT to appear... I have tried this Select statement...
SELECT tblCustIncidents.IncidentID, tblCustIncidents.EntryDateTime, tblCustIncidents.Title, tblCustIncidents.StatusType, tblCustIncidents.Summary, tblMaintStatusTypes.StatusDescr
FROM tblCustIncidents LEFT JOIN tblMaintStatusTypes ON tblCustIncidents.StatusType = tblMaintStatusTypes.StatusType
WHERE tblMaintStatusTypes.OpenOrClosedType = 'Open';
OK I figured out access is picky... so I have tried this...
SELECT tblCustIncidents.IncidentID,
tblCustIncidents.EntryDateTime,
tblCustIncidents.Title,
tblCustIncidents.StatusType,
tblCustIncidents.Summary,
tblMaintStatusTypes.StatusDescr
FROM tblCustIncidents
inner join (SELECT tblCustIncidents.IncidentID,
tblCustIncidents.EntryDateTime,
tblCustIncidents.Title,
tblCustIncidents.StatusType,
tblCustIncidents.Summary,
tblMaintStatusTypes.StatusDescr
FROM tblCustIncidents)
ON (tblCustIncidents.StatusType = tblMaintStatusTypes.StatusType AND tblMaintStatusTypes.OpenOrClosedType = 'Open');
SELECT t1.*
FROM table1 t1
JOIN table2 t2
ON t1.Column1=t2.Column1
WHERE t2.column2='OPEN'
Based on your query you just need to add clause to the join and than change join from left to inner, otherwise you will still get all rows from table 1.
SELECT tblCustIncidents.IncidentID
,tblCustIncidents.EntryDateTime
,tblCustIncidents.Title
,tblCustIncidents.StatusType
,tblCustIncidents.Summary
,tblMaintStatusTypes.StatusDescr
FROM tblCustIncidents
INNER JOIN tblMaintStatusTypes
ON tblCustIncidents.StatusType = tblMaintStatusTypes.StatusType
WHERE tblMaintStatusTypes.Column2 = 'OPEN'

Getting a ton of duplicates? Left Join incorrect?

Here is a sample of my query:
SELECT ...
TRIM(eml.fxeml1) as "Email Address"
FROM pfship
LEFT JOIN mstfxem fax
ON fax.fxco=pfship.cshco AND fax.fxdiv=pfship.cshdiv AND fax.fxsold=pfship.cshsld
AND fax.fxtype='C' AND TRIM(fax.fxfax1) <> '' AND fax.fxdept LIKE '*F%'
LEFT JOIN mstfxem eml
ON eml.fxco=pfship.cshco AND eml.fxdiv=pfship.cshdiv AND eml.fxsold=pfship.cshsld
AND eml.fxtype='C' AND TRIM(eml.fxeml1) <> '' AND eml.fxdept LIKE '*E%'
WHERE ((pfship.cshco || '/' || pfship.cshdiv) = ?)
AND (? = '*ALL' OR CAST(cshsld AS CHAR(15)) = ?)
AND ...
ORDER BY TRIM(cshnme)
This query should return 9 records. When I remove:
LEFT JOIN mstfxem fax
ON fax.fxco=pfship.cshco AND fax.fxdiv=pfship.cshdiv AND fax.fxsold=pfship.cshsld
AND fax.fxtype='C' AND TRIM(fax.fxfax1) <> '' AND fax.fxdept LIKE '*F%'
LEFT JOIN mstfxem eml
ON eml.fxco=pfship.cshco AND eml.fxdiv=pfship.cshdiv AND eml.fxsold=pfship.cshsld
AND eml.fxtype='C' AND TRIM(eml.fxeml1) <> '' AND eml.fxdept LIKE '*E%'
I get 9 records, but with it I get 360 records. What is wrong with my join?
Thanks
I think the problem is that 1 person can have many emails or faxes, how do I group the results into 1 string per record so I end up with only 9 records?
Nothing is wrong with your join, you have a one to many relationship between the tables.
Depending on what you need there are several techniques. First is the derived table. Write a query to pick out only one record from the one to many table (your requirements should specify which record to pick). Join to that instead. OR you may be able to put further conditions on the left join to get only one record. Some examples of these techniques:
select t1.field1, t2.email
from table1 t1
join (select myid, max(email) as email from table2 group by myid) t2
on t1.myid = t2.myid
Or
select t1.field1, t2.email
from table1 t1
Left join from table2 t2
on t1.myid = t2.myid and t2.active = 1
Or if you want all of the emails in a comma delimited list, that code is database specific and you would need to let us know which database backend you are using.

Join on tables with OR (repeating data) - SQL Server 2005

Firstly to put things into context I am trying to write a search on SQL Server 2005. Below is my table structure
Schema1.Table1
GUID
1
2
3
Schema2.Table2
GUID MAINTITLE
1 Water Monkies
2 Water Doggies
Schema3.Table3
GUID MAINTITLE
3 Water Hyrdas
Expected behavior is that the user will search for 'Water' and I have to retrieve all the GUID in Schema1.Table1 match them with entries in Schema2.Table2 and Schema3.Table3 where GUID in the list AND MAINTITLE like '%WATER%'
I have to achieve this using JOINS.
What I've done so far is:
select Schema1.Table1.GUID
from Schema1.Table1 JOIN Schema2.Table2 ON Schema1.Table1.GUID = Schema2.Table2.GUID
JOIN Schema3.Table3 ON Schema1.Table1.GUID = Schema3.Table3.GUID
but this returns an AND'ed result which gives me no results
I then tried
select distinct Schema1.Table1.GUID
from Schema1.Table1, Schema2.Table2, Schema3.Table3
where (Schema2.Table2.GUID=Schema1.Table1.GUID OR Schema3.Table3.GUID=Schema1.Table1.GUID
) AND (Schema2.Table2.MAINTITLE like '%water%' OR Schema3.Table3.MAINTITLE like '%water%')
but since this is an implied join it returns all rows of table2 where table3s' maintitle is like water too.
Can I haz some help plese?
it's not possible to do that with joins, you have to use unions. although i can't provide proof of this
select T1.GUID
from T1 join (T2 union all T3) as T on T1.GUID=T.GUID
where T.MAINTITLE like '%WATER%'
maybe
You can try using a LEFT JOIN and ISNULL
select Schema1.Table1.GUID,
ISNULL(Schema2.MAINTITLE, Schema3.MAINTITLE)
from Schema1.Table1 LEFT JOIN
Schema2.Table2 ON Schema1.Table1.GUID = Schema2.Table2.GUID LEFT JOIN
Schema3.Table3 ON Schema1.Table1.GUID = Schema3.Table3.GUID
WHERE ISNULL(Schema2.MAINTITLE, Schema3.MAINTITLE) LIKE '%blabla%'
A UNION ALL does seem like a better option, but this can work too.
Also have a look at using COALESCE (Transact-SQL) instead of ISNULL