3

我是 SQL 新手。我正在寻找一个简单的 SQL 解决方案来组合行/列,其列包含相同的数据,在本例中为邮政编码。例如,数据如下所示:

州、县、市、邮编、计数

"CA","阿拉米达","海沃德","94541",5371
"CA","阿拉米达","海沃德","94542",2209
"CA","阿拉米达","海沃德","94544",7179
"CA","阿拉米达","海沃德","94545",4209
"CA","阿拉米达","卡斯特罗谷","94546",7213
"CA","阿拉米达","海沃德","94546",37
"CA","阿拉米达","LIVERMORE","94550",9809
"CA","阿拉米达","LIVERMORE","94551",6558
"加州","阿拉米达","卡斯特罗谷","94552",3121
"CA","阿拉米达","海沃德","94552",12
"CA","阿拉米达","弗里蒙特","94555",5392

我希望最终的数据看起来像这样:

州、县、市、邮编、计数

"CA","阿拉米达","海沃德","94541",5371
"CA","阿拉米达","海沃德","94542",2209
"CA","阿拉米达","海沃德","94544",7179
"CA","阿拉米达","海沃德","94545",4209
"CA","ALAMEDA","CASTRO VALLEY / HAYWARD","94546",7250
"CA","阿拉米达","LIVERMORE","94550",9809
"CA","阿拉米达","LIVERMORE","94551",6558
"CA","阿拉米达","CASTRO VALLEY HAYWARD","94552",3133
"CA","阿拉米达","弗里蒙特","94555",5392

您可以看到两行中的数据已被合并或求和。对于包含完全相同邮政编码的行,城市名称(两者)出现在城市列中,并且计数是每行计数的总和。

有没有办法使用 SQL 来做到这一点?即使它需要两个不同的 SQL 语句也没关系。

4

2 回答 2

2

假设使用 SQL Server,您可以使用它FOR XML来获得您想要的结果。

select distinct t.state,t.county,t.zip,t2.sumcount,
STUFF(
        (
            SELECT '/' + city AS [text()]
            FROM mytable t3
            WHERE t.zip = t3.zip
            FOR XML PATH('')
        ), 1, 1, '') AS ColList
from mytable t 
  join (select zip, sum(count) as sumcount
       from mytable
       group by zip) t2 on t.zip=t2.zip

还有一些SQL Fiddle

如果您使用的是 MySQL,请查看 using GROUP_CONCAT

select distinct t.state,t.county,t.zip,t2.sumcount,
GROUP_CONCAT(t.city) as cities
from mytable t 
  join (select zip, sum(count) as sumcount
       from mytable
       group by zip) t2 on t.zip=t2.zip
GROUP BY t.state,t.county,t.zip,t2.sumcount

还有更多小提琴

祝你好运。

于 2013-01-28T21:00:34.250 回答
0

我调整了 sgeddes 的出色答案以实际获得计数并避免重复条目,并添加了一个支持脚本,以便您对其进行测试。这确实假设 SQL Server。

IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'MYTABLE') BEGIN
  drop table MYTABLE;
END;
go

create table MYTABLE
(
  state nvarchar(2)
 ,county nvarchar(100)
 ,city nvarchar(100)
 ,zip nvarchar(10)
)
go

insert into MYTABLE(state,county,city,zip) values('CA','ALAMEDA','HAYWARD','94541');
insert into MYTABLE(state,county,city,zip) values('CA','ALAMEDA','HAYWARD','94541');
insert into MYTABLE(state,county,city,zip) values('CA','ALAMEDA','HAYWARD','94544');
insert into MYTABLE(state,county,city,zip) values('CA','ALAMEDA','HAYWARD','94545');
insert into MYTABLE(state,county,city,zip) values('CA','ALAMEDA','CASTRO VALLEY','94546');
insert into MYTABLE(state,county,city,zip) values('CA','ALAMEDA','CASTRO VALLEY','94546');
insert into MYTABLE(state,county,city,zip) values('CA','ALAMEDA','CASTRO VALLEY','94546');
insert into MYTABLE(state,county,city,zip) values('CA','ALAMEDA','CASTRO VALLEY','94546');
insert into MYTABLE(state,county,city,zip) values('CA','ALAMEDA','CASTRO VALLEY','94546');
insert into MYTABLE(state,county,city,zip) values('CA','ALAMEDA','HAYWARD','94546');
insert into MYTABLE(state,county,city,zip) values('CA','ALAMEDA','HAYWARD','94546');
insert into MYTABLE(state,county,city,zip) values('CA','ALAMEDA','HAYWARD','94546');
insert into MYTABLE(state,county,city,zip) values('CA','ALAMEDA','HAYWARD','94546');
insert into MYTABLE(state,county,city,zip) values('CA','ALAMEDA','LIVERMORE','94550');
insert into MYTABLE(state,county,city,zip) values('CA','ALAMEDA','LIVERMORE','94551');
insert into MYTABLE(state,county,city,zip) values('CA','ALAMEDA','CASTRO VALLEY','94552');
insert into MYTABLE(state,county,city,zip) values('CA','ALAMEDA','HAYWARD','94552');
insert into MYTABLE(state,county,city,zip) values('CA','ALAMEDA','FREMONT','94555');

select distinct
  t.state
 ,t.county
 ,t.zip
 ,t2.sumcount
 ,STUFF((
       SELECT distinct '/' + city AS [text()]
       FROM mytable t3
       WHERE t.zip = t3.zip
       FOR XML PATH('')
       ), 1, 1, '') AS ColList
from
  mytable t 
  inner join
  (
  select zip, sum(count) as sumcount
  from
    (
    select zip,count(*) as count
    from mytable
    group by zip
    ) x
  group by zip
  ) t2
  on t.zip=t2.zip

输出如下所示:

state   county  zip sumcount    ColList
CA  ALAMEDA 94541   2   HAYWARD
CA  ALAMEDA 94544   1   HAYWARD
CA  ALAMEDA 94545   1   HAYWARD
CA  ALAMEDA 94546   9   CASTRO VALLEY/HAYWARD
CA  ALAMEDA 94550   1   LIVERMORE
CA  ALAMEDA 94551   1   LIVERMORE
CA  ALAMEDA 94552   2   CASTRO VALLEY/HAYWARD
CA  ALAMEDA 94555   1   FREMONT
于 2013-01-28T21:10:25.460 回答