I have a table that maintains records of employers and employees' data. Something like this
EmployerName EmployerPhone EmployerAddress EmployeeName EmployeePhone EmployeeAddress Date
-------------------------------------------------------------------------------------------------------
John 12345 NewYork Harry 59786 NewYork 12-1-1991
Mac 22345 Bankok John 12345 Delhi 12-3-1991
Smith 54732 Arab Amar 59226 China 21-6-1991
Sarah 12345 Bhutan Mac 22345 NewYork 5-9-1991
Root 85674 NewYork Smith 54732 Japan 2-11-1991
I have another table that will have generic records on the basis of phone number (both employers and employees).
Table structure is as following
Phone Name Address
I want to put latest records according to date from Table1 to Table2 on the basis of phone..
Like this
Phone Name Address
-----------------------
59786 Harry NewYork
22345 Mac NewYork
59226 Amar China
12345 Sarah Bhutan
22345 Mac NewYork
85674 Root NewYork
54732 Smith Arab
I've written many queries but couldn't find anyone resulted as required.
Any kind of help will be appreciated.
For initialize the table without phone duplicates:
INSERT IGNORE INTO Table2 (Phone, Name, Address)
SELECT X.* FROM (
SELECT EmployeeName,EmployeePhone,EmployeeAddress FROM Table1
UNION
SELECT EmployerName,EmployerPhone,EmployerAddress FROM Table1
) X
WHERE NOT EXISTS (SELECT Phone FROM Table2 WHERE Phone=X.Phone)
I think this is what you are looking for if I understand your question correctly. Should work for a once-off
DECLARE #restbl TABLE
(
Name varchar(100),
Phone varchar(20),
Addr varchar(100),
[Date] date,
RecType varchar(100)
)
INSERT INTO #restbl
SELECT EmployerName, EmployerPhone, NULL, MAX([Date]), 'Employer'
FROM #tbl
GROUP BY EmployerName, EmployerPhone
INSERT INTO #restbl
SELECT EmployeeName, EmployeePhone, NULL, MAX([Date]), 'Employee'
FROM #tbl
GROUP BY EmployeeName, EmployeePhone;
WITH LatestData (Name, Phone, [Date])
AS
(
SELECT Name, Phone, MAX([Date])
FROM #restbl
GROUP BY Name, Phone
)
INSERT INTO FinalTable (Name, Phone, [Address])
SELECT DISTINCT ld.Name, ld.Phone, ISNULL(tEmployer.EmployerAddress, tEmployee.EmployeeAddress) AS [Address]
FROM LatestData ld
LEFT JOIN #tbl tEmployer ON ld.Name = tEmployer.EmployerName AND ld.Phone = tEmployer.EmployerPhone AND ld.Date = tEmployer.Date
LEFT JOIN #tbl tEmployee ON ld.Name = tEmployee.EmployeeName AND ld.Phone = tEmployee.EmployeePhone AND ld.Date = tEmployee.Date
Related
I want to convert a row in sql table to multiple rows in other table.
example: say if i'm having a table 'UserDetail' and he has 2 address's (home, office...) then the table looks like...
I wand the result to be in the second table as shown in the image
We can use Cross Apply
;WITH CTE(UseriD,Address1Line,Address1City,Address1State,Address2Line,Address2City,Address2State )
AS
(
SELECT 1,'Line1','City1','State1','Line2','City2','State2'
)
SELECT UseriD,[Address],City,[State]
FROM CTE
CROSS APPLY ( VALUES (Address1Line,Address1City,Address1State ),
(Address2Line,Address2City,Address2State )
)AS Dt([Address],City,[State])
Result
UseriD Address City State
-----------------------------
1 Line1 City1 State1
1 Line2 City2 State2
Demo:http://rextester.com/KHFUM28227
You could use "union all" to do that like:
select * into newTable
from
(
select UserId, Address1Line as Address, Address1City as City, Address1State as State
from myTable
union all
select UserId, Address2Line as Address, Address2City as City, Address2State as State
from myTable
) tmp
If you use just UNION instead of UNION ALL you would also be removing the duplicates where Address1 and Address2 is same.
You can CROSS JOIN or CROSS APPLY the table to a list of numbers.
Then use IIF or CASE to get the correspondent numbered fields.
Or CROSS APPLY on the address values directly on the table.
Example snippet:
declare #UserTable table (UserId int, Address1Line varchar(30), Address1City varchar(30), Address1State varchar(30), Address2Line varchar(30), Address2City varchar(30), Address2State varchar(30));
insert into #UserTable (UserId, Address1Line, Address1City, Address1State, Address2Line, Address2City, Address2State) values
(1,'Wonder Lane 42','WonderTown', 'WonderState', 'Somewhere 1 B', 'Nowhere', 'Anywhere'),
(2,'Backstreet 69','Los Libros', 'Everland', 'Immortal Cave 777', 'Ghost City', 'The Wild Lands');
-- Cross Join on numbers
select UserId,
case n when 1 then Address1Line when 2 then Address2Line end as [Address],
case n when 1 then Address1City when 2 then Address2City end as [City],
case n when 1 then Address1State when 2 then Address2State end as [State]
from #UserTable u
cross join (values (1),(2)) as nums(n);
-- Cross Apply on Adress values
select UserId, [Address], [City], [State]
from #UserTable Usr
cross apply (values
(1, Address1Line, Address1City, Address1State),
(2, Address2Line, Address2City, Address2State)
) AS Addr(n, [Address], [City], [State]);
Both return:
UserId Address City State
------ ----------------- ---------- --------------
1 Wonder Lane 42 WonderTown WonderState
1 Somewhere 1 B Nowhere Anywhere
2 Backstreet 69 Los Libros Everland
2 Immortal Cave 777 Ghost City The Wild Lands
I am currently working with a spreadsheet in MS Access 2010 which contains about 130k rows of information about people who voted in a local election recently. Each row has their residential information (street name, number, postcode etc.) and personal information (title, surname, forename, middle name, DOB etc.). Each row represents an individual person rather than a household (therefore in many cases the same residential address appears more than once as more than one person resides in a particular household).
What I want to achieve is basically to create a new field in this dataset called 'count'. I want this field to give me a count of how many different surnames reside at a single address.
Is there an SQL script that will allow me to do this in Access 2010?
+------------------+----------+-------+---------+----------+-------------+
| PROPERTYADDRESS1 | POSTCODE | TITLE | SURNAME | FORENAME | MIDDLE_NAME |
+------------------+----------+-------+---------+----------+-------------+
FAKEADDRESS1 EEE 5GG MR BLOGGS JOE N
FAKEADDRESS2 EEE 5BB MRS BLOGGS SUZANNE P
FAKEADDRESS3 EEE 5RG MS SMITH PAULINE S
FAKEADDRESS4 EEE 4BV DR JONES ANNE D
FAKEADDRESS5 EEE 3AS MR TAYLOR STUART A
The following syntax has got me close so far:
SELECT COUNT(electoral.SURNAME)
FROM electoral
GROUP BY electoral.UPRN
However, instead of returning me all 130k odd rows, it only returns me around 67k rows. Is there anything I can do to the syntax to achieve the same result, but just returning every single row?
Any help is greatly appreciated!
Thanks
You could use something like this:
select *,
count(surname) over (partition by householdName)
from myTable
If you have only one column which contains the name,
ex: Rob Adams
then you can do this to have all the surnames in a different column so it will be easier in the select:
SELECT LEFT('HELLO WORLD',CHARINDEX(' ','HELLO WORLD')-1)
in our example:
select right (surmane, charindex (' ',surname)-1) as surname
example on how to use charindex, left and right here:
http://social.technet.microsoft.com/wiki/contents/articles/17948.t-sql-right-left-substring-and-charindex-functions.aspx
if there are any questions, leave a comment.
EDIT: I edited the query, had a syntax error, please try it again. This works on sql server.
here is an example:
create table #temp (id int, PropertyAddress varchar(50), surname varchar(50), forname varchar(50))
insert into #temp values
(1, 'hiddenBase', 'Adamns' , 'Kara' ),
(2, 'hiddenBase', 'Adamns' , 'Anne' ),
(3, 'hiddenBase', 'Adamns' , 'John' ),
(4, 'QueensResidence', 'Queen' , 'Oliver' ),
(5, 'QueensResidence', 'Queen' , 'Moira' ),
(6, 'superSecretBase', 'Diggle' , 'John' ),
(7, 'NandaParbat', 'Merlin' , 'Malcom' )
select * from #temp
select *,
count (surname) over (partition by PropertyAddress) as CountMembers
from #temp
gives:
1 hiddenBase Adamns Kara 3
2 hiddenBase Adamns Anne 3
3 hiddenBase Adamns John 3
7 NandaParbat Merlin Malcom 1
4 QueensResidence Queen Oliver 2
5 QueensResidence Queen Moira 2
6 superSecretBase Diggle John 1
Your query should look like this:
select *,
count (SURNAME) over (partition by PropertyAddress) as CountFamilyMembers
from electoral
EDIT
If over partition by isn't supported, then I guess you can get to your desired result by using group by
select *,
count (SURNAME) over (partition by PropertyAddress) as CountFamilyMembers
from electoral
group by -- put here the fields in the select (one by one), however you can't write group by *
GROUP BY creates an aggregate query, so it's by design that you get fewer records (one per UPRN).
To get the count for each row in the original table, you can join the table with the aggregate query:
SELECT electoral.*, elCount.NumberOfPeople
FROM electoral
INNER JOIN
(
SELECT UPRN, COUNT(*) AS NumberOfPeople
FROM electoral
GROUP BY UPRN
) AS elCount
ON electoral.UPRN = elCount.UPRN
Given the update I want to post another answer. Try it like this:
create table #temp2 ( PropertyAddress1 varchar(50), POSTCODE varchar(20), TITLE varchar (20),
surname varchar(50), FORENAME varchar(50), MIDDLE_NAME varchar (50) )
insert into #temp2 values
('FAKEADDRESS1', 'EEE 5GG', 'MR', 'BLOGGS', 'JOE', 'N'),
('FAKEADDRESS1', 'EEE 5BB', 'MRS', 'BLOGGS', 'SUZANNE', 'P'),
('FAKEADDRESS2', 'EEE 5RG', 'MS', 'SMITH', 'PAULINE', 'S'),
('FAKEADDRESS3', 'EEE 4BV', 'DR', 'JONES', 'ANNE', 'D'),
('FAKEADDRESS4', 'EEE 3AS', 'MR', 'TAYLOR', 'STUART', 'A')
select PropertyAddress1, surname,count (#temp2.surname) as CountADD
into #countTemp
from #temp2
group by PropertyAddress1, surname
select * from #temp2 t2
left join #countTemp ct
on t2.PropertyAddress1 = ct.PropertyAddress1 and t2.surname = ct.surname
This yields:
PropertyAddress1 POSTCODE TITLE surname FORENAME MIDDLE_NAME PropertyAddress1 surname CountADD
FAKEADDRESS1 EEE 5GG MR BLOGGS JOE N FAKEADDRESS1 BLOGGS 2
FAKEADDRESS1 EEE 5BB MRS BLOGGS SUZANNE P FAKEADDRESS1 BLOGGS 2
FAKEADDRESS2 EEE 5RG MS SMITH PAULINE S FAKEADDRESS2 SMITH 1
FAKEADDRESS3 EEE 4BV DR JONES ANNE D FAKEADDRESS3 JONES 1
FAKEADDRESS4 EEE 3AS MR TAYLOR STUART A FAKEADDRESS4 TAYLOR 1
I have a table, EmpDetail, with three columns:
EMP_NAME DETAIL_ID DETAIL
----------------------------------
ABC ADDRESS abc123
ABC PHONE 12345
ABC EMAIL abc#xyz
BCD ADDRESS bcd234
BCD PHONE 23456
BCD EMAIL bcd#345
I want to create a new table such that the values from the DETAIL_ID become the new column names:
EMP_NAME ADDRESS PHONE EMAIL
----------------------------------------
ABC abc123 12345 abc#xyz
BCD bcd234 23456 bcd#345
I have already tried Join but i am unable to get the desired result.
Please suggest a way to do this.
Here's an example of how to join for just two details, address and phone:
insert into NewTable
(EMP_NAME, ADDRESS, PHONE)
select name.EMP_NAME
, addr.DETAIL
, phone.DETAIL
from (
select distinct EMP_NAME
from OldTable
) name
left join
OldTable addr
on addr.EMP_NAME = name.EMP_NAME
and addr.DETAIL_ID = 'ADDRESS'
left join
OldTable phone
on phone.EMP_NAME = name.EMP_NAME
and phone.DETAIL_ID = 'PHONE'
Grouping also works, probably a little simpler:
insert into NewTable
(EMP_NAME, ADDRESS, PHONE)
select EMP_NAME
, max(case when DETAIL_ID = 'ADDRESS' then DETAIL end)
, max(case when DETAIL_ID = 'PHONE' then DETAIL end)
from OldTable
group by
EMP_NAME
CREATE TABLE NewTable AS
SELECT EMP_NAME ,
wm_concat(ADDRESS) ADDRESS,
wm_concat(PHONE) PHONE,
wm_concat(EMAIL) EMAIL
FROM
(SELECT EMP_NAME ,
CASE
WHEN DETAIL_ID='ADDRESS'
THEN DETAIL
ELSE NULL
END ADDRESS ,
CASE
WHEN DETAIL_ID='PHONE'
THEN DETAIL
ELSE NULL
END PHONE ,
CASE
WHEN DETAIL_ID='EMAIL'
THEN DETAIL
ELSE NULL
END EMAIL
FROM EmpDetail
)
GROUP BY EMP_NAME;
Summary
Okay, back in 2007 I've been asked to produce a tiny piece of software which objective was to input persons names, address and phone number according to the local phone directory.
At this time, the only requirement was to be able to group this list by street name. Hence, the street name in the address was sufficient.
It's been the third year now that once a year I'm having this headache of reuniting the street names and sectors together. A sector in nothing more than "downtown", "upper city", "125e", "over 125e" and "unknown" for streets I can't classify.
Data sample and structure
I have an initial table which was created the first time the software was delivered. I will make it SQL Server, as I imported the data into it for ease of work.
CREATE TABLE Contacts (
ContactId int not null identity(1, 1) primary key
, lastname nvarchar(50) not null
, firstname nvarchar(20) not null
, civic nvarchar(10) not null
, street nvarchar(20) not null
, city nvarchar(20) not null
, phone bigint not null
)
-- With the following sample data:
insert into Contacts (lastname, firstname, civic, street, city, phone)
values (N'LNAME-5551231234', N'A', N'89', N'MY STREET', N'SHAWINIGAN', 5551231234)
GO
insert into Contacts (lastname, firstname, civic, street, city, phone)
values (N'LNAME-5559879876', N'FNAME', N'10', N'YOUR STREET', N'SHAWINIGAN', 5559879876)
GO
insert into Constacts (lastname, firstname, civic, street, city, phone)
values (N'LNAME-5554564567', N'AFNAME', N'25', N'HIS STREET', N'SHAWINIGAN-SUD', 5554564567)
GO
Then, I added tables with the street names correctly orthographed, and another for the different sectors.
-- Sectors
CREATE TABLE Sectors (
sectorId int not null identity(1, 1) primary key
, sectorName nvarchar(20) not null
)
GO
insert into Sectors (sectorName)
values (N'Downtown')
GO
insert into Sectors (sectorName)
values (N'Upper city')
GO
-- Streets
CREATE TABLE Streets (
streetId int not null identity(1, 1) primary key
, sectorId int not null references Sectors (sectorId)
, streetName nvarchar(20) not null
)
GO
insert into Streets (sectorId, streetName)
values (1, N'My St.')
GO
insert into Streets (sectorId, streetName)
values(1, N'Ur Street')
GO
insert into Streets (sectorId, streetName)
values (2, N'HIS STREET')
GO
Which would result, for the benefit of the explanation:
Sectors
sectorId | sectorName
---------------------
1 | Downtown
2 | Upper city
Streets
streetId | sectorId | streetName
--------------------------------
1 | 1 | My St.
2 | 1 | Ur Street
3 | 2 | HIS STREET
Contacts
contactId | lastname | firstname | civic | street | city | phone
--------------------------------------------------------------------------------------------
1 | LNAME-5551231234 | A | 89 | My Street | SHAWINIGAN | 5551231234
2 | LNAME-5559879876 | FNAME | 10 | Your Street | SHAWINIGAN | 5559879876
3 | LNAME-5554564567 | AFNAME | 25 | HIS STREET | SHAWINIGAN-SUD | 5554564567
Objective
I got to resolve the street name conflict due to the orthograph. First, it seems that the field Contacts.street holds one value which exists in Streets.streetName. Thus, when I comparing with an equal (=) sign, I get about 6,000 rows only, when the population of the city is about 13,000 people.
Because of this, I try to join the tables with a like clause, but then I can gather about 20,000 rows, with duplicates lastname, name, civic, phone information combination from Contacts.
In addition to it, I seem to lack of precision or don't know how to say, but when I'm using a like, I get some strange results.
The results obtained are, for instance, let's consider I have the street 125e Rue in Streets, and having 12e Rue, 25e Rue in Contacts, then it looks like the Contact is duplicated because both streets meet the like pattern. (It would be so much easier with production data to understand, but these are people addresses and phone number, so I can't...)
Queries tempted so far
This query produces the kind of above-mentioned duplicates information, but only duplicate information from Contacts, as the Streets.streetName change from a record to another in the scope of this query. Besides, this query makes look the information like if there were multiple addresses for A LASTNAME-5551231234, for instance.
select c.city
, s.sectorName
, st.streetName
, c.civic
, c.lastname
, c.firstname
, c.phone
from Contacts c
inner join Streets st on st.streetName like N'%' + c.street + N'%'
inner join Sectors s on s.sectorId = st.sectorId
group by c.city
, s.sectorName
, st.streetName
, c.civic
, c.lastname
, c.firstname
, c.phone
order by c.city
, s.sectorName
, st.streetName
, c.civic
, c.lastname
Another query, from which I would have liked to inspire myself of since it looks like it produces the right results, when we remove as much information as possible from the Contacts table.
Finally, I'm pretty confused myself, and I don't expect one of you, professional developers and DBA, can help me with one simple answer, but with a walkthrough and an empirical approach, so I'm willing to try anything you may tink of that I have not already thought of.
Thanks for any help you provide. =)
surely it can't be this simple...
select c.city
, s.sectorName
, st.streetName
, c.civic
, c.lastname
, c.firstname
, c.phone
from Contacts c
OUTER JOIN Streets st on st.streetName = c.street
inner join Sectors s on s.sectorId = st.sectorId
group by c.city
, s.sectorName
, st.streetName
, c.civic
, c.lastname
, c.firstname
, c.phone
order by c.city
, s.sectorName
, st.streetName
, c.civic
, c.lastname
I have an output like this:
id name date school school1
1 john 11/11/2001 nyu ucla
1 john 11/11/2001 ucla nyu
2 paul 11/11/2011 uft mit
2 paul 11/11/2011 mit uft
I would like to achieve this:
id name date school school1
1 john 11/11/2001 nyu ucla
2 paul 11/11/2011 mit uft
I am using direct join as in:
select distinct
a.id, a.name,
b.date,
c.school
a1.id, a1.name,
b1.date,
c1.school
from table a, table b, table c,table a1, table b1, table c1
where
a.id=b.id
and...
Any ideas?
We will need more information such as what your tables contain and what you are after.
One thing I noticed is you have a school and then school1. 3nf states that you should never duplicate fields and append numbers to them to get more information even if you think that the relationship will only be 1 or 2 additional items. You need to create a second table that stores a user associated with 1 to many schools.
I agree with everyone else that both your source table and your desired output are poor design. While you probably can't do anything about your source table, I recommend the following code and output:
Select id, name, date, school from MyTable;
union
Select id, name, date, school1 from MyTable;
(repeat as necessary)
This will give you results in the format:
id name date school
1 john 11/11/2001 nyu
1 john 11/11/2001 ucla
2 paul 11/11/2011 mit
2 paul 11/11/2011 uft
(Note: in my version of SQL, union queries automatically select distinct records so the distinct flag isn't needed)
With this format, you could easily count the number of schools per student, number of students per school, etc.
If processing time and/or storage space is a factor here, you could then split this into 2 tables, 1 with the id,name & date, the other with the id & school (basically what JonH just said). But if you're just working up some simple statistics, this should suffice.
This problem was just too irresistable, so I just took a guess at the data structures that we are dealing with. The technology wasn't specified in the question. This is in Transact-SQL.
create table student
(
id int not null primary key identity,
name nvarchar(100) not null default '',
graduation_date date not null default getdate(),
)
go
create table school
(
id int not null primary key identity,
name nvarchar(100) not null default ''
)
go
create table student_school_asc
(
student_id int not null foreign key references student (id),
school_id int not null foreign key references school (id),
primary key (student_id, school_id)
)
go
insert into student (name, graduation_date) values ('john', '2001-11-11')
insert into student (name, graduation_date) values ('paul', '2011-11-11')
insert into school (name) values ('nyu')
insert into school (name) values ('ucla')
insert into school (name) values ('uft')
insert into school (name) values ('mit')
insert into student_school_asc (student_id, school_id) values (1,1)
insert into student_school_asc (student_id, school_id) values (1,2)
insert into student_school_asc (student_id, school_id) values (2,3)
insert into student_school_asc (student_id, school_id) values (2,4)
select
s.id,
s.name,
s.graduation_date as [date],
(select max(name) from
(select name,
RANK() over (order by name) as rank_num
from school sc
inner join student_school_asc ssa on ssa.school_id = sc.id
where ssa.student_id = s.id) s1 where s1.rank_num = 1) as school,
(select max(name) from
(select name,
RANK() over (order by name) as rank_num
from school sc
inner join student_school_asc ssa on ssa.school_id = sc.id
where ssa.student_id = s.id) s2 where s2.rank_num = 2) as school1
from
student s
Result:
id name date school school1
--- ----- ---------- ------- --------
1 john 2001-11-11 nyu ucla
2 paul 2011-11-11 mit uft