How to select record in 1 to many relation - sql

I have a contact table and contactphone tables.
Each record in contacts can have many records in contactphone but only 1 can be primary at most (primary is Boolean but can be also NULL)
Contacts:
cid name address
1 Jack A
2 Bill B
3 Mor C
4 Jeff D
ContactsPhones:
cpid cid phone primary
1 1 140 True
2 1 150
3 2 170
I need to write a query that returns all contacts with their details, if they have more than 1 phone then present only primary.
Output:
cid name address phone
1 Jack A 140
2 Bill B 170
3 Mor C
4 Jeff D
What is most easiest way to do it?
Note that primary can have (True,False, Null)

This is a bit tricky. I think the easiest method is distinct on:
SELECT DISTINCT ON (cid) cid, name, address, cp.phone
FROM Contacts c LEFT JOIN
ContactsPhones cp
ON c.cid = cp.cpid
ORDER BY cid, (case when primary then 1 else 2 end);

You can use a LEFT JOIN to a derived table that uses ROW_NUMBER to specify more suitable record:
SELECT cid, name, address, phone
FROM Contacts AS c
LEFT JOIN (
SELECT phone, cpid,
ROW_NUMBER() OVER(PARTITION BY cpid
ORDER BY CASE
WHEN primary THEN 0
ELSE 1
END) AS rn
FROM ContactsPhones) AS cp
ON c.cid = cp.cpid AND cp.rn = 1

Related

SQL help i need to find the inventory remaining in my office

In sql help i have 3 tables, table one is asset table which is as follow
id
asset_code
asset_name
asset_group
asset_quantity
1
A001
demo asset
4
5
2
A002
demo asset 2
6
3
and another table is asset_allocation
id
asset_id
allocated_quantity
allocated_location
1
1
2
IT office
2
1
1
main hall
the last table is asset_liquidated which will present assets that are no longer going to be used
id
asset_id
liquidated_quantity
1
1
2
2
1
1
lets say i have 5 computers and i have allocated 3 computers and 1 is no longer going to be used so i should be remaining with 1 computer so now how do i make sql auto generate this math for me
You need to use aggregation and the join your tables -
SELECT id, asset_code, asset_name, asset_group, asset_quantity,
asset_quantity - COALESCE(AA.allocated_quantity, 0) - COALESCE(AL.liquidated_quantity, 0) available_quantity
FROM asset A
LEFT JOIN (SELECT asset_id, SUM(allocated_quantity) allocated_quantity
FROM asset_allocation
GROUP BY asset_id) AA ON A.id = AA.asset_id
LEFT JOIN (SELECT asset_id, SUM(liquidated_quantity) liquidated_quantity
FROM asset_liquidated
GROUP BY asset_id) AL ON A.id = AL.asset_id
This query will give you -1 as available_quantity for asset_id 1 as you have only 5 available, 3 of them are allotted and 3 are liquidated as per your sample data.
Please see if this helps
SELECT
asset_quantity AS Total_Assets
,ISNULL(allocated_quantity, 0) allocated_quantity
,ISNULL(liquidated_quantity, 0) liquidated_quantity
FROM asset
LEFT OUTER JOIN (
SELECT
asset_id, SUM(allocated_quantity) AS allocated_quantity
FROM asset_allocation
GROUP BY asset_id
) asset_allocation2
ON asset_allocation2.asset_id = asset.id
LEFT OUTER JOIN (
SELECT
asset_id, SUM(liquidated_quantity) AS liquidated_quantity
FROM asset_liquidated
GROUP BY asset_id
) asset_liquidated 2
ON asset_liquidated 2.asset_id = asset.id

Not able to generate SQL Join query for Postgres

I have a table structure like this and I am using Postgres
ContactPhoneRelation
id
ContactId
PhoneId
1
123
999
2
123
998
I have another table GroupTable
id
groupId
ContactId
PhoneId
1
1
123
999
2
2
123
999
3
2
123
998
I am trying to fetch the data from ContactPhoneRelation which does not exist in groupId 1 and ContactId is 123, So I want to query such that where groupId is 1 and ContactId is 123 and ContactId phoneId both does not exists in groupId 1
So in return, it should give me this result
id
contactId
PhoneId
2
123
998
If I query for groupId 2, It should give me 0 rows in return.
I tried this query but it gave me the opposite data.
select * from ContactPhoneRelation cp
left join GroupTable gt on gt.ContactId = cp.ContactId
where cp.contactId = '123' and gt.groupId = 1
In all honesty, I don't understand what you are trying to achive. But maybe this is because of language issues.
For better understanding I added an example on db<>fiddle: Link
Edit (29.09.2021; 16:49):
SELECT
c.*
FROM
(
SELECT
b.groupID
,a.ContactID
,a.PhoneID
FROM
(SELECT DISTINCT
ContactId
,PhoneID
FROM
ContactPhoneRelation
) a
FULL JOIN
(SELECT DISTINCT
groupID
FROM
GroupTable
) b ON (1=1)
ORDER BY groupID, ContactID, phoneID
) c
LEFT OUTER JOIN GroupTable d ON (
c.groupID = d.groupID
AND c.ContactID = d.ContactID
AND c.PhoneID = d.PhoneID
)
WHERE
d.groupID IS NULL

how left outer join works for table have null values

I have four tables: person and booking, booking_detail,room_pricing where person_id used as foreign key in the booking table and person_id is used as foreign key in the booking_detail table. where room_id is used as foreign_key in booking_detail
Supposing I want to show all the booking_id from booking table and their corresponding total income from rooms on each booking including those who doesn't exist in `` table such as id 1 ,i am using oracle database
person
person_id name
1 xyz
2 abc
3 jkl
booking
booking_id person_id
1 1
2 3
3 1
booking_details
person_id roomid
2 201
3 303
3 303
room_pricing
room_id price
201 $ 100
302 $ 100
303 $ 200
final table should be like this
booking_id total
1 0
2 400$
3 0
Try the following using left join
select
b.booking_id,
coalesce(sum(total), 0) as total
from booking b
left join booking_details bd
on b.person_id = bd.person_id
left join room_pricing rp
on bd.room_id = rp.room_id
group by
b.booking_id
You want a left join with aggregation:
select p.person_id, coalesce(sum(o.total), 0)
from person p left join
order o
on o.person_id = p.person_id
group by p.person_id;

Selecting objects that are associated with similar datasets

I'm trying to select all company rows from a [Company] table that share with at least one other company, the same number of employees (from an [Employee] table that has a CompanyId column), where each group of respective employees share the same set of LocationIds (a column in the [Employee] table) and in the same proportion.
So, for instance, two companies with three employees each that have the locationIds 1,2, and 2, would be selected by this query.
[Employee]
EmployeeId | CompanyId | LocationId |
========================================
1 | 1 | 1
2 | 1 | 2
3 | 1 | 2
4 | 2 | 1
5 | 2 | 2
6 | 2 | 2
7 | 3 | 3
[Company]
CompanyId |
============
1 |
2 |
3 |
Returns the CompanyIds:
======================
1
2
CompanyIds 1 and 2 are selected because they share in common with at least one other company: 1. the number of employees (3 employees); and 2. the number/proportion of LocationIds associated with those employees (1 employee has LocationId 1 and 2 employees have LocationId 2).
So far I think I want to use a HAVING COUNT(?) > 1 statement, but I'm having trouble working out the details. Does anyone have any suggestions?
This is ugly, but the only way I can think of to do it:
;with CTE as (
select c.Id,
(
select e.Location, count(e.Id) [EmployeeCount]
from Employee e
where e.IdCompany=c.Id
group by e.Location
order by e.Location
for xml auto
) LocationEmployeeData
from Company c
)
select c.Id
from Company c
join (
select x.LocationEmployeeData, count(x.Id) [CompanyCount]
from CTE x
group by x.LocationEmployeeData
having count(x.Id) >= 2
) y on y.LocationEmployeeData = (select LocationEmployeeData from CTE where Id = c.Id)
See fiddle: http://www.sqlfiddle.com/#!6/6bc16/5
It works by encoding the Employee count per Location data (multiple rows) into an xml string for each Company.
The CTE code on its own:
select c.Id,
(
select e.Location, count(e.Id) [EmployeeCount]
from Employee e
where e.IdCompany=c.Id
group by e.Location
order by e.Location
for xml auto
) LocationEmployeeData
from Company c
Produces data like:
Id LocationEmployeeData
1 <e Location="1" EmployeeCount="2"/><e Location="2" EmployeeCount="1"/>
2 <e Location="1" EmployeeCount="2"/><e Location="2" EmployeeCount="1"/>
3 <e Location="3" EmployeeCount="1"/>
Then it compares companies based on this string (rather than trying to ascertain whether multiple rows match, etc).
An alternative solution could look like this. However it also requires performance testing in advance (I don't feel quite confident with <> type join).
with List as
(
select
IdCompany,
Location,
row_number() over (partition by IdCompany order by Location) as RowId,
count(1) over (partition by IdCompany) as LocCount
from
Employee
)
select
A.IdCompany
from List as A
inner join List as B on A.IdCompany <> B.IdCompany
and A.RowID = B.RowID
and A.LocCount = B.LocCount
group by
A.IdCompany, A.LocCount
having
sum(case when A.Location = B.Location then 1 else 0 end) = A.LocCount
Related fiddle: http://sqlfiddle.com/#!6/d9f2e/1

SQL join - join first no zero element if possible

I have a data structure something like this:
Company:
ID Name
1 A
2 B
3 C
4 D
5 E
Contact
ID CompanyID Phone
1 1 (12)111
2 1 (12)222
3 2 NULL
4 2 (12)333
5 3 NULL
6 5 (12)444
I need to match company with its phone - one record for company. In 70% of cases in my data a company has one contact with one phone and this is easy. But sometimes there are more contacts and some phones are null.
So my logic would be:
- if company has more than 1 contact take 1st not null phone (by ID)
- if it is not possible then phone=null
So results of my query would be
A (12)111
B (12)333
C NULL
D NULL
E (12)444
It's probably easy but I got stuck with it ;) Thanks for any help
select a.[Name], min(b.[Phone])
from Company a
JOIN Contact b
on a.ID = b.CompanyID
then, optionals, e.g.
WHERE b.[Phone] is not null
then group
GROUP BY a.[Name]
Try the following:
SELECT com.name
,MIN(case when isnull(con.phone, 'null') != 'null' then con.phone end) a
FROM Company com
LEFT JOIN Contact con on com.id=con.companyid
GROUP BY com.name