How to hidden value when it doesn't match - oracle12c

I'm in a trouble like this:
For a following table A with data
|ID| |Department| |Name|
1 IT JOHN
3 IT JOHN
5 IT JOHN
1 SALE LINH
3 SALE LINH
2 SALE LINH
3 CLERK MINA
6 CLERK MINA
4 CLERK MINA
There are some ROLEID, each ROLEID has role corresponding, such as 1 is Manager,2 is leader...
I want to select based on ROLEID, if exist ROLEID = 3, all values has department is IT won't display. How can I do that?

Related

Replace empty strings with NULL instead of empty strings when using JOIN

I have two tables:
table_a
id name
1 john
2 dave
3 tim
4 marta
5 jim
table_b
id sum random_metric
1 10.50 abc
3 11.5 efg
5 5.76 ghj
I have joined them on id
SELECT ...
FROM table_a
LEFT JOIN table_b ON table_a.id = table_b.id
and I get:
id name sum random_metric
1 john 10.5 abc
2 dave
3 tim 11.5 efg
4 marta
5 jim 5.76 ghj
Then I want to convert the sum column to double precision but since it has empty strings in rows 2, 4 it does not work.
How could I join tables so that I would have this:
id name sum random_metric
1 john 10.5 abc
2 dave NULL NULL
3 tim 11.5 efg
4 marta NULL NULL
5 jim 5.76 ghj

Hibernate Native Query Left Join vs Postgresql Left join (Why are they different)

I'd like to start by saying that I'd like to think I am pretty proficient sql (10 years of experience) and have ran into a head scratcher. I have the following data
Group
Id Name
1 Group 1
2 Group 2
User
Id Name
1 Jon Williams
2 Mike Williams
3 Joyce Copper
Product
Id Name
1 Cookies
2 Milk
3 Ice cream
Status
Id Name
1 Untouched
2 Consumed
3 Partly Consumed
HouseholdProduct
Id Product User Status
1 1 1 1
2 2 2 1
3 3 3 1
4 1 1 1
5 2 2 1
6 3 3 1
GroupHousehold
GroupId HouseholdProductId
1 1
1 2
1 3
2 1
2 2
2 3
In Postgres, I wrote the following
select g.id as Group Id, g.name as Group, p.name as Product,
u.name as User
from group g
left join GroupHouse gh on
g.id = gh.groupId
left join HouseHoldProduct hp on
gh.houseHoldProductId = hp.id
left join User u on
hp.userId = u.Id
left join Product p on
hp.product_id = p.id
order by g.id asc
I get the following data which is correct:
ID Group Product User
1 Group 1 Cookies Jon Williams
1 Group 1 Milk Mike Williams
1 Group 1 Ice cream Joyce Copper
2 Group 2 Cookies Jon Williams
2 Group 2 Milk Mike Williams
2 Group 2 Ice cream Joyce Copper
When I take that exact query and paste it in Hibernate as native query, I get the follow results which is wrong.
ID Group Product User
1 Group 1 Cookies Jon Williams
1 Group 1 Milk Jon Williams
1 Group 1 Ice cream Jon Williams
2 Group 2 Cookies Jon Williams
2 Group 2 Milk Jon Williams
2 Group 2 Ice cream Jon Williams
It looks like hibernate is the left joining to the User table incorrectly. Does anyone know why this is happening? Is there a way to fix this?

Full Outer Joins In PostgreSql [duplicate]

This question already has answers here:
Left Outer Join Not Working?
(4 answers)
Closed 4 years ago.
I've created a table of students with columns student_id as primary key,
student_name and gender.
I've an another table gender which consists of gender_id and gender.
gender_id in student refers to table gender.
Tables data looks like this:
Student table
STUDENT_ID STUDENT_NAME GENDER
1 Ajith 1
2 Alan 1
3 Ann 2
4 Alexa 2
5 Amith 1
6 Nisha 2
7 Rathan 1
8 Rebecca 2
9 asdf null
10 asd null
11 dbss null
Gender Table
GENDER_ID GENDER
1 Male
2 Female
3 Others
My query and its result
SELECT S.STUDENT_NAME,
G.GENDER
FROM STUDENTS S
FULL OUTER JOIN GENDER G ON G.GENDER_ID = S.GENDER
result is giving with 12 rows including the Others value from the gender table.
STUDENT_ID STUDENT_NAME GENDER
1 Ajith Male
2 Alan Male
3 Ann Female
4 Alexa Female
5 Amith Male
6 Nisha Female
7 Rathan Male
8 Rebecca Female
Others
9 asdf
10 asd
11 dbss
I'm trying to restrict a particular student_id:
SELECT S.STUDENT_ID,
S.STUDENT_NAME,
G.GENDER
FROM STUDENTS S
FULL OUTER JOIN GENDER G ON G.GENDER_ID = S.GENDER
WHERE S.STUDENT_ID <> 11;
now the the total number of the rows are reduced to 10.
STUDENT_ID STUDENT_NAME GENDER
1 Ajith Male
2 Alan Male
3 Ann Female
4 Alexa Female
5 Amith Male
6 Nisha Female
7 Rathan Male
8 Rebecca Female
9 asdf
10 asd
Why has the one row with Others Values disappeared from the second select query?
I'm trying to find the cause of this issue.
That's because NULL <> 11 is not TRUE, but NULL, and only rows where the condition is TRUE are included in the result.
You'd have to write something like
WHERE s.student_id IS DISTINCT FROM 11
Your second select query returns all rows where student_id is different (<>) from 11.

query multiple attribute in a table with single attribute in another table

I can't explain my problem in English well. So I write my problem in a personal way.
user_id name surname
1 john great
2 mary white
3 joseph alann
event_id official_id assistant_id date
1 1 2 2017-12-19
2 1 3 2017-12-20
3 2 3 2017-12-21
I want to get names at the same time when I query an event. I tried:
SELECT * FROM event a, user b WHERE a.official_id=b.user_id AND a.assistant_id=b.user_id
When I use "OR" instead of "AND" gives me cartesian result. I want the result like:
event_id off_id off_name asst_id asst_name date
1 1 john 2 mary 2017-12-19
2 1 john 3 joseph 2017-12-20
3 2 mary 3 joseph 2017-12-21

Return all records regardless if there is a match

In my Table 1, It may have AND have a null entry in the address column to corresponding record OR not have a matching entry in Table 2.
I want to present all the records in Table 1 but also present corresponding entries from Table 2. My RESULT is what I am trying to achieve.
Table 1
ID First Last
1 John Smith
2 Bob Long
3 Bill Davis
4 Sam Bird
5 Tom Fenton
6 Mary Willis
Table 2
RefID ID Address
1 1 123 Main
2 2 555 Center
3 3 626 Smith
4 4 412 Walnut
5 1
6 2 555 Center
7 3
8 4 412 Walnut
Result
Id First Last Address
1 John Smith 123 Main
2 Bob Long 555 Center
3 Bill Davis 626 Smith
4 Sam Bird 412 Walnut
5 Tom Fenton
6 Mary Willis
You need an outer join for this:
SELECT * FROM Table1 t1 LEFT OUTER JOIN Table2 t2 ON t1.ID = t2.RefID
How do you join those two tables? If table 2 have more than 1 matched address, how do you want display them? Please clarify in your question.
Here is a query based on my assumptions.
SELECT
ID, First, Last,
Address = (SELECT MAX(Address) FROM Table2 t2 WHERE t1.ID = t2.ID)
FROM Table1 t1