SQL DB2 Conditional Select - sql

I'm working on a DB2 stored procedure and am having a little trouble getting the results I want. The problem with the following query is that it does not return rows from table A that don't pass the final where clause. I would like to receive all rows from table A that meet the first WHERE clause (WHERE A.GENRC_CD_TYPE = 'MDAA'). Then, add an email column from table B for each of those rows(WHERE (A.DESC) = B.MATL_PLNR_ID).
SELECT A.GENRC_CD,
A.DESC_30,
A.DOL,
A.DLU,
A.LU_LID,
B.EMAIL_ID_50
FROM GENRCCD A,
MPPLNR B
WHERE A.GENRC_CD_TYPE = 'MDAA'
AND (A.DESC_30) = B.MATL_PLNR_ID;
Any help is much appreciated, thanks!

Then what you need is a LEFT JOIN:
SELECT A.GENRC_CD,
A.DESC_30,
A.DOL,
A.DLU,
A.LU_LID,
B.EMAIL_ID_50
FROM GENRCCD A LEFT JOIN
MPPLNR B on A.DESC_30=B.MATL_PLNR_ID
WHERE A.GENRC_CD_TYPE = 'MDAA'

Related

Record exist in one DB2 table but not in another table

I have 2 DB2 tables. I want to find out records that are in table A is not Table B with the following condition
I wrote this query and it is not working
SELECT A.CL_BATCH_DEPT,
A.CL_TRANS_CODE, A.CL_CUR_DOC_NO
FROM DBPA60AC.TB_ACCOUNT_EVENT A
LEFT JOIN DBPA60AC.TB_DOCUMENT B ON A.CL_CUR_DOC_NO = B.CL_DOCNO
WHERE A.CL_BATCH_DEPT = 'R07' AND A.CL_TRANS_CODE = '210'
AND A.CL_CUR_DOC_NO = "PI%" AND
B CL_DOCNO IS NULL
I am guessing that you want:
SELECT e.*
FROM DBPA60AC.TB_ACCOUNT_EVENT e LEFT JOIN
DBPA60AC.TB_DOCUMENT d
ON e.CL_CUR_DOC_NO = d.CL_DOCNO
WHERE e.CL_BATCH_DEPT = 'R07' AND
e.CL_TRANS_CODE = '210' AND
e.CL_CUR_DOC_NO LIKE 'PI%' AND
d.CL_DOCNO IS NULL;
That is, the comparison to PI% suggests that you really want LIKE.
Note that I also changed the aliases so they are meaningful.

Select information from different table SQL

SELECT Boeking.reisnummer, (aantal_volwassenen + aantal_kinderen) AS totaal_reizigers
FROM Boeking
WHERE Boeking.reisnummer = Reis.Reisnummer
AND Reis.Reisnummer = Reis.Bestemmingscode;
Table 1 (Boeking) has aantal_volwassen and aantal_kinderen, and Reisnummer.
Table 2 (Reis) has Reisnummer and Bestemmingscode.
I have to show the total of aantal_volwassenen and aantal_kinderen. But i also have to show Reis.bestemmingscode which comes from a different table. Currently i have to enter a parameter, how can i solve this?
You need to specify all the tables in the FROM part of your query. The tables should then be joined (JOIN) to get the data you need.
SELECT Boeking.reisnummer
,(aantal_volwassenen + aantal_kinderen) AS totaal_reizigers
,Reis.Bestemmingscode
FROM Boeking INNER JOIN Reis
ON Boeking.reisnummer = Reis.Reisnummer

How can I do a SQL join to get a value 4 tables farther from the value provided?

My title is probably not very clear, so I made a little schema to explain what I'm trying to achieve. The xxxx_uid labels are foreign keys linking two tables.
Goal: Retrieve a column from the grids table by giving a proj_uid value.
I'm not very good with SQL joins and I don't know how to build a single query that will achieve that.
Actually, I'm doing 3 queries to perform the operation:
1) This gives me a res_uid to work with:
select res_uid from results where results.proj_uid = VALUE order by res_uid asc limit 1"
2) This gives me a rec_uid to work with:
select rec_uid from receptor_results
inner join results on results.res_uid = receptor_results.res_uid
where receptor_results.res_uid = res_uid_VALUE order by rec_uid asc limit 1
3) Get the grid column I want from the grids table:
select grid_name from grids
inner join receptors on receptors.grid_uid = grids.grid_uid
where receptors.rec_uid = rec_uid_VALUE;
Is it possible to perform a single SQL that will give me the same results the 3 I'm actually doing ?
You're not limited to one JOIN in a query:
select grids.grid_name
from grids
inner join receptors
on receptors.grid_uid = grids.grid_uid
inner join receptor_results
on receptor_results.rec_uid = receptors.rec_uid
inner join results
on results.res_uid = receptor_results.res_uid
where results.proj_uid = VALUE;
select g.grid_name
from results r
join resceptor_results rr on r.res_uid = rr.res_uid
join receptors rec on rec.rec_uid = rr.rec_uid
join grids g on g.grid_uid = rec.grid_uid
where r.proj_uid = VALUE
a small note about names, typically in sql the table is named for a single item not the group. thus "result" not "results" and "receptor" not "receptors" etc. As you work with sql this will make sense and names like you have will seem strange. Also, one less character to type!

Multirow return Query MSACCESS

I need to run a query that returns all events that has Electrophysiology Study But don't have %Ablation%
So in this case I should receive only events 608 and 612. The table has two columns SS_EVENT_EP_ID and STUDYPROCEDURE
Screenshot of tables
https://plus.google.com/photos/105880715521229058253/albums/6026235567691005409/6026235568072337762
I just saw your tables. Yes you are correct, you need a subquery.
What you need is the EXISTS operator as well.
SELECT EP.SS_Event_EP_ID, EP.StudyProcedure, Event_EP.EventDate
FROM Event_EP INNER JOIN EP_Procedure As EP
ON Event_EP.SS_Event_EP_ID = EP.SS_Event_EP_ID
WHERE EP.StudyProcedure = "Electrophysiology study"
AND (Event_EP.EventDate Between #1/1/2004# And #12/31/2012#)
AND NOT EXISTS (
Select SS_Event_EP_ID from EP_Procedure As EP_I
Where EP_I.SS_Event_EP_ID = EP.SS_Event_EP_ID
And EP_I.StudyProcedure Like "%blation%"
)

SQL Server: select only if nested select returns something

I am trying to set the following query so that it only selects any data from the DaysMov table (A) if the nested Select finds a matching date in the Calendar table (B).
Currently in such a case it just returns nothing for "dayMov" but still selects the other data from table A.
Can someone tell me how I can achieve this, e.g. by using Where or Case ?
SELECT
(
SELECT B.calendar_DT
FROM Calendar B
WHERE B.year_id = #selYear AND B.month_of_year = A.calMonth AND B.day_of_week = A.calDay AND (B.day_of_month BETWEEN A.calFrom AND A.calTill)
) AS dayMov,
A.countries,
A.regions,
'variable' AS validity,
A.name,
A.desc,
'' AS mode
FROM DaysMov A
WHERE A.mode = ''
Many thanks for any help with this, Tim.
You should be able to do the same thing with an inner join, like this:
SELECT
B.calendar_DT AS dayMov,
A.countries,
A.regions,
'variable' AS validity,
A.name,
A.desc,
'' AS mode
FROM DaysMov A
INNER JOIN Calendar B ON B.year_id = #selYear AND B.month_of_year = A.calMonth AND B.day_of_week = A.calDay AND (B.day_of_month BETWEEN A.calFrom AND A.calTill)
WHERE A.mode = ''
This query will not produce a row when Calendar does not have a match on the specified condition. Assuming that the query from your post has worked (except that it produced some rows that you did not want) the query above shouldn't produce additional rows, because the subquery on the same condition returned at most a single result.