Ms-access query to merge rows - sql

I want a query to merge each of two rows meeting the following conditions in a large table:
Surname, Name, FatherName are duplicates
1, 2 contain "---" on one of the rows and 3, 4 contain "---" on the other row
Example data:
Surname Name FatherName Phone Mobile 1 2 3 4
Smith Alex Nick 12345 00000 xxx zzz --- ---
Smith Alex Nick 12345 00000 --- --- vvv aaa
Jone Mary John 22222 11111 sss eee --- ---
Pan Peter Peter 33333 22222 ttt uuu --- ---
Bob Nick Nick 44444 77777 --- --- ppp qqq
Mary Maria John 99999 00000 jjj kkk --- ---
Mary Maria John 99999 00000 --- --- iii ---
Expected output:
Surname Name FatherName Phone Mobile 1 2 3 4
Smith Alex Nick 12345 00000 xxx zzz vvv aaa
Jone Mary John 22222 11111 sss eee --- ---
Pan Peter Peter 33333 22222 ttt uuu --- ---
Bob Nick Nick 44444 77777 --- --- ppp qqq
Mary Maria John 99999 00000 jjj kkk iii ---

Try this simple query:
Select
Surname,
Name,
FatherName,
Phone,
Mobile,
Max(T.[1]) As [1],
Max(T.[2]) As [2],
Max(T.[3]) As [3],
Max(T.[4]) As [4]
From
YourTable As T
Group By
Surname,
Name,
FatherName,
Phone,
Mobile

This will work for your sample data.
First with a self join I get the merged rows and then with UNION ALL for the unique rows:
SELECT t1.Surname, t1.Name, t1.FatherName, t1.Phone, t1.Mobile,
t1.[1], t1.[2], t2.[3], t2.[4]
FROM tablename t1 INNER JOIN tablename t2
ON t1.Surname = t2.Surname AND t1.Name = t2.Name AND t1.FatherName = t2.FatherName
AND (
(t1.[1] <> '---' OR t1.[2] <> '---')
AND
(t1.[3] = '---' AND t1.[4] = '---')
AND
(t2.[3] <> '---' OR t2.[4] <> '---')
AND
(t2.[1] = '---' AND t2.[2] = '---')
)
UNION ALL
SELECT * FROM tablename AS t1
WHERE NOT EXISTS (
SELECT 1 FROM tablename AS t2
WHERE t1.Surname = t2.Surname AND t1.Name = t2.Name AND t1.FatherName = t2.FatherName AND t1.[1] <> t2.[1]
)

Related

Google Sheets "=QUERY()" JOIN ON or equitant

I have one large spreadsheet with names, addresses, phone numbers, emails, Etc. Some records have a second address for which I have a column named "Address 2" I was hopping to write a query that would give me an output with duplicate rows of which the only difference was the "Address 2" column would be in the main address Column.
Data:
A
B
C
D
E
F
G
1
Status
Name
Address
Phone
Email
Address2
Hire Date
2
Joe Smith
123 Smith St
201 555 3099
Joe#stackoverflow.com
7th Avenue Sq
4
Q
Jane Smith
321 Not Smith St
12/15/1980
5
Robert Smith
818 555 4321
Robert#googlesheets.com
12/13/1981
Looking for an Query output to look like:
A
B
C
D
E
F
1
Status
Name
Address
Phone
Email
Hire Date
2
Joe Smith
123 Smith St
201 555 3099
Joe#stackoverflow.com
3
Joe Smith
7th Avenue Sq
201 555 3099
Joe#stackoverflow.com
4
Q
Jane Smith
321 Not Smith St
12/15/1980
5
Robert Smith
818 555 4321
Robert#googlesheets.com
12/13/1981
I was trying something like:
=QUERY({Sheet1!$A2:$G,Sheet1!$B2:$B,Sheet1!$F2:$J },"SELECT Col1, Col2, Col3, Col4, Col5, Col7 JOIN Col6 ON Col2 = Col2")
Which I think is more or less how it would be in SQL, but Google sheets doesn't have a join function.
Is there any way to get this done?
most simple you can do is:
=QUERY({A1:E, G1:G; A2:B, F2:F, D2:E, G2:G}, "where Col3 is not null", )
Something like this?
You can stack data of the same length with {},
this sample create 2 query function and stack them together.
=ArrayFormula(
LAMBDA(DATA_1,DATA_2,
QUERY({DATA_1;DATA_2},"WHERE Col2 IS NOT NULL ORDER BY Col2",1)
)(
QUERY({A1:G4},"SELECT "&JOIN(",","Col"&{1,2,3,4,5,7}),1),
QUERY({A1:G4},"SELECT "&JOIN(",","Col"&{1,2,6,4,5,7})&" WHERE Col6 IS NOT NULL LABEL "&JOIN(",","Col"&{1,2,6,4,5,7}&"''"),1)
)
)

SQL multiple columns value into one column

Here is my table design
Location Inventory
ID Name HostName LID1 LID2 LID3
1 AAA Peter 1 2 3
2 BBB Betty 2
3 CCC Charlie 1 2
As my expected result is like this below.
HostName Name
Peter AAA
BBB
CCC
Betty BBB
Charlie AAA
BBB
But I run the sql statement not like this.
SELECT HostName,location.Name AS Department
FROM inventory
INNER JOIN location ON inventory.LID1 = location.ID
UNION
SELECT HostName,location.Name AS Department
FROM inventory
INNER JOIN location ON inventory.LID2 = location.ID
UNION
SELECT HostName,location.Name AS Department
FROM inventory
INNER JOIN location ON inventory.LID3 = location.ID
HostName Name
Peter AAA
Peter BBB
Peter CCC
Betty BBB
Charlie AAA
Charlie BBB
Anyone can help me to solve the problem?
Thanks.

SQL Server display left join result set as additional columns instead of rows

I am working with SQL Server 2012, I have a two tables, Prospects and ProspectContact, with this sample data:
ProspectID ProspectName ProspectAdd
-----------------------------------------------
1 ABC Company India
2 XYZ Company UAE
3 PQR Company KSA
4 JKL Company INDIA
ProspectContacts table:
ProspectID CName CAdd CDesignation Email
---------------------------------------------------------------------------
1 Mr Sagar India Manager sagar#gmail.com
1 Mr Saurabh India Manager Saurabh#gmail.com
2 Mr Swami UAE Director swami#gmail.com
2 Mr .ABC UAE Engg abc#gmail.com
3 Mr PQR KSA Manager pqr#gmail.com
4 Mr XYZ INDIA Manager xyz#gmail.com
If I apply left join on the above tables with below query
SELECT
P.[PROSPECTNAME], P.[ADDRESS], PC.CONTACTPERSON, PC.EMAILID
FROM
[PROSPECTS] P
LEFT JOIN
[PROSPECTCONTACTS] PC ON P.PROSPECTID = PC.PROSPECTID
WHERE
P.USERID = #UserID
ORDER BY
CDATE DESC
I get this result set:
ProspectName Adress Name Email
---------------------------------------------------
ABC COMPANY INDIA Mr. Sagar sagar#gmail.com
ABC COMPANY INDIA Mr. Saurabh saurabh#gmail.com
whereas my expected result set would be this:
ProspectName Adress Name1 Email1 Name2 Email2
-----------------------------------------------------------------------------
ABC COMPANY INDIA Mr. Sagar sagar#gmail.com Mr.Saurabh saurabh#gmail.com
XYZ COMPNAY USA Mr.Swami swami#gmail.com Mr. ABC abc#gmail.com
PQR COMPANY KSA Mr.Pqr pqr#gmail.com NULL NULL
JKL COMPNAY INDIA Mr.XYZ xyz#gmail.com NULL NULL
Try something like this:
SELECT P.[PROSPECTNAME], P.[ADDRESS], PC.CONTACTPERSON, PC.EMAILID
FROM [PROSPECTCONTACTS] PC
LEFT JOIN [PROSPECTS] P
ON P.PROSPECTID=PC.PROSPECTID
WHERE P.USERID=#UserID ORDER BY CDATE Desc

Removing Duplicates Based date

I have the following Query to select (will use for an update statement) remove duplicates based on the min service date and keeping the most recent svc date.
select st.SubID, st.RecordNo, st.Fname, st.Lname, st.MemberID, st.ServiceDate, IsDeduped, DedupCriteria
from stagingtable st
join (select MemberID
from stagingtable
where SubID = 99999
and waveseqid = 1
group by MemberID
having count(*) > 1) st2
on st.MemberID = st2.MemberID
and st.ServiceDate = (Select min(ServiceDate) from stagingtable s where s.subid = 99999 and s.waveseqid = 1 and st.MemberID = s.MemberID)
where SubID = 99999
and waveseqid = 1
order by RecordNo
This seems to pull in sometime only pull in multiples with the same date for the memberid:
SurveyID RecordNo Fname Lname MemberID Option9 IsDeduped DedupCriteria
99999 1 John Doe 123 10/1/2015 0 NULL x These show on the query
99999 2 John Doe 123 10/1/2015 0 NULL x These show on the query
99999 3 John Doe 123 10/8/2015 0 NULL But expected these as well
99999 4 John Doe 123 10/12/2015 0 NULL But expected these as well
99999 4 John Doe 123 10/14/2015 0 NULL But expected these as well
99999 6 John Doe 123 10/29/2015 0 NULL But expected these as well
99999 7 John Doe 123 12/14/2015 0 NULL But expected these as well
Your "AND" statement restricts the results to only rows with the minimum service date.
and st.ServiceDate = (Select min(ServiceDate) from stagingtable s where s.subid = 99999 and s.waveseqid = 1 and st.MemberID = s.MemberID)
That's why you get two rows and not all of them.

Joining these tables

I have 3 tables as below :
Table Name :
------------
UserList
Column Name
-------------
DealerID DealerUserID
AAA 111
AAA 222
AAA 333
BBB 111
BBB 444
CCC 111
CCC 555
--
Table Name :
------------
UserInfo
Coulmns
--------
DealerUserID Name
111 John
222 James
333 Dany
444 Daniel
555 Romie
--
Table Name :
------------
CarPermitted
Coulmns
--------
DealerID DealerUserID
AAA 111
AAA 222
BBB 111
CCC 111
I want a result as below of a query which will take input as :
For DealerID = AAA
Name DealerUserID AllowedStatus
John 111 true
James 222 true
Dany 333 false
I have tried many joins as below, but could not get my desired result. Any suggestion how can I get it.
AllowedStatus is the value I need to fetch as :
If the combination of DealerID and DealerUserID from table UserList is present in CarPermitted///rest will be false..
Note : It will display all the dealeruserId belongs to one dealer
Here you go, you don't show your queries so I can say how you were going wrong
From the comments, Allowed status comes from if the record exists in the permitted table.
SELECT UF.Name, UF.DealerUserID,
CASE P.DealerID IS NULL THEN 'false' ELSE 'true' END AS AllowedStatus
FROM UserList UL
JOIN UserInfo UF ON UL.DealerUserID = UF.DealerUserID
LEFT JOIN CarPermitted P ON UL.DealerUserID = P.DealerUserID AND UL.DealerID = P.DealerID
WHERE UL.DealerID = 'AAA'