Add condition in where clause - sql

I have 3 tables advert , application and people.
Select * from advert v, application a
inner join people p on p.id = a.id
where v.id=a.id
This query returns me all application irrespective of the gender. But sometimes in the advert table, gender is specified as M. So now i want the query above return me only application made by M. To get this value i need to add one more condition, p.gender = v.gender. How do i do this? Sometimes the value of v.gender = n/a. Then I wont need this condition. It should return me all application irrespective gender.

To get your desired results, you need to modify the join condition between advert and people to join the records in either case (v.gender = 'n/a' or p.gender = v.gender):
select *
from advert v
join application a
on a.id = v.id
join people p
on p.id = a.id
and (v.gender = 'n/a' or p.gender = v.gender)

I'm a little confused as to what you are asking but here is something to start with. Some example data would also be helpful
select *
from advert v
inner join application a
on a.id = v.id
inner join people p
on a.id = p.id
where p.gender = 'M';
or
select *
from advert v
inner join application a
on a.id = v.id
inner join people p
on a.id = p.id
where p.gender = v.gender;

You could potentially use an IN operator thusly:
WHERE v.GENDER IN ('N/A',p.GENDER)
This would return all records where v.GENDER = 'N/A' or where v.gender = p.GENDER

Related

List of users who do not have transactions after 'x' date

I have a query I need to create that pulls data from three different tables. Essentially the end result is to pull data for users that have not had any activity on a users' account since 07/01/2018, but they have to have an account plan as "x." Is there a way to manipulate this query I've created to get it to display what I need it to? Maybe somehow correlate it to a count of 0 on the TRANDATE column? Very lost here and could use some help!
select p.ID as ID, p.LAST as LastName, p.FIRST as FirstName
From gl
inner join p
on p.ID = gl.PID
left join psp
on psp.PLANNUM = gl.ACCNUM
where gl.ACCNUM = 'x'
and psp.ACTIVE = 1
and gl.TRANDATE <= to_date('07/01/2018', 'MM/DD/YYYY')
;
commit;
Thank you all very much and please let me know if there's anything else I can provide here.
You don't really specify your table structure, so I'm kinda guessing here. Does your "gl" table have more than one record per PID+ACCNUM? What are the primary keys? There are a lot of extra details you could provide.
select p.ID as ID, p.LAST as LastName, p.FIRST as FirstName, g.maxdate
From (select gl.PID, gl.ACCNUM, max(gl.TRANDATE) as maxdate
from gl
where gl.ACCNUM = 'x'
group by gl.PID, gl.ACCNUM) g
inner join p
on p.ID = g.PID
inner join psp
on psp.PLANNUM = g.ACCNUM
and psp.ACTIVE = 1
where g.maxdate <= to_date('07/01/2018', 'MM/DD/YYYY')
;
Also, (a) you don't need a commit on a select, and (b) a left/outer join is an inner join when you have its column in the WHERE clause (psp.ACTIVE = 1).
I would simply use aggregation and max() in the having clause:
select p.ID as ID, p.LAST as LastName, p.FIRST as FirstName
from gl inner join
p
on p.ID = gl.PID inner join
psp
on psp.PLANNUM = gl.ACCNUM
where gl.ACCNUM = 'x' and psp.ACTIVE = 1
group by p.ID, p.LAST, p.FIRST
having max(gl.TRANDATE) <= date '2018-07-01';
Note that the where condition on psp.ACTIVE turns the outer join into an inner join, so I changed the join type for readability.
Since all your display fields are from p, you can do grouping in inner queryfor the p.id which occur more than once. All the rest of ids(not in) are of your interest.
select p.ID as ID, p.LAST as LastName, p.FIRST as FirstName
From p where p.ID not in (
select p.ID From gl
inner join p on p.ID = gl.PID
left join psp on psp.PLANNUM = gl.ACCNUM
where gl.ACCNUM = 'x'
and psp.ACTIVE = 1
and gl.TRANDATE <= to_date('07/01/2018', 'MM/DD/YYYY')
group by p.ID
having count(1) >= 1)

How can I join 3 tables, limit the query based on a condition, and return empty columns if condition not met?

I have the following query currently:
select * from people
LEFT JOIN addresses
ON people.id = addresses.id
LEFT JOIN pers
ON people.id = pers.pers_id
WHERE people.id =:id
AND addresses.is_primary = 'Y'
Of course if there is no address where is_primary = 'Y' for a given person, the query doesn't return any results.
Without is_primary='Y', the query returns multiple addresses.
Is there any way, instead, to return null columns for all of the address fields in the event where there is no record for the id where is_primary = 'Y'?
You can do something like this -
select *
from people
LEFT JOIN addresses
ON people.pidm = addresses.pidm
and addresses.is_primary = 'Y'
RIGHT JOIN pers
ON people.id = pers.pers_id
WHERE people.id = :id
use case when
select *,case when addresses.is_primary not in('Y') then 'primary address different' else addresses.is_primary end as is_primary from people
LEFT JOIN addresses
ON people.pidm = addresses.pidm
RIGHT JOIN pers
ON people.id = pers.pers_id
WHERE people.id =:id
I strongly recommend that you not mix left join and right join. The query is just so hard to follow.
Instead, start with the table where you want to keep all the rows. Then only use left join. Sometimes, you may need to put conditions in the on clause.
In your case:
select *
from people p left join
addresses a
on p.pidm = a.pidm and a.is_primary = 'Y' left join
pers
on p.id = pers.pers_id
where p.id = :id;

How do I go about checking the existence of records in all tables JOIN`ed in a statement?

I want ensure none of the tables included through INNER JOIN return 0 rows, resulting in an empty result-set due to the join being INNER. I want to be able to return information to the user about which table had no records, causing the query to return empty.
PS: Please ignore Foreign Keys for this question.
Example-query:
SELECT * FROM Person P
INNER JOIN [User] U ON U.PersonId = P.Id
INNER JOIN Email E ON E.UserId = U.Id
INNER JOIN Company C on C.Id = U.CompanyId
WHERE P.Id = 3
Let's say the user had no associated Email-records. I then want to be able to tell the user about this. Note: I only need to print the first failing step, I.E if the user has no Email and no Company, I only need to tell the user about him not having any Email.
How I would have solved it earlier:
I would perform the complete query in steps, building it up join by join, doing a whole lot of redundant querying. I really don't like this solution at all, which is why I'm asking for help.
-- Ensure Person exists
IF NOT EXISTS (
SELECT * FROM Person P
WHERE P.Id = 3
)
BEGIN
PRINT 'No associated Person was found';
RETURN 1;
END
-- Ensure User exists
IF NOT EXISTS (
SELECT * FROM Person P
INNER JOIN [User] U ON U.PersonId = P.Id
WHERE P.Id = 3
)
BEGIN
PRINT 'No associated User was found';
RETURN 1;
END
And so on. How can I write this more concise, in a way that solves the same problem but avoids repeating queries?
Thanks in advance.
Update
By looking at the answers, I realized my example was bad. Using LEFT JOIN is ok in this example, since the "join-chain" is not straight; Person joins User, then User joins in multiple directions, like this:
Person - User - Email
|
Company
I'll try to provide a different example:
SELECT * FROM a
INNER JOIN b ON b.a_id = a.id
INNER JOIN c ON c.b_id = b.id
INNER JOIN d ON d.c_id = c.id
INNER JOIN e ON e.d_id = d.id
INNER JOIN f ON f.e_id = e.id
INNER JOIN g ON g.f_id = f.id
The join-chain would then look like this:
a - b - c - d - e - f - g
If I were to use LEFT JOIN's to ensure existence of records, the IIF/CASE statements would get pretty terrible. Example:
SELECT
CASE WHEN b.id is null THEN 'b is null' END as b_is_null
CASE WHEN b.id is not null and c.id is null THEN 'b is null' END as c_is_null
..
CASE WHEN b.id is not null and c.id is not null and d.id is not null and e.id is not null and f.id is not null and g.id is null THEN 'g is null' AS g_is_null
FROM a
LEFT JOIN b ON b.a_id = a.id
LEFT JOIN c ON c.b_id = b.id
LEFT JOIN d ON d.c_id = c.id
LEFT JOIN e ON e.d_id = d.id
LEFT JOIN f ON f.e_id = e.id
LEFT JOIN g ON g.f_id = f.id
It can get really ugly. And these examples are with 1-character alias-names and 2-character property-names.
Keep in mind, I also want to check if the first table (Person/a, the one not joined) also returns rows.
You need to use left join. From your query, if a person doesn't have any user they obviously doesn't have any email or company. So all left join will be fine.
SELECT P.*,
IIF(U.PersonID IS NULL, 0, 1) AS isUserExists,
IIF(E.UserId IS NULL, 0, 1) AS isEmailExists,
IIF(C.Id IS NULL, 0, 1) AS isCompanyInformationExists
FROM Person P
LEFT JOIN [User] U ON U.PersonId = P.Id
LEFT JOIN Email E ON E.UserId = U.Id
LEFT JOIN Company C on C.Id = U.CompanyId
WHERE P.Id = 3
If U.PersonID IS NULL then no user existed, if E.UserId IS NULL then no email existed, if C.Id IS NULL then no company information existed for that person. By this you can use your messages as you need.
Use case statements like below and print your message.
select
case when u.PersonId is null and p.id is not null then 'user not exists'
WHEN u.PersonId IS NOT NULL AND p.id IS NOT NULL AND e.mailid is null THEN 'mail id not exist'
else 'No associated Person was found' end message
FROM Person P
left JOIN [User] U ON U.PersonId = P.Id
left JOIN Email E ON E.UserId = U.Id
left JOIN Company C on C.Id = U.CompanyId
WHERE P.Id = 3
DECLARE #Email VARCHAR(50)
DECLARE #Company VARCHAR(50)
SELECT #Email = E.Email, #Company = C.CompanyName FROM Person P
INNER JOIN [User] U ON U.PersonId = P.Id
LEFT JOIN Email E ON E.UserId = U.Id
LEFT JOIN Company C on C.Id = U.CompanyId
WHERE P.Id = 3
IF #Email IS NULL
Print 'NO Email'
IF #Company IS NULL
Print 'No Company'

How to compare with Case in Where clause

Here am having Person and Address Tables .Some Persons may have address or not.If they have address then want to join address table otherwise no need to join.Please help to solve this case.
select p.name,nvl(a.address,'address not available') from person p,address a
where p.id = 2 and case
when p.addid is not null
then p.addid = a.id
else 0=0 end
The general solution - use Boolean logic. You cannot choose between complete expressions using CASE, so you should rewrite it to use a combination of AND and OR. Using the logic from your question, you would rewrite it as:
WHERE p.id = 2
AND
(
(p.addid IS NOT NULL AND p.addid = a.id)
OR (p.addid IS NULL AND 0=0)
)
Which ultimately simplifies down to:
WHERE p.id = 2
AND (p.addid IS NULL OR p.addid = a.id)
The specific solution for your query - use better JOIN syntax, and simply leverage a LEFT JOIN:
SELECT p.name, nvl(a.address,'address not available')
FROM person p
LEFT OUTER JOIN address a ON p.addid = a.id
WHERE p.id = 2
Try to use coalesce function as below
select p.name,nvl(a.address,'address not available') from person p,address a
where p.id = 2
and coalesce(p.addid,a.id)=a.id
select p.name, nvl(a.address, 'address not available')
from person p left outer join address a
on (p.addid = a.id )
where p.id = 2;
You can't use case, but you can achieve the same effect with combination of ors sand ands
select p.name,
nvl(a.address,'address not available')
from person p,
address a
where p.id = 2 and
( p.addid is not null AND p.addid = a.id
OR
p.addid is null
)
try this:
select p.name,nvl(a.address,'address not available') from person p,address a
where p.id = 2 and a.id = case when p.addid is not null then p.addid else a.id end;

sql query sum bringing back different results

I have the following two queries below, the Total is coming back different, but I am adding the sums in each of the query the same way. Why is the total coming back different?
select [Total Children] = (SUM(demo.NumberOfPreschoolers) + SUM(demo.NumberOfToddlers) + SUM(demo.NumberOfInfants)),
County = co.Description
from ClassroomDemographics as demo
inner join Classrooms as c on demo.Classroom_Id = c.Id
inner join Sites as s on c.Site_Id = s.Id
inner join Profiles as p on s.Profile_Id = p.Id
inner join Dictionary.Counties as co on p.County_Id = co.Id
where co.Description = 'MyCounty'
Group By co.Description
select [Number Of DLL Children] = SUM(cd.NumberOfLanguageSpeakers),
[Total Children] = (SUM(demo.NumberOfPreschoolers) + SUM(demo.NumberOfToddlers) + SUM(demo.NumberOfInfants)),
County = co.Description
from ClassroomDLL as cd
inner join Classrooms as c on cd.Classroom_Id = c.Id
inner join Sites as s on c.Site_Id = s.Id
inner join Profiles as p on s.Profile_Id = p.Id
inner join Dictionary.Counties as co on p.County_Id = co.Id
inner join ClassroomDemographics as demo on c.Id = demo.Classroom_Id
where co.Description = 'MyCounty'
Group by co.Description
Just a quick glance over the two querties, I would presume that:
inner join ClassroomDemographics as demo on c.Id = demo.Classroom_Id
in the second query is excluding results that are in the first query, therefor the aggregated values will be different.
Your join to the Classrooms table is joining with an extra table in the 2nd query.
Query 1:
from ClassroomDemographics as demo
inner join Classrooms as c on demo.Classroom_Id = c.Id
Query 2:
from ClassroomDLL as cd
inner join Classrooms as c on cd.Classroom_Id = c.Id
...
inner join ClassroomDemographics as demo on c.Id = demo.Classroom_Id
My bet is that the ClassroomDLL table has less data in it, or has rows with a null for one of the join criteria columns, either of which could exclude rows from the results and throw your aggregate totals off.