Left join oracle db - sql

I have two tables and I wanna join them (outer).
day description
-----
1 mo
2 tu
...
5 fr
order day code
----
1 1 0
2 2 0
3 1 2
4 3 0
5 4 0
I wanna this table as a result:
description order code
------
mo 1 0
th 2 0
we 4 0
tu 5 0
fr null null
When I use a left join like this, the code does not work right and does not show me friday:
select *
from days d left join order o on d.id= o.day
where o.code = 0
But when I write the code like this, the code works right:
select *
from
(select * from day) d
left join
(select * from order when code = 0) o
on d.id = o.day
Does anyone know why?

Just put the condition on the left joined table in the on clause of the left join rather than in the where clause:
select d.description, o.order, o.code
from days d
left join order o on d.id= o.day and o.code = 0
Conditions in the where clause are mandatory - so the where clause eliminates rows where the left join came back empty, since o.code is null, which is not equal to 0.

on condition is used to join the table.
where condition is to filter the records from the join result.
Below will join the table on the condition of d.id = o.day
and then select records only with o.code=0
select *
from days d left join order o on d.id= o.day
where o.code = 0
In order to join on both d.id = o.day and o.code=0
you need below
select *
from days d left join order o on d.id= o.day
and o.code = 0

Related

Using Sql server with In

I have two tables Tbl_Event (primary key:-EventID) and tbl_items.
tbl_items table contains multiple items for same EventID with status 0 or 1.
Sample Data:
Tbl_event:
EventId Name
5 Test
6 Seminar
7 Meet
tbl_items
ItemId EventId status
1 5 0
2 6 1
2 6 0
3 7 1
3 7 1
I just need to get Event "Meet" having both status value 1.
This is what I tried:
SELECT *
FROM Tbl_items L
INNER JOIN Tbl_Events E ON L.EventId = E.EventId
WHERE L.Eventid NOT IN (SELECT Eventid FROM TBL_VMS_LENT_ITEMS WHERE Status = 0)
One approach is to use a subquery which aggregates over events and checks that all statuses are 1:
WITH cte AS (
SELECT EventId
FROM tbl_items
GROUP BY EventId
HAVING SUM(CASE WHEN status <> 1 THEN 1 ELSE 0 END) = 0
)
SELECT t1.*
FROM Tbl_event t1
INNER JOIN cte t2
ON t1.EventId = t2.EventId;
If you just want the event IDs, use SELECT * FROM cte, otherwise use the full query I gave immediately above. This approach will generalize to more complex logic, should you need it later.
This will give you all events that have all items with status 1.
SELECT
E.EventID
FROM
Tbl_event AS E
INNER JOIN tbl_items AS I ON E.EventID = I.EventID
GROUP BY
E.EventID
HAVING
MIN(I.status) = 1
If you want the full Event record:
;WITH EventsWithItemsStatus1 AS
(
SELECT
E.EventID
FROM
Tbl_event AS E
INNER JOIN tbl_items AS I ON E.EventID = I.EventID
GROUP BY
E.EventID
HAVING
MIN(I.status) = 1
)
SELECT
E.*
FROM
Tbl_event AS E
INNER JOIN EventsWithItemsStatus1 AS N ON E.EventID = N.EventID
Use group by clause with subquery
select * from Tbl_event e
inner join (
select EventId
from tbl_items
where status = 1
group by EventId
having count(*) > 1
) i on i.EventId = e.EventId
Question is not very clear
SELECT min(L.ItemId), L.EventId
FROM Tbl_items L
where L.Status = 1
group by L.EventId
having count(*) > 1

Group by day and left outer join the whole

The data like
mdn day flag
c 20180302 0
c 20180303 1
b 20180303 0
a 20180301 1
b 20180301 0
a 20180302 1
I get the whole by select distinct mdn from data, and left join every day, how to realize it by using hive? As following, it's only one day sample:
with temp as (select distinct mdn from data)
select * from temp b
left outer join
(select * from data where day=20180302) a
on a.mdn=b.mdn
The result of one day like:
c c 20180302 0
a a 20180302 1
b null null null
Exactly, it is just one day, and I want to get 'b null 20180302 null'
Use a cross join to generate all the combinations:
select m.mdn, d.day, data.flag
from (select distinct mdn from data) m cross join
(select distinct day from data) d left join
data
on data.mdn = m.mdn and data.day = d.day ;

Different output when using count and group by

When trying to get a count of IDs I get a different answer when grouping by day vs when I am not.
select cv.CONV_DAY, count(distinct cv.CLICK_ID)
from
clickcache.click cc
right join(
select distinct cv.CLICK_ID, cv.CONV_DAY, cv.PIXEL_ID
from clickcache.CONVERSION cv
where cv.CLICK_ID IS NOT NULL) cv ON cv.CLICK_ID = cc.ID
where cc.ADV_ACCOUNT_ID = 25176
and cv.CONV_DAY between '2016-8-01' AND '2016-08-07'
and AMP_CLICK_STATUS_ID = 1
AND pixel_id IN
(SELECT DISTINCT conversion_pixel_id
FROM
ampx.campaign_event_funnel ef
JOIN ampx.campaign cp ON
cp.id = ef.campaign_id
AND cp.campaign_status_id = 1
WHERE
ef.account_id IN(25176)
AND include_optimization = 1 )
group by 1
order by 1 asc
This yields 170 which is the correct answer and the I want. This, on the other hand, displays 157.
select count(distinct cv.CLICK_ID)
from
clickcache.click cc
right join(
select distinct cv.CLICK_ID, cv.CONV_DAY, cv.PIXEL_ID
from clickcache.CONVERSION cv
where cv.CLICK_ID IS NOT NULL) cv ON cv.CLICK_ID = cc.ID
where cc.ADV_ACCOUNT_ID = 25176
and cv.CONV_DAY between '2016-8-01' AND '2016-08-07'
and AMP_CLICK_STATUS_ID = 1
AND pixel_id IN
(SELECT DISTINCT conversion_pixel_id
FROM
ampx.campaign_event_funnel ef
JOIN ampx.campaign cp ON
cp.id = ef.campaign_id
AND cp.campaign_status_id = 1
WHERE
ef.account_id IN(25176)
AND include_optimization = 1 )
My question is why do I get this discrepancy and how to fix it to get a proper count?
Thank you!
Your count dependents from right query, maybe you have duplicate row?
example
table1
id name value
1 2 3
table2
id name value
1 4 5
2 6 3
1 6 3
right join tables on value get result
select * from table1 a right join table2 b on a.value = b.value
1 2 3 2 6 3
1 2 3 1 6 3
select count(distinct a.value)
from (select a.id, a.name, a.value, b.id, b.name, b.value
from table1 a right join table2 b on a.value = b.value)
result is 1
select b.id, count(distinct a.value)
from (select a.id, a.name, a.value, b.id, b.name, b.value
from table1 a right join table2 b on a.value = b.value group)
group by b.id
result is two rows
2 1
1 1
My guess is that, you have a problem for this reason.

SQL Server, selecting from 2 columns from different tables

I have these columns from 2 tables
Table1 Table2
Code ID Code ID
A 1 A 1
B 1 B 1
C 1 C 1
D 1
E 1
My query:
Select
a.id, a.code, b.code
from
Table1 a, Table2 b
where
a.id = '1' and a.id = b.id
What I expected
ID code code
1 A A
1 B B
1 C C
1 D NULL
1 E NULL
What I got
ID code code
1 A A
1 B A
1 C A
1 D A
1 E A
1 A B
1 B B
1 C B
....
Any ideas? distinct didn't help
Thanks
Well, all the ID's in both tables are 1, so by joining on ID you'll get the cartesian product of both tables.
Instead, you'll need to do a left outer join based on Table1.Code:
Select a.id, a.code, b.code
from Table1 a LEFT OUTER JOIN Table2 b
on a.code = b.code
where a.id = '1';
You need to do a LEFT OUTER JOIN instead of a Cartesian Product
SELECT a.Id, a.Code, b.Code FROM Table1 a
LEFT OUTER JOIN Table2 b ON a.Code = b.Code
WHERE a.Id = '1'
A LEFT OUTER JOIN returns all rows from the left-hand side of the join (in this case Table 1) regardless of whether there is a matching record in the table on the right-hand side of the join (in this case Table 2). Where there is no match a NULL is returned for b.Code as per your requirements.
Reference OUTER JOINS

Sql Query to find sum from different tables

Hi i require a help in writing a query.
Tables are:
tblStandard1students
tblStandard2students
tblStandard1students
tblDivision
tblCandidateinfo
tblStandard1students,tblStandard2students,tblStandard1studentstbl contain information about students enrolled in standard 1,2 and 3
tblStandars1students
Candid admitted
1 Y
2 N
3 Y
tblDivision contains only 2 columns
ID Division
1 A
2 B
3 C
tblCandidateinfo
Candid gender Division
1 M 1
2 F 2
and so on...
Now I want the table like this
Division Students(Standard1) Students(Standard2) Students(Standard3)
M F M F M F
------------------------------------------------------------------------
A 1 0 0 0 0 1
B 2 2 3 3 4 4
C 1 0 0 0 0 0
I tried this following query:
SELECT Division,
( SELECT count(*)
FROM tblStandard1students A
INNER JOIN tblCandidateinfo B ON A.Candid=B.Candid
INNER JOIN tblDivision C ON C.ID=B.Division) AS Students(Standard1),
( SELECT count(*)
FROM tblStandard2students A
INNER JOIN tblCandidateinfo B ON A.Candid=B.Candid
INNER JOIN tblDivision C ON C.ID=B.Division) AS Students(Standard2),
( SELECT count(*)
FROM tblStandard3students A
INNER JOIN tblCandidateinfo B ON A.Candid=B.Candid
INNER JOIN tblDivision C ON C.ID=B.Division ) AS Students(Standard3)
FROM tblDivision Z
but this is only half the query i din segregate it gender wise...help me to complete it.
;WITH combined AS
(
SELECT ci.Division, 'Students(Standard1) ' + ci.gender AS grp
FROM tblCandidateInfo ci
INNER JOIN tblStandard1students s ON ci.Candid = s.Candid
UNION ALL
SELECT ci.Division, 'Students(Standard2) ' + ci.gender AS grp
FROM tblCandidateInfo ci
INNER JOIN tblStandard2students s ON ci.Candid = s.Candid
UNION ALL
SELECT ci.Division, 'Students(Standard3) ' + ci.gender AS grp
FROM tblCandidateInfo ci
INNER JOIN tblStandard1studentstbl s ON ci.Candid = s.Candid
)
SELECT Division,
[Students(Standard1) M], [Students(Standard1) F],
[Students(Standard2) M], [Students(Standard2) F],
[Students(Standard3) M], [Students(Standard3) F]
FROM
(
SELECT d.Division, grp
FROM tblDivision d
LEFT OUTER JOIN combined c ON d.ID = c.Division
) x
PIVOT
(
COUNT(grp)
FOR grp IN ([Students(Standard1) M], [Students(Standard1) F],
[Students(Standard2) M], [Students(Standard2) F],
[Students(Standard3) M], [Students(Standard3) F])
) y
ORDER BY Division
SELECT divison.Division ,IFNULL(stander1.M,0),IFNULL(stander1.F,0) FROM test.tblDivision divison
Left join (SELECT division ,count( case gender when 'M' then 1 else null end) as M,count( case gender when 'F' then 1 else null end) as F
FROM
test.tblCandidateinfo tc inner join test.tblStandars1students ts1
ON tc.Candid=ts1.Candid
group by division) as stander1 on stander1.division= divison.id
group by divison.id
;
Insted of IFNULL use ISNULL and
take left join for all standar tables