Issue with my Left outer join query - sql

Here are my tables,
I'm trying to fetch the list of free bets with the details if the user has placed his bet or not. This is the query i have written to fetch the details for the same,
select DISTINCT tbl_CreateFreeBet.FreeBetID,
tbl_CreateFreeBet.FreeBetDescription,
tbl_CreateFreeBet.FreeBetAmount,
tbl_CreateFreeBet.TournamentID,
tbl_CreateFreeBet.MatchID,
tbl_UserFreeBets.UserForYes,
tbl_UserFreeBets.UserForNo,
tbl_UserFreeBets.UserForNoBets
from tbl_CreateFreeBet left outer join tbl_UserFreeBets
on tbl_CreateFreeBet.MatchID = 1 and
tbl_CreateFreeBet.MatchID = tbl_UserFreeBets.MatchID and
tbl_CreateFreeBet.FreeBetID = tbl_UserFreeBets.FreeBetID and
tbl_CreateFreeBet.TournamentID = tbl_UserFreeBets.TournamentID and
(tbl_UserFreeBets.UserForYes = 'User2' or tbl_UserFreeBets.UserForNo =
'User2')
This is working fine, when there is a data in tbl_CreateFreeBet table for the MatchID. But if there is no data, then this query is not returning the expected result.
For example: With tbl_CreateFreeBet.MatchID = 1, I need to get all the free bets of matchID = 1, with the details of the passed in user, if has bet on 'yes' or 'no'. This comes up fine, as there is data for MatchId = 1 in tbl_CreateFreeBet.
But, it fails, when the tbl_CreateFreeBet.MatchID = 2 input is passed. Here there is no free bet created for the MatchID = 2. But still it returns me the result for MatchID=1.
Please share the query if one is aware of what changes need to be done for my query. Thank you.

Conditions on the first table in a LEFT JOIN should be in the WHERE clause. I think you intend this logic:
select cfb.FreeBetID, cfb.FreeBetDescription, cfb.FreeBetAmount,
cfb.TournamentID, cfb.MatchID,
ufb.UserForYes, ufb.UserForNo, ufb.UserForNoBets
from tbl_CreateFreeBet cfb left outer join
tbl_UserFreeBets ufb
on cfb.MatchID = ufb.MatchID and
cfb.FreeBetID = ufb.FreeBetID and
cfb.TournamentID = ufb.TournamentID and
(ufb.UserForYes = 'User2' or ufb.UserForNo = 'User2')
where cfb.MatchId = 1

Related

Conditional AND statement based on results of COUNT()

I'm getting lost in a query's logic, and I need some help. To start, I have a parent table Campaigns. Then, a child table CampaignParks that looks like this:
select * from CampaignParks
For business reasons I won't bore you with, we need to include Parks in the Campaign table even if they are not included in the Campaign. To actually include them in the campaign, there is a flag for that. None, one, or multiple Parks might be set to Include In Campaign.
Ok. Simple enough. Now...
Given a #ParkID, I need to return all parent Campaigns where the ParkID is a match, but ONLY if one (or more) of the IncludeInCampaign flags for the campaign has been set. If no IncludeInCampaign flags for a given campaign have been set, then we just ignore the value of #Parks. We don't care about it.
Examples:
#ParkID = 11070. We get back 4 & 1
#ParkID = 11526. We get back 1
#ParkID = 26496. We get back 1
#ParkID = null. We get back 4 & 1
ParkID = 69. We get back 1.
So, I think the query would look something like this:
SELECT
DISTINCT
sc.ContactID,
cp.*
FROM
Campaigns sc
JOIN CampaignParks cp on sc.CampaignID = cp.CampaignID
WHERE
((SELECT COUNT(*) FROM CampaignParks cp2 WHERE cp2.CampaignID = sc.CampaignID AND cp2.IncludeInCampaign = 1) > 0 OR cp.ParkID = #ParkID)
But, this isn't getting me the right results.
(I need to take my daughter to gymnastics... I'll take a look at relies when I get back. Thank you!!)
I think your code is close. The major difference is the AND versus OR in the WHERE clause:
SELECT sc.ContactID, cp.*
FROM Campaigns c JOIN
CampaignParks cp
on sc.CampaignID = cp.CampaignID
WHERE cp.ParkId = #ParkId AND
EXISTS (SELECT 1
FROM CampaignParks cp2
WHERE cp2.CampaignID = cp.CampaignID AND
cp2.IncludeInCampaign = 1
);

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!

DISTINCT SQL query with inner joins that omits a column from considerations,

I have a DB2 query as follows:
SELECT DISTINCT RETAILMASTERFILE.DOIDCD AS "RETAILMASTERFILE_DOIDCD",
RETAILMASTERFILE.COCOMO AS "RETAILMASTERFILE_COCOMO",
#XENOS.CUSTREF AS "XENOS_CUSTREF",
#XENOS.ADDUDT AS "XENOS_ADDUDT",
#XENOS.ADUPDD AS "XENOS_ADUPDD",
#XENOS.ADUPDT AS "XENOS_ADUPDT",
#XENOS.ADSTAT AS "XENOS_ADSTAT"
FROM RETAILMASTERFILE INNER JOIN
#XENOS ON RETAILMASTERFILE.DOCOMP = #XENOS.ADCOMP
AND RETAILMASTERFILE.COCOMO = #XENOS.ADDELN
WHERE (RETAILMASTERFILE.DOIDCD = 'CUST008')
AND (RETAILMASTERFILE.COCOMO = '345126032')
AND (RETAILMASTERFILE.DOCOMP = 'LONDON')
The problem is #XENOS.ADUPDT may not be unique which gives me an unwanted duplicate record.
Is there any way I can exclude this from consideration ? Everything I've tried so far within my limited knowledge and crude understanding of group by has so far broken my query.
Use GROUP BY instead:
SELECT RETAILMASTERFILE.DOIDCD AS "RETAILMASTERFILE_DOIDCD",
RETAILMASTERFILE.COCOMO AS "RETAILMASTERFILE_COCOMO",
#XENOS.CUSTREF AS "XENOS_CUSTREF",
#XENOS.ADDUDT AS "XENOS_ADDUDT",
#XENOS.ADUPDD AS "XENOS_ADUPDD",
MAX(#XENOS.ADUPDT) AS "XENOS_ADUPDT",
#XENOS.ADSTAT AS "XENOS_ADSTAT"
FROM RETAILMASTERFILE INNER JOIN
#XENOS
ON RETAILMASTERFILE.DOCOMP = #XENOS.ADCOMP AND
RETAILMASTERFILE.COCOMO = #XENOS.ADDELN
WHERE (RETAILMASTERFILE.DOIDCD = 'CUST008') AND (RETAILMASTERFILE.COCOMO = '345126032') AND
(RETAILMASTERFILE.DOCOMP = 'LONDON')
GROUP BY RETAILMASTERFILE.DOIDCD,
RETAILMASTERFILE.COCOMO,
#XENOS.CUSTREF,
#XENOS.ADDUDT,
#XENOS.ADUPDD,
#XENOS.ADSTAT;

Tables are not joining properly in sql server 2008

I have to show result from the dbo.mail_Messages table, but the problem is that Messages should be shown which Ids are not in dbo.mail_Reply. For that I am using the query below but it is showing nothing.
I have also attached a screen shot:
How can I fix this?
My code:
SELECT
dbo.mail_Messages.MessageID,
dbo.mail_Users_Messages_Mapped.PlaceHolderID,
IsRead,
SenderId,
dbo.mail_Messages.Subject,
dbo.mail_Messages.Body,
dbo.mail_Messages.Date,UserEmail
FROM
dbo.mail_Users_Messages_Mapped
JOIN
dbo.mail_Messages ON dbo.mail_Messages.MessageID = dbo.mail_Users_Messages_Mapped.MessageID
JOIN
dbo.mail_Users ON dbo.mail_Users.UserID = dbo.mail_Users_Messages_Mapped.UserId
JOIN
dbo.mail_Reply ON dbo.mail_Reply.MessageID = dbo.mail_Messages.MessageID
WHERE
UserEmail = 'user1'
AND dbo.mail_Users_Messages_Mapped.PlaceHolderId = 1
AND dbo.mail_Messages.MessageID != dbo.mail_Reply.MessageID
Your query will produce an empty result as it has two contradicting conditions. In the join clause you demand that:
dbo.mail_Reply.MessageID = dbo.mail_Messages.MessageID
And in the where clause you demand that
dbo.mail_Messages.MessageID != dbo.mail_Reply.MessageID
Since a value cannot be both equal and not equal to another value, the combination of these two conditions is an empty result.
One way to solve this, if I understand the requirements correctly, is to stop joining mail_Reply, and use the in operator instead:
SELECT
dbo.mail_Messages.MessageID,
dbo.mail_Users_Messages_Mapped.PlaceHolderID,
IsRead,
SenderId,
dbo.mail_Messages.Subject,
dbo.mail_Messages.Body,
dbo.mail_Messages.Date,UserEmail
FROM
dbo.mail_Users_Messages_Mapped
join
dbo.mail_Messages on dbo.mail_Messages.MessageID = dbo.mail_Users_Messages_Mapped.MessageID
join
dbo.mail_Users on dbo.mail_Users.UserID = dbo.mail_Users_Messages_Mapped.UserId
where
UserEmail = 'user1'
and
dbo.mail_Users_Messages_Mapped.PlaceHolderId = 1
and
dbo.mail_Messages.MessageID NOT IN (SELECT MessageID FROM dbo.mail_Reply)

SQL query for filtering data

I`m working on some sql queries to get some data out of a table; I have made 2 queries for the
same data but both give another result. The 2 queries are:
SELECT Samples.Sample,
data_overview.Sample_Name,
data_overview.Sample_Group,
data_overview.NorTum,
data_overview.Sample_Plate,
data_overview.Sentrix_ID,
data_overview.Sentrix_Position,
data_overview.HybNR,
data_overview.Pool_ID
FROM tissue INNER JOIN (
( patient INNER JOIN data_overview
ON patient.Sample = data_overview.Sample)
INNER JOIN Samples ON
(data_overview.Sample_id = Samples.Sample_id) AND
(patient.Sample = Samples.Sample)
) ON
(tissue.Sample_Name = data_overview.Sample_Name) AND
(tissue.Sample_Name = patient.Sample_Name)
WHERE data_overview.Sentrix_ID= 1416198
OR data_overview.Pool_ID='GS0005701-OPA'
OR data_overview.Pool_ID='GS0005702-OPA'
OR data_overview.Pool_ID='GS0005703-OPA'
OR data_overview.Pool_ID='GS0005704-OPA'
OR data_overview.Sentrix_ID= 1280307
ORDER BY Samples.Sample;")
And the other is
SELECT Samples.Sample,
data_overview.Sample_Name,
data_overview.Sample_Group,
data_overview.NorTum,
data_overview.Sample_Plate,
data_overview.Sentrix_ID,
data_overview.Sentrix_Position,
data_overview.HybNR,
data_overview.Pool_ID
FROM tissue INNER JOIN
(
(patient INNER JOIN data_overview
ON patient.Sample = data_overview.Sample)
INNER JOIN Samples ON
(data_overview.Sample_id = Samples.Sample_id)
AND (patient.Sample = Samples.Sample)) ON
(tissue.Sample_Name = data_overview.Sample_Name)
AND (tissue.Sample_Name = patient.Sample_Name)
WHERE ((
(data_overview.Sentrix_ID)=1280307)
AND (
(data_overview.Pool_ID)="GS0005701-OPA"
OR (data_overview.Pool_ID)="GS0005702-OPA"
OR (data_overview.Pool_ID)="GS0005703-OPA"
OR (data_overview.Pool_ID)="GS0005704-OPA"))
OR (((data_overview.Sentrix_ID)=1416198))
ORDER BY data_overview.Sample;
The one in the top is working quite well but it still won't filter the sentrix_ID.
The second 1 is created with Access but when I try to run this Query in R it gave
a unexpected symbol error. So if anyone knows how to create a query that filter POOL_ID and Sentrix_id with the given parameters thanks in advance
Is it a case of making the where clause something like this:
WHERE Sentrix_ID = 1280307 AND (Pool_ID = 'VAL1' OR Pool_ID = 'VAL2' OR Pool_ID = 'VAL3')
i.e. making sure you have brackets around the "OR" components?
Maybe you meant:
...
WHERE data_overview.Sentrix_ID IN (1280307,1416198 )
AND data_overview.Pool_ID IN ("GS0005701-OPA", "GS0005702-OPA", "GS0005703-OPA" ,"GS0005704-OPA")
;