Query to match multiple column values with multiple rows - postgresql

I have the following table in my postgresql 9.1
Table contact:
contact_id phone mobile
1 123 456
2 111 222
3 333 123
4 222 444
Table role:
contact_fk exchange
7 1
8 2
1 4
5 5
2 4
4 5
I need the result like:
contact_id phone mobile exchange
1 123 456 4
3 333 123 4
2 111 222 4
I want all the contact data whose mobile field data is in any other contacts phone field and the user must be in exchange 4, which is available in contact_role table
FYI: the contact table contains around 50k rows so joining the contact table to itself taking a lot time, so we must apply the contact_role condition together.

Joining 50k table with 2 columns (where contact_fk is probably primary) shouldn't take long time.
SELECT t1.contact_id, t1.phone, t1.mobile, t2.exchange
FROM contact as t1
JOIN role as t2 ON t1.contact_id = t2.contact_fk
WHERE t2.exchange = 4
Or if you have index on exchange column, this probably will be faster:
SELECT t1.exchange, t2.contact_id, t2.phone, t2.mobile
FROM role as t1
JOIN contact as t2 ON t1.contact_fk = t2.contact_id
WHERE t1.exchange = 4

Here is the answer
select t1.contact_id,
t1.phone,
t1.mobile,
t2.phone,
t2.exchange
from contact t1
inner join (select contact_id,
mobile,
phone,
exchange
from contact, contact_role
where contact.contact_id = contact_fk
and exchange = 4
) t2
on t1.mobile = t2.phone
and t1.mobile != '';

Related

Listing rows which have the same value as an entred id in a certain column in postgresql

I have a following table structure in Postgres:
id
conversation_id
member_id
3
2
73
4
2
1
5
2
2
6
3
1
8
3
73
How I can select all rows of members with the same conversation_id as the entred member_id (2) for example?
For the example above it will return rows 3, 4, 5
I tried this one query, but it does not return what I expect:
SELECT cu.id, cu.member_id
FROM conversation c, conversation_user cu
WHERE cu.conversation_id=c.id
GROUP BY cu.member_id, cu.id
having cu.member_id = 2
I found a solution:
SELECT cu.id
FROM conversation c, conversation_user cu
WHERE cu.conversation_id=c.id
AND cu.conversation_id = (SELECT cu2.conversation_id FROM conversation_user cu2 WHERE cu2.member_id=2 LIMIT 1)

Several top numbers in a column T-SQL

I have a table called _Invoice in SQL Server 2016 - like this:
Company InvoiceNo
-----------------
10 1
10 2
10 3
20 1
20 2
20 3
20 4
I want to get the highest value from all companies.
Like this:
Company InvoiceNo
-----------------
10 3
20 3
I want this data to then update another table that is called InvoiceSeries
where the InvoiceNo is higher than the NextNo in InvoiceSeries table
I am stuck with getting the highest data from InvoiceNo:
UPDATE InvoiceSeries
SET NextNo = -- Highest number from each company--
FROM InvoiceSeries ise
JOIN _Invoice i ON ise.InvoiceSeries = i.InvoiceSeries
WHERE i.InvoiceNo > ise.NextNo
Some example data:
Columns in InvoiceSeries Columns in _Invoices
Company NextNo Company InvoiceNo
10 9007 10 9008
20 1001 10 9009
10 9010
10 9011
10 9012
20 1002
20 1003
20 1004
If I understand correctly, you are looking for the HIGHEST common invoice number
Example
Select A.*
From YourTable A
Join (
Select Top 1 with ties
InvoiceNo
From YourTable
Group By InvoiceNo
Having count(Distinct Company) = (Select count(Distinct Company) From YourTable)
Order By InvoiceNo Desc
) B on A.InvoiceNo=B.InvoiceNo
Returns
Company InvoiceNo
10 3
20 3
EDIT - Updated for comment
Select company
,Invoice=max(invoiceno)
From YourTable
Group By company
This answer assumes there will be a record in the Invoice Series table.
--Insert Sample Data
CREATE TABLE #_Invoice (Company INT, InvoiceNo INT)
INSERT INTO #_Invoice(Company, InvoiceNo)
VALUES
(10 , 1),
(10 , 2),
(10 , 3),
(20 , 1),
(20 , 2),
(20 , 3),
(20 , 4)
CREATE TABLE #InvoiceSeries(Company INT, NextNo INT)
INSERT INTO #InvoiceSeries(Company, NextNo)
VALUES
(10, 1),
(20 ,1)
UPDATE s
SET NextNo = MaxInvoiceNo
FROM #InvoiceSeries s
INNER JOIN (
--Get the Max invoice number per company
SELECT Company, MAX(InvoiceNo) as MaxInvoiceNo
FROM #_Invoice
GROUP BY Company
) i on i.Company = s.Company
AND s.NextNo < i.MaxInvoiceNo --Only join to records where the 'nextno' is less than the max
--Confirm results
SELECT * FROM #InvoiceSeries
DROP TABLE #InvoiceSeries
DROP TABLE #_Invoice

PostgreSQL Query to get the output from same table with same table values

I have one table
id employee leave_days leave_type type
1 ABC 10 sick remove
2 ABC 20 sick add
3 ABC 15 Annual remove
4 ABC 50 Annual add
5 XYZ 10 sick remove
6 XYZ 20 sick add
7 XYZ 15 Annual remove
8 XYZ 50 Annual add
From the above table I will group by the column name called leave_type and then I will merge rows and the output should be as follows.
I have to group by column name leave_type and add new column called leave_allocated . In the leave_allocated column, the column type with value add only will come.
id employee leave_days leave_type leave_allocated
1 ABC 10 sick 20
2 ABC 15 Annual 50
3 XYZ 10 sick 20
4 XYZ 15 Annual 50
I tried with sub query I could not match the inner query with outer query .
This should help
SELECT id,
employee,
leave_dates,
leave type,
(SELECT leave_days
FROM TABLE t2
WHERE t2.id = t1.id
AND t2.type = 'add'
) leave_allocated
FROM TABLE t1
WHERE t1.type = 'remove'

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

Making a query in MS Access

I am having a problem with my query. I have 2 tables:
Table 1 is AutoCompany it has fields company and CodeCar. CodeCar can be 3 or 4 depending on the type of car that company has.
table 1: AutoCompany
company| CodeCar|
jora 3
jora 4
jora 3
ghita 3
ghita 3
ghita 4
gheorghe 4
gheorghe 3
gheorghe 3
Table 2 CodeCarCompanies has the codes:
car | codeCar
mers 3
vW 4
I need to select the companies with the count of the occurance of the 2 codeCars
resulting in something like this:
company | MERS| VW
jora 2 1
ghita 2 1
gheorghe 2 1
My attempt so far:
SELECT COUNT(dbo.AutoComany) AS MERS, dbo.Company, COUNT(dbo.AutoComany.
[CodeCar]) AS VW,
FROM dbo.AutoComany FULL OUTER JOIN
dbo.AutoComany ON dbo.АВТОМОБ.КодПредпр = AutoCompany.company
WHERE (dbo.CodeCarComapnies.[CodeCar] = 3)
GROUP BY dbo..company, dbo.CodeCarComapnies.[CodeCar]
HAVING (dbo.CodeCarComapnies.[CodeCar] = 4)
In MS Access, I think you want:
SELECT codecarcomapnies.car,
Count(autocompany.codecar) AS CountOfCodeCar
FROM autocompany
INNER JOIN codecarcomapnies
ON autocompany.codecar = codecarcomapnies.codecar
WHERE autocompany.codecar IN ( 3, 4 )
GROUP BY codecarcomapnies.car;
The above was built using the MS Access query design window and the Sum Σ button
Edit re Comment
SELECT Sum(IIf([autocompany].[codecar]=3,1,0)) AS mers,
Sum(IIf([autocompany].[codecar]=4,1,0)) AS vw
FROM autocompany
Or
TRANSFORM Count(autocompany.CodeCar) AS CountOfCodeCar
SELECT "Total" AS Total
FROM autocompany
INNER JOIN CodeCarComapnies
ON autocompany.CodeCar = CodeCarComapnies.codeCar
WHERE autocompany.CodeCar In (3,4)
GROUP BY "Total"
PIVOT CodeCarComapnies.car