I am getting a correct result, but now instead of showing me result that is 0 I want them to show me null result . How could I get results null instead of 0 ?
SELECT w.FIRST_NAME,w.LAST_NAME,COUNT(s.SECTION_ID) AS COUNTED_SECTIONS
FROM INSTRUCTOR w LEFT OUTER JOIN SECTION s
ON w.INSTRUCTOR_ID = s.INSTRUCTOR_ID
GROUP BY w.FIRST_NAME,w.LAST_NAME
ORDER BY w.LAST_NAME;
currently showing
FIRST_NAME LAST_NAME COUNTED_SECTIONS
------------------------- ------------------------- ----------------
Rick Chow 0
Marilyn Frantzen 10
Fernand Hanks 9
Charles Lowry 0
etc
but I want
FIRST_NAME LAST_NAME COUNTED_SECTIONS
------------------------- ------------------------- ----------------
Rick Chow
Marilyn Frantzen 10
Fernand Hanks 9
Charles Lowry
etc
I've tried it with NVL and it doesn't work
NVL(COUNT(s.SECTION_ID),NULL) AS COUNTED_SECTIONS
I think NULLIF() is available in oracle:
SELECT w.FIRST_NAME,w.LAST_NAME,NULLIF(COUNT(s.SECTION_ID),0) AS COUNTED_SECTIONS
FROM INSTRUCTOR w LEFT OUTER JOIN SECTION s
ON w.INSTRUCTOR_ID = s.INSTRUCTOR_ID
GROUP BY w.FIRST_NAME,w.LAST_NAME
ORDER BY w.LAST_NAME;
Try nullif:
SELECT w.FIRST_NAME,w.LAST_NAME, NULLIF(COUNT(s.SECTION_ID), 0) AS COUNTED_SECTIONS
FROM INSTRUCTOR w LEFT OUTER JOIN SECTION s
ON w.INSTRUCTOR_ID = s.INSTRUCTOR_ID
GROUP BY w.FIRST_NAME,w.LAST_NAME
ORDER BY w.LAST_NAME;
You can use DECODE in order to decide what you want to display
DECODE(COUNT(s.SECTION_ID),0, NULL, COUNT(s.SECTION_ID)) AS COUNTED_SECTIONS
Related
I have two tables, emp and location. I need to fetch the records for all the matching eid s' of emp table based on location type.
If the location type=2 then we need to fetch the city associated with it.
If we don't have type=2 record we need to fetch type=1 associated city for the matching eid.
My case statement works fine until there are two records for the eid of both type 1 and type 2. But I need to fetch only type 2 in this case
select case when a.type=2 then a.city
When a.type=1 then a.city
Else '0' End As City
From location a
Join emp r
On a.eid=r.eid
emp table
eid ename
1 james
2 mark
3 kristie
4 john
5 allen
location table
city eid type
athens 1 2
melbourne 2 1
london 2 2
newyork 3 1
output:
eid ename city type
1 james athens 2
2 mark london 2
3 kristie newyork 1
I think the most direct way to represent what you're asking for is:
select coalesce(l2.city, l1.city, '0') as city
From emp r
left join location l1
on l1.eid = r.eid
and l1.type=1
left join location l2
on l2.eid = r.eid
and l2.type=2
The subquery-based solution proposed by Jeremy Real may also work, but it assumes that 1 and 2 are they only values in the table for location.type (and I just don't find it to be as intuitive).
Try this:
select a.eid
,r.ename
,case when a.type=2 then b.city
when a.type=1 then b.city
else '0' End As City
from (
select a.eid, max(a.type) as type
From location a
group by a.eid
) a
right outer join location b
on a.eid = b.eid and a.type=b.type
inner join emp r
on b.eid=r.eid
You want to rank your cities. Use ROW_NUMBER to do that:
select e.eid, e.name, l.city, l.type
from emp e
join
(
select
city, eid, type,
row_number() over (partition by eid order by type desc) as rn
from location
) l on l.eid = e.eid and l.rn = 1;
rn is 1 for the better city per eid (where "better" is the one with the higher type).
I have 2 tables respectively,emp_data, role_data
EMP_DATA
ID EMPID EMPNAME ROLEID
1 A01 ABC 1
2 A01 ABC 3
ROLE_DATA
ROLEID ROLENAME EMPID
1 SE A01
2 SSE B01
When I join these 2 tables, i have to get OUTPUT OF 2 records like the below one
EMPID EMPNAME ROLEID ROLENAME
A01 ABC 1 SE
A01 ABC 3 <NULL OR EMPTY>
The query what i have written will give output where instead of null in rolename it gives me SE.
SELECT ED.EMPID,ED.EMPNAME,ED.ROLEID,RD.ROLENAME
FROM SYN.EMP_DATA ED,SYN.ROLE_DATA RD
WHERE ED.EMPID=RD.EMPID
Kindly help me in this regard as to how to get the output like i desire.
Thanks in advance
You want a left join:
select e.EMPID, e.EMPNAME, e.ROLEID, r.ROLENAME
from EMP_DATA e left join
ROLE_DATA r
on e.ROLEID = r.ROLEID;
By the way, your problem suggests an issue with your database. EMP_DATA.ROLEID should be declared as a foreign key referencing ROLE_DATA. If so, it would be an error to insert a value that is invalid.
Show us your code. You have to use LEFT JOIN
SELECT emp.EMPID
,emp.EMPNAME
,emp.ROLEID
,ROLE.ROLENAME
FROM EMP_DATA as emp
LEFT JOIN ROLE_DATA as ROLE ON emp.ROLEID = ROLE.ROLEID;
Try this query
select a.EMPID, a.EMPNAME, a.ROLEID,IFNULL(b.ROLENAME, 'SE') as ROLENAME
from EMP_DATA a
lef join ROLE_DATA b on a.ROLEID=B.ROLEID
I have three tables STUDENTS, SUBJECTS, RANK ,with data as -
1) STUDENTS [NAME(Primary)]
NAME
--------
Alex
Greg
2) SUBJECTS [ID(Primary)]:
ID
--------
100
101
102
3) RANK [SEQ(Primary), NAME, ID, RANK]
SEQ NAME ID RANK
------ ------- ------ ------
1 Alex 100 A
2 Greg 100 A
3 Greg 101 B
I want to create a view that should display data as
NAME ID RANK
------- ------ ------
Alex 100 A
Alex 101 Z
Alex 102 Z
Greg 100 A
Greg 101 B
Greg 102 Z
So, for every student and for every subject, the View should display the RANK if present in RANK table, else replace the NULL with 'Z'.
I'm a newbie to SQL. So any help in forming the query would be deeply appreciated!
cross join student and subject then left outer join the result with rank to get ranks for all (student, subject) combination. selecting column with NVL OR COALESCE will replace NULL with 'z'.
SELECT st.name,
su.id,
NVL(ra.rank,'Z') Rank, --COALESCE(ra.rank,'Z') Rank
FROM student st
CROSS JOIN subject su
LEFT OUTER JOIN rank ra
ON ra.name = st.name
AND ra.id = su.id
ORDER BY st.name,su.id
Note : ORDER BY can be removed from above query if you don't need.
fiddle
SELECT r.NAME, r.ID, NVL(r.RANK, 'Z')
FROM RANK r, studendts st, SUBJECTS su
WHERE st. NAME = r. NAME
AND su.ID = r.ID
ORDER BY 1,2,3
Right now my query is giving me a 0 when I count the enrolled date. How could I have a row show me 'none' result instead of a 0.
what I am missing here ?
COALESCE(TO_CHAR(SUM(ENROLLMENTS)),'none') ENROLLMENTS
this is what I have
SELECT lt.STUDENT_ID,lt.FIRST_NAME, lt.LAST_NAME,COALESCE(TO_CHAR(SUM(ENROLLMENTS)),'none') ENROLLMENTS
FROM STUDENT lt
LEFT OUTER JOIN
(SELECT s.STUDENT_ID, z.COURSE_NO,COUNT(e.ENROLL_DATE) AS ENROLLMENTS
FROM STUDENT s
LEFT JOIN ENROLLMENT e ON s.STUDENT_ID = e.STUDENT_ID
LEFT JOIN SECTION z ON e.SECTION_ID = z.SECTION_ID
WHERE s.PHONE LIKE '702%'
GROUP BY s.STUDENT_ID, z.COURSE_NO) rt
ON lt.STUDENT_ID = rt.STUDENT_ID
WHERE lt.PHONE LIKE '702%'
GROUP BY lt.STUDENT_ID,lt.FIRST_NAME, lt.LAST_NAME,ENROLLMENTS
ORDER BY lt.LAST_NAME,lt.FIRST_NAME;
instead of having results
STUDENT_ID FIRST_NAME LAST_NAME ENROLLED
---------- ------------------------- ------------------------- -----------
253 Walter Boremmann 1
396 James E. Norman 0
etc
I'd like to have it like this
STUDENT_ID FIRST_NAME LAST_NAME ENROLLED
---------- ------------------------- ------------------------- -----------
253 Walter Boremmann 1
396 James E. Norman none
etc
I think you need a CASE statement because TO_CHAR is returning "0":
(CASE WHEN SUM(ENROLLMENTS)=0 THEN 'none' ELSE SUM(ENROLLMENTS) END) ENROLLMENTS
I have a users table, and a view table which lists some user ids... They look something like this:
Users:
User_ID | Name | Age | ...
555 John Doe 35
556 Jane Doe 24
557 John Smith 18
View_Table
User_ID
555
557
Now, when I do run a query to retrieve a user:
SELECT User_ID,Name,Age FROM Users WHERE User_ID = 555
SELECT User_ID,Name,Age FROM Users WHERE User_ID = 556
I also would like to select a boolean, stating whether or not the user I'm retrieving is present in the View_Table.
Result:
User_ID Name Age In_View
555 John Doe 35 1
556 Jane Doe 24 0
Any help would be greatly appreciated. Efficiency is a huge plus. Thanks!!
SELECT Users.User_ID,Name,Age, View_Table.User_ID IS NOT NULL AS In_View
FROM Users
LEFT JOIN View_table USING (User_ID)
WHERE User_ID = 555
SELECT
User_ID, Name, Age,
CASE WHEN v.UserID is not null THEN 1 ELSE 0 END AS In_View
FROM Users u
LEFT JOIN View_Table v on u.User_ID = v.UserID
WHERE UserID ...;
I would do a LEFT JOIN. So long as you have key/index for User_ID, it should be very efficient.
SELECT User_ID,Name,Age, IF(View_Table.User_ID, 1, 0) AS In_View
FROM Users LEFT JOIN View_Table USING(User_ID)
WHERE User_ID = 555
I know this is an "Old" question but just happened upon this and none of these answers seemed to be that good. So I thought I would throw in my 2 cents
SELECT
u.User_ID,
u.Name,
u.Age,
COALESCE((SELECT 1 FROM View_Table AS v WHERE v.User_ID = u.User_ID ), 0) AS In_View
FROM
Users AS u
WHERE
u.User_ID = 555
Simply select 1 with a correlated query ( or null ) then to get the 0 we can use the handy function COALESCE which returns the first non-null value left to right.