我目前正在使用MS Access 2010中的电子表格,其中包含大约130,000行关于最近在当地选举中投票的人的信息。每行都有他们的住宅信息(街道名称,号码,邮政编码等)和个人信息(标题,姓氏,姓名,中间名,DOB等)。每行代表一个人而不是一个家庭(因此在许多情况下,同一住宅地址不止一次出现,因为不止一个人居住在特定家庭)。
我想要实现的目标基本上是在这个名为' count'的数据集中创建一个新字段。我希望这个字段能够计算出一个地址中有多少个不同的姓氏。
是否有允许我在Access 2010中执行此操作的SQL脚本?
+------------------+----------+-------+---------+----------+-------------+
| 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
到目前为止,以下语法让我接近:
SELECT COUNT(electoral.SURNAME)
FROM electoral
GROUP BY electoral.UPRN
但是,它不会返回给我所有130k的奇数行,而是只返回大约67k行。有什么我可以做的语法来实现相同的结果,但只返回每一行?
非常感谢任何帮助!
谢谢
答案 0 :(得分:1)
您可以使用以下内容:
select *,
count(surname) over (partition by householdName)
from myTable
如果您只有一列包含该名称,
例如:Rob Adams
那么你可以这样做,让所有的姓氏都在不同的栏目中,这样在选择中会更容易:
SELECT LEFT('HELLO WORLD',CHARINDEX(' ','HELLO WORLD')-1)
在我们的示例中:
select right (surmane, charindex (' ',surname)-1) as surname
如何在这里左右使用charindex的示例:
如果有任何问题,请发表评论。
编辑:我编辑了查询,出现语法错误,请再试一次。这适用于sql server。
这是一个例子:
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
给出:
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
您的查询应如下所示:
select *,
count (SURNAME) over (partition by PropertyAddress) as CountFamilyMembers
from electoral
修改强>
如果不支持over partition by,那么我猜您可以使用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 *
答案 1 :(得分:1)
GROUP BY会创建一个汇总查询,因此您可以获得更少的记录(每个UPRN一个)。
要获取原始表中每一行的计数,可以使用聚合查询加入表:
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
答案 2 :(得分:0)
鉴于更新,我想发布另一个答案。试试这样:
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
这会产生:
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