How to remove duplicate values from oracle join? - sql

I want to create a view that present only the results and not present the duplicates, I have 3 tables in oracle database:
The first table contain general information about a person
+-----------+-------+-------------+
| ID | Name | Birtday_date|
+-----------+-------+-------------+
| 1 | Byron | 12/10/1998 |
| 2 | Peter | 01/11/1973 |
| 4 | Jose | 05/02/2008 |
+-----------+-------+-------------+
The second table contain information about a telephone of the people in the first table.
+-------+----------+----------+----------+
| ID |ID_Person |CELL_TYPE | NUMBER |
+-------+- --------+----------+----------+
| 1221 | 1 | 3 | 099141021|
| 2221 | 1 | 2 | 099091925|
| 3222 | 1 | 1 | 098041013|
| 4321 | 2 | 1 | 088043153|
| 4561 | 2 | 2 | 090044313|
| 5678 | 4 | 1 | 092049013|
| 8990 | 4 | 2 | 098090233|
+----- -+----------+----------+----------+
The Third table contain information about a email of the people in the first table.
+------+----------+----------+---------------+
| ID |ID_Person |MAIL_TYPE | Email |
+------+- --------+----------+---------------+
| 221 | 1 | 1 |jdoe#aol.com |
| 222 | 1 | 2 |jdoe1#aol.com |
| 421 | 2 | 1 |xx12#yahoo.com |
| 451 | 2 | 2 |dsdsa#gmail.com|
| 578 | 4 | 1 |sasaw1#sdas.com|
| 899 | 4 | 2 |cvcvsd#wew.es |
+------+----------+----------+---------------+
if i do a inner join with this tables the result will do something like that
+-----+-------+-------------+----------+----------+----------+----------------+
| ID | Name | Birtday_date| CELL_TYPE| NUMBER |MAIL_TYPE|Email |
+-----+-------+-------------+----------+----------+----------+----------------+
| 1 | Byron | 12/10/1998 | 3 | 099141021|1 |jdoe#aol.com |
| 1 | Byron | 12/10/1998 | 3 | 099141021|2 |jdoe1#aol.com |
| 1 | Byron | 12/10/1998 | 2 | 099091925|1 |jdoe#aol.com |
| 1 | Byron | 12/10/1998 | 2 | 099091925|2 |jdoe1#aol.com |
| 1 | Byron | 12/10/1998 | 1 | 098041013|1 |jdoe#aol.com |
| 1 | Byron | 12/10/1998 | 1 | 098041013|2 |jdoe1#aol.com |
| 2 | Peter | 01/11/1973 | 1 | 088043153|1 |xx12#yahoo.com |
| 2 | Peter | 01/11/1973 | 1 | 088043153|2 |dsdsa#gmail.com |
| 2 | Peter | 01/11/1973 | 2 | 090044313|1 |xx12#yahoo.com |
| 2 | Peter | 01/11/1973 | 2 | 090044313|2 |dsdsa#gmail.com |
| 4 | Jose | 05/02/2008 | 1 | 088043153|1 |sasaw1#sdas.com |
| 4 | Jose | 05/02/2008 | 1 | 088043153|2 |cvcvsd#wew.es |
| 4 | Jose | 05/02/2008 | 2 | 088043153|1 |sasaw1#sdas.com |
| 4 | Jose | 05/02/2008 | 2 | 088043153|2 |cvcvsd#wew.es |
+-----+-------+-------------+----------+----------+----------+----------------+
So the result that i will to present in a view is the next
+-----+-------+-------------+----------+----------+----------+----------------+
| ID | Name | Birtday_date| CELL_TYPE| NUMBER |MAIL_TYPE|Email |
+-----+-------+-------------+----------+----------+----------+----------------+
| 1 | Byron | 12/10/1998 | 3 | 099141021|1 |jdoe#aol.com |
| 1 | Byron | 12/10/1998 | | |2 |jdoe1#aol.com |
| 1 | Byron | 12/10/1998 | 2 | 099091925| | |
| 1 | Byron | 12/10/1998 | 1 | 098041013| | |
| 2 | Peter | 01/11/1973 | 1 | 088043153|1 |xx12#yahoo.com |
| 2 | Peter | 01/11/1973 | | |2 |dsdsa#gmail.com |
| 2 | Peter | 01/11/1973 | 2 | 090044313| | |
| 4 | Jose | 05/02/2008 | 1 | 092049013|1 |sasaw1#sdas.com |
| 4 | Jose | 05/02/2008 | | |2 |cvcvsd#wew.es |
| 4 | Jose | 05/02/2008 | 2 | 098090233| | |
+-----+-------+-------------+----------+----------+----------+----------------+
I tried to achieve a similar output using
case
when row_number() over (partition by table1.id order by table2.type) = 1
then table1.value
end
as "VALUE"
But the result is nothing that I expect and some rows they repeats

What you need to do is enumerate the rows and then join on those enumerations. This is tricky, because you don't know how many are in each list. Well, there is another method using conditional aggregation:
select p.id, p.name, p.birthday,
max(cell_type) as cell_type, max(number) as number,
max(mail_type) as mail_type, max(email) as email
from person p left join
((select id_person, cell_type, number,
null as mail_type, null as email,
row_number() over (partition by id_person order by number) as seqnum
from phones
) union all
(select id_person, null as cell_type, null as number,
mail_type, email,
row_number() over (partition by id_person order by email) as seqnum
from emails
)
) pe
on pe.id_person = p.id_person
group by p.id, p.name, p.birthday, pe.seqnum

Hope this helps.
Create table person(ID int ,Name varchar(20), Birtday_date date)
Insert into person values
(1,'Byron' ,'12/10/1998'),
(2,'Peter' ,'01/11/1973'),
(4,'Jose ' ,'05/02/2008')
Create table phones (ID int,ID_Person int,CELL_TYPE int,NUMBER float)
Insert into phones values
(1221, 1 , 3,099141021),
(2221, 1 , 2,099091925),
(3222, 1 , 1,098041013),
(4321, 2 , 1,088043153),
(4561, 2 , 2,090044313),
(5678, 4 , 1,092049013),
(8990, 4 , 2,098090233)
Create table emails(ID int,ID_Person int, MAIL_TYPE int, Email varchar(100))
Insert into emails values
(221, 1 , 1, 'jdoe#aol.com '),
(222, 1 , 2, 'jdoe1#aol.com '),
(421, 2 , 1, 'xx12#yahoo.com '),
(451, 2 , 2, 'dsdsa#gmail.com'),
(578, 4 , 1, 'sasaw1#sdas.com'),
(899, 4 , 2, 'cvcvsd#wew.es ')
select p.id, p.name, p.Birtday_date,
case when Lag(number) over(partition by p.id order by p.id,pe.id) = number then null else cell_type end as cell_type,
case when Lag(number) over(partition by p.id order by p.id,pe.id) = number then null else number end as number,
mail_type as mail_type, email as email
from person p left join
(select pp.ID_Person, cell_type, number, mail_type, email,pp.id from
(select ID_Person, cell_type, number,id,
row_number() over (partition by ID_Person order by id ) as seqnum
from phones
) pp left join
(select ID_Person,
mail_type, email, 1 as seqnum
from emails
)e on pp.ID_Person = e.ID_Person and pp.seqnum = e.seqnum
) pe
on pe.ID_Person = p.Id
order by p.id, pe.id

Related

Summarize count of multi table in single SQL query

I have three table with below details:
Table 1: worklog
+-----------+------------+-------------+
| worklogid | technician | description |
+-----------+------------+-------------+
| 1 | john | some text |
+-----------+------------+-------------+
| 2 | jack | some text |
+-----------+------------+-------------+
| 3 | john | some text |
+-----------+------------+-------------+
| 4 | jenifer | some text |
+-----------+------------+-------------+
Table 2: task
+--------+-------+-------------+
| taskid | owner | description |
+--------+-------+-------------+
| 1 | john | some text |
+--------+-------+-------------+
| 2 | john | some text |
+--------+-------+-------------+
| 3 | john | some text |
+--------+-------+-------------+
| 4 | jack | some text |
+--------+-------+-------------+
Table 3: request
+-----------+------------+-----------+-------------+
| requestid | technician | title | description |
+-----------+------------+-----------+-------------+
| 1 | john | some text | ... |
+-----------+------------+-----------+-------------+
| 2 | sara | some text | ... |
+-----------+------------+-----------+-------------+
| 3 | john | some text | ... |
+-----------+------------+-----------+-------------+
| 4 | jack | some text | ... |
+-----------+------------+-----------+-------------+
Now I need to SQL query for this result:
+------------+------------------+---------------+------------------+
| technician | count(worklogid) | count(taskid) | count(requestid) |
+------------+------------------+---------------+------------------+
| john | 2 | 3 | 2 |
+------------+------------------+---------------+------------------+
| jack | 1 | 1 | 1 |
+------------+------------------+---------------+------------------+
| jenifer | 1 | 0 | 0 |
+------------+------------------+---------------+------------------+
| sara | 0 | 0 | 1 |
+------------+------------------+---------------+------------------+
What should I do?
One method is to just use union all and aggregation:
select techician, sum(is_workid), sum(is_taskid), sum(is_requestid)
from ((select technician, 1 as is_workid, 0 as is_taskid, 0 as is_requestid
from worklog
) union all
(select owner, 0, 1, 0
from task
) union all
(select technician, 0, 0, 1
from request
)
) t
group by technician;
In Postgres, you can also aggregate before joining:
select *
from (select technician, count(*) as num_workid
from worklog
group by technician
) w full join
(select owner as technician, count(*) as num_task
from task
group by owner
) t
using (technician) full join
(select technician, count(*) as num_request
from request
group by technician
) w
using (technician);
With a full join, I find that using is simpler than on clauses. But the name needs to be the same in all the tables.

Distincted ids for grouped values

I want to count the distinct ids in each numb and store them in a column :
Tried this:
WITH T AS(
SELECT
MAX(CASE WHEN LOGS like'CAR%' then REPLACE(LOGS,'CAR-','')end)as CAR,
MAX(CASE WHEN LOGS like 'MOT%' then REPLACE(LOGS,'MOT-','')end)as MOTO,
MAX(CASE WHEN LOGS like 'BICYCLE%' then REPLACE(LOGS,'BICYCLE-','')end)as BICYCLE,
MAX(CASE WHEN LOGS like 'SHIP%' then REPLACE(LOGS,'SHIP-','')end)as SHIP,
ID,
ORIG,
DATE_ID ,
NUMB,
STEPS
from dbo.test
group by ORIG,DATE_ID,ID ,NUMB,STEPS
)
SELECT ID,ORIG,NUMB,STEPS,DATE_ID,CAR,MOTO,BICYCLE,SHIP,
(SELECT COUNT(DISTINCT ID) FROM dbo.test tp WHERE ORIG= '4567') as COUNTER
from t
where ORIG= '4567'
and NUMB in('1515','1921','2121')
GROUP BY ID,ORIGIN_URI,NUMB,STEPS,DATE_ID,CAR,MOTO,BICYCLE,SHIP
Receive this output:
+----+--------------+--------+-------+----------+-------+------+---------+------+---------+
| ID | ORIG | NUMB | STEPS | DATE_ID | CAR | MOTO | BICYCLE | SHIP | COUNTER |
+----+--------------+--------+-------+----------+-------+------+---------+------+---------+
| 1 | 4567 | 1515 | 1 | 20201010 | HONDA | NULL | NULL | NULL | 3 |
| 1 | 4567 | 1515 | 2 | 20201010 | HONDA | NULL | NULL | NULL | 3 |
| 1 | 4567 | 1515 | 3 | 20201010 | HONDA | NULL | NULL | NULL | 3 |
| 2 | 4567 | 1921 | 1 | 20201111 | NULL | KTM | NULL | NULL | 3 |
| 3 | 4567 | 2121 | 1 | 20201231 | NULL | NULL | NULL | BOAT | 3 |
| 3 | 4567 | 2121 | 2 | 20201231 | NULL | NULL | NULL | BOAT | 3 |
| 3 | 4567 | 2121 | 3 | 20201231 | NULL | NULL | NULL | BOAT | 3 |
| 3 | 4567 | 2121 | 4 | 20201231 | NULL | NULL | NULL | BOAT | 3 |
+----+--------------+--------+-------+----------+-------+------+---------+------+---------+
As you can see COUNTER columns has the count of distincted ids but for all NUMB
I want to output this:
+----+--------------+--------+-------+----------+-------+------+---------+------+---------+
| ID | ORIG | NUMB | STEPS | DATE_ID | CAR | MOTO | BICYCLE | SHIP | COUNTER |
+----+--------------+--------+-------+----------+-------+------+---------+------+---------+
| 1 | 4567 | 1515 | 1 | 20201010 | HONDA | NULL | NULL | NULL | 2 |
| 1 | 4567 | 1515 | 2 | 20201010 | HONDA | NULL | NULL | NULL | 2 |
| 2 | 4567 | 1515 | 1 | 20201010 | HONDA | NULL | NULL | NULL | 2 |
| 2 | 4567 | 1921 | 1 | 20201111 | NULL | KTM | NULL | NULL | 1 |
| 3 | 4567 | 2121 | 1 | 20201231 | NULL | NULL | NULL | BOAT | 2 |
| 3 | 4567 | 2121 | 2 | 20201231 | NULL | NULL | NULL | BOAT | 2 |
| 3 | 4567 | 2121 | 3 | 20201231 | NULL | NULL | NULL | BOAT | 2 |
| 1 | 4567 | 2121 | 1 | 20201231 | NULL | NULL | NULL | BOAT | 2 |
+----+--------------+--------+-------+----------+-------+------+---------+------+---------+
1515 has 2 ids
1921 has 1 id
2121 has 2 ids
I tried also to place a GROUP BY NUMB inside (SELECT COUNT(DISTINCT ID) FROM dbo.test tp WHERE ORIG= '4567') but didn't work.
What you seem to want is:
count(distinct steps) over (partition by orig, numb)
Alas, SQL Server doesn't support count(distinct) with window functions.
Happily, there is an easy workaround (which begs the question as to why the above syntax is not supported):
(dense_rank() over (partition by orig, numb order by steps asc) +
dense_rank() over (partition by orig, numb order by steps desc) - 1
) as counter

Get Field Hierachy

I have the following tables and I want to get the quantity of users by country:
+--------+------+:
| user | zone |
+--------+------+
| Paul | 7 |
+--------+------+
| John | 5 |
+--------+------+
| Peter | 6 |
+--------+------+
| Frank | 5 |
+--------+------+
| Silvia | 2 |
+--------+------+
| Carl | 4 |
+--------+------+
| Mark | 3 |
+--------+------+
Regions
+---------+-----------------+----------+--+
| zone_id | zone_name | idUpzone | |
+---------+-----------------+----------+--+
| 1 | Global | null | |
+---------+-----------------+----------+--+
| 2 | US | 1 | |
+---------+-----------------+----------+--+
| 3 | Florida | 2 | |
+---------+-----------------+----------+--+
| 4 | Orlando | 3 | |
+---------+-----------------+----------+--+
| 5 | China | 1 | |
+---------+-----------------+----------+--+
| 6 | Orlando Sector | 4 | |
+---------+-----------------+----------+--+
| 7 | Beijing | 5 | |
+---------+-----------------+----------+--+
so I get something like this
+---------+-----+
| Country | QTY |
+---------+-----+
| US | 4 |
+---------+-----+
| China | 3 |
+---------+-----+
Use a recursive CTE to get the highest level and then join:
with cte as (
select zone_id, zone_id as top_zone_id, zone_name as top_zone_name, 1 as lev
from regions
where parent_zone_id = 1
union all
select r.zone_id, cte.top_zone_id, top_zone_name, lev + 1
from cte join
regions r
on r.idUpzone = cte.zone_id
)
select cte.top_zone_name, count(*)
from users u join
cte
on u.zone = cte.zone_id
group by cte.top_zone_name;
Try this out:
SELECT
r.zone_name AS Contry, COUNT(*) QTY
FROM (
SELECT * FROM users u
INNER JOIN regions r ON u.zone = r.zone_id
) a
GROUP BY r.zone_name

SQL select records based on not existing record

I have a table with records like this:
ID | NAME | PHASE_ID | OPERATION_ID | EXT_ID |
---+------+----------+--------------+--------+
1 | john | 5 | 5019 | aa-aaa |
2 | ann | 1 | 5048 | aa-aaa |
3 | mary | 1 | 5048 | bb-bbb |
4 | zack | 5 | 5019 | cc-ccc |
5 | paul | 1 | 5048 | cc-ccc |
6 | zoe | 2 | 5555 | aa-aaa |
7 | luke | 3 | 6666 | bb-bbb |
and I want to select all names from records having PHASE_ID=1 and OPERATION_ID=5048 for which there is no record with the same EXT_ID that have PHASE_ID=5 and OPERATION_ID=5019. In this table there is many combinations of PHASE_ID and OPERATION_ID for the same EXT_ID.
In this table name that fulfill this select would be only mary.
Any help composing that select would be appreciated.
Sounds like not exists:
select distinct name
from t
where phase_id = 1 and operation_id = 5048 and
not exists (select 1
from t t2
where t2.ext_id = t.ext_id and
t2.phase_id = 5 and t2.operation_id = 5019
);

Need query for JOIN four tables with some conditions?

I have the following four tables:
1) mls_user
2) mls_category
3) bonus_point
4) mls_entry
In mls_user table values are like below:
*-------------------------*
| id | store_id | name |
*-------------------------*
| 1 | 101 | sandeep |
| 2 | 101 | gagan |
| 3 | 102 | santosh |
| 4 | 103 | manu |
| 5 | 101 | jagveer |
*-------------------------*
In mls_category table values are like below:
*---------------------------------*
| cat_no | store_id | cat_value |
*---------------------------------*
| 20 | 101 | 1 |
| 21 | 101 | 4 |
| 30 | 102 | 1 |
| 31 | 102 | 2 |
| 40 | 103 | 1 |
| 41 | 103 | 1 |
*---------------------------------*
In bonus_point table values are like below:
*-----------------------------------*
| user_id | store_id | bonus_point |
| 1 | 101 | 10 |
| 4 | 101 | 5 |
*-----------------------------------*
In mls_entry table values are like below:
*-------------------------------------------------------*
| user_id | store_id | category | distance | status |
*-------------------------------------------------------*
| 1 | 101 | 20 | 10 | Approved |
| 1 | 101 | 21 | 40 | Approved |
| 1 | 101 | 20 | 10 | Approved |
| 2 | 101 | 20 | 5 | Approved |
| 3 | 102 | 30 | 10 | Approved |
| 3 | 102 | 31 | 80 | Approved |
| 4 | 101 | 20 | 15 | Approved |
*-------------------------------------------------------*
And I want below output:
*--------------------------------------------------*
| user name | Points | bonus Point | Total Point |
*--------------------------------------------------*
| Sandeep | 30 | 10 | 40 |
| Santosh | 30 | 0 | 30 |
| Manu | 15 | 5 | 20 |
| Gagan | 5 | 0 | 5 |
| Jagveer | 0 | 0 | 0 |
*--------------------------------------------------*
I tell the calculation of how the points will come for user Sandeep.
Points = ((10+10)/1 + 40/4)=30
Here 1 and 4 is cat value which comes from mls_category.
I am using below code for a particular user but when i
SELECT sum(t1.totald/c.cat_value) as total_distance
FROM mls_category c
join (
select sum(distance) totald, user_id, category
FROM mls_entry
WHERE user_id=1 AND store_id='101' AND status='approved'
group by user_id, category) t1 on c.cat_no = t1.category
I have created tables in online for checking
DEMO
Computing the points (other than the bonus points) requires a separate join between the mls_entry and mls_category tables. I would do this in a separate subquery, and then join this to the larger query.
Here is one approach:
SELECT
u.name,
COALESCE(t1.points, 0) AS points,
COALESCE(b.bonus_point, 0) AS bonus_points,
COALESCE(t1.points, 0) + COALESCE(b.bonus_point, 0) AS total_points
FROM mls_user u
LEFT JOIN
(
SELECT e.user_id, SUM(e.distance / c.cat_value) AS points
FROM mls_entry e
INNER JOIN mls_category c
ON e.store_id = c.store_id AND e.category = c.cat_no
GROUP BY e.user_id
) t1
ON u.id = t1.user_id
LEFT JOIN bonus_point b
ON u.id = b.user_id
ORDER BY
total_points DESC;
This is the output I am getting from the above query in the demo you setup:
The output does not match exactly, because you have (perhaps) a typo in Santosh's data in your question, or otherwise the expected output in your question has a typo.